Sunday 11 April 2021

Php: else statement

Syntax:

if(condition){

}
else{

}

 

If the condition true, then if block code executed. otherwise else block code executed.

 

Example

if ($a % 2 == 0) {
    echo "a is even number";
} else {
    echo "a is odd number";
}

 

if_else_demo.php

#!/usr/bin/php

<?php
$a = 11;

if ($a % 2 == 0) {
    echo "a is even number";
} else {
    echo "a is odd number";
}

?>

 

Output

$./if_else_demo.php 

a is odd number

 

Alternative if-else syntax

Syntax

if(condition) :
    ......
    ......
else
    ......
    ......
endif;

 

Example

if ($a % 2 == 0) :
    echo "a is even number";
else :
    echo "a is odd number";
endif;

 

if_else_alternative_syntax.php

#!/usr/bin/php

<?php
$a = 11;

if ($a % 2 == 0) :
    echo "a is even number";
else :
    echo "a is odd number";
endif;

?>

Output

$./if_else_alternative_syntax.php 

a is odd number




 

 

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment