if is a most important feature of PHP as in many languages like C.
if statements can be nested infinitely within other if statements.
Sometimes you want more than one statement to be executed conditionally.
This can be done using control statement if.
In if you have to group number of statements that you want to execute has to put within curly brace.
Syntax of if
Following is a general syntax of if control structure:
1 2 3 4 |
if(expression) { // statements } |
If expression evaluates to TRUE, PHP will execute statement, and if it evaluates to FALSE it will ignored.
Flowchart of if
Example of if-
1 2 3 4 5 6 7 8 9 10 |
<?php $a = 10; $b = 5; if($a > $b) { echo "a is bigger than b"; } ?> /* output: a is bigger than b */ |
This is a simple example of if structure.
In this, expression $a > $b is true therefore the statements inside if get executed.