PHP. Conditional Operators

Home » Tutorials » PHP » PHP. Conditional Operators
Today we will consider conditional operators if, if…else, if…elseif…else. A lot of this you may know from javascript. If..else also has got short syntax. Also we’ll try to use switch, which replaces a lot of if.else entities.

if...else example

if(condition) {
	//some code
} elseif (condition) {
	//some code
} elseif (condition) {
	//some code
} elseif (condition) {
	//some code
}
 else {
	//some code
}
As you understand, it can be a lot of elseif entities in the code. Switch is more compact.

Code lesson

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
<?php 

$a = 4;

if($a>5) {
	echo $a;	
} else {
	echo $a-- ."<br>";
}

echo $b = ($a!=2) ? 2 : 1;

echo "<br>";

if(condition) {
	//some code
} elseif (condition) {
	//some code
} elseif (condition) {
	//some code
} elseif (condition) {
	//some code
}
 else {
	//some code
}

echo "b равно $b";

$b=7;

echo "<br>";

switch ($b) {
	case 5:
		echo "b равно 5";
		break;

	case 6:
		echo "b равно 6";
		break;	
	
	default:
		echo "b равно чему-то там";
		break;
}


?>

</body>
</html>

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This