Conditional Statements in PHP with Example

Conditional Statements in PHP with Example
Rating: 5 - 1 Votes

Conditional Statements in PHP with Example

phpIn PHP, if and elseif statements are conditional statements.  They work with comparison operators.  Conditional statements form a block where further code execution depends upon result of a condition.  The condition will return a boolean value (true or false).

If statement is always the first statement in the series of conditional statements.  If first statement fails to meet a certain condition, then it will jump to the next conditional statement which is elseif statement.  The last statement is always else statement in that series.  The code inside else statement always gets executed if both the previous conditional statements (if and elseif) are failed to meet a certain condition.

A very simple example is given below.  You can change the values of $n1, $n2, and $n3 to get the desired output.  By default, none of the values are equal, so it will go to else block.