welcome to tutorialfamily

php ifelse

PHP if..else – used for check condition and execute the code.

If…else are the statement of php.


PHP If else

We can check various condition and execute the code using if…else statement.

  • if
  • if else
  • if else elseif

If statement

Using this statement first of check condition, if condition is true, the code will execute


If statement



<!DOCTYPE html>
<html>
<head>
	<title>If statement</title>
</head>
<body>
// if statement
<?php

	$a=5;
	if($a<10)
	{
		echo "BMW is my favourite car"; // (code execute when first condition is true)
	}
  ?>						
</body>
</html>

output

PHP If else

We can check various condition and execute the code using if…else statement.

  • if else
  • if else elseif

If else statement

Using this statement first of check condition, if condition is true, the code will execute otherwise else part execute.


If else statement



<!DOCTYPE html>
<html>
<head>
	<title>If else statement</title>
</head>
<body>
// if...else
<?php

	$a=5;
	if($a>10)
	{
		echo "BMW is my favourite car"; // (code execute when first condition is true)
	}
	else{
		echo "Toyota is my favourite car"; // (code execute when first condition is false and this condition is true)
	}

  ?>						
</body>
</html>

output

if elseif else statement

Using this statement we can check multiple condition.

IF first condition are false then check the second condition. if second condition are also false then code execute the else part.


if elseif else statement



<!DOCTYPE html>
<html>
<head>
	<title>if elseif else statement</title>
</head>
<body>
// if...elseif..else
<?php

	$a=5;
	if($a>10)
	{
		echo "Toyota is my favourite car"; // (code execute when first condition is true)
	}
	elseif($a>10){
		echo "Oddi is my favourite car"; // (code execute when first condition is false and this condition is true)
	}
    else{
		echo "BMW is my favourite car"; // (code execute when all condition is false)
	}
  ?>	
</body>
</html>

output

Our Content