Thursday 8 April 2021

Php: if statement

"if" tell the program execute the section of code when the condition evaluates to true.

 

Syntax

if(condition){

         // Executes if the condition true

}

 

Example

if ($a % 2 == 0) {

    echo "a is even number";

}

 

Find the below working application.

 

if_condition_demo.php

#!/usr/bin/php

<?php
$a = 10;
$b = 11;

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

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

?>

 

Output

$./if_condition_demo.php 

a is even number

 

Alternative if syntax

if (condition) :

 

endif;

 

Example

if ($a % 2 == 0) :

    echo "a is even number";

endif;

 

if_alternative_syntax_demo.php

#!/usr/bin/php

<?php
$a = 10;

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

?>

 

Output

$./if_alternative_syntax_demo.php 

a is even number

 

 

 

 

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment