PHP If Else Statement

PHP If Statement Previous Next PHP If Elseif Else


PHP if else statement is used to execute some code only if a specified condition is true.

PHP The if...else Statement


The if...else statement is used to execute some code only if a specified condition is true.

Syntax


if(condition){ code to be executed if condition is true; }else{ code to be executed if condition is false; }

Here is the code of if...else statement.

Filename: php / if-else-statment.php


Code
<?php
// PHP if...else statement
$x=20;
$y="10";
if($x > $y){
 echo "$x is greater than $y";
}else{
  echo "$y is less than $x";
}
?>
Output
20 is greater than 10

Filename: php / ifelse1.php


Code
<?php
//ifelse1
$a = 10;
$b = 9;
if(a <= b){
echo "$a is less than equal to $b";
}else{
echo "$a is greater than equal to $b";
}
?>

Running Step.


Type Url - https://localhost/php/ifelse1.php

Filename: php / elsegreater.php


Code
<?php
//elsegreater
$a = 10;
$b = 20;
if(a > b){
echo "$a is greater than $b";
}else{
echo "$a is less than $b";
}
?>

Running Step.


Type Url - https://localhost/php/elsegreater.php

Filename: php / elsegreater1.php


Code
<?php
//elsegreater1
$a = 10;
$b = 11;
if(a >= b){
echo "$a is greater than equal to $b";
}else{
echo "$a is less than equal to $b";
}
?>

Running Step.


Type Url - https://localhost/php/elsegreater1.php

Filename: php / elseequal.php


Code
<?php
//elseequal
$a = 11;
$b = 10;
if(a == b){
echo "$a is equal to $b";
}else{
echo "$a is not equal to $b";
}
?>

Running Step.


Type Url - https://localhost/php/elseequal.php

Filename: php / elsenot.php


Code
<?php
//elsenot
$a = 10;
$b = 10;
if(a != b){
echo "$a  is not equal to $b";
}else{
echo "$a  is equal to $b";
}
?>

Running Step.


Type Url - https://localhost/php/elsenot.php


Tags: PHP


Was this helpful ?

Related Code


Popular Code

Latest Code