PHP If-else : Conditional block
PHP If-else : Conditional block
When particular condition evaluates to true then sequence of statements from if block are executed. If condition is false then some set of statements in else block gets executed.
Syntax -
if (condition) //Execute code if condition is true else //Execute code if condition is false
Example : Conditional Operators
<html> <body> <?php $curr_date = date("D"); if ($curr_date == "Sun") echo "Today is holiday"; else echo "Today is no holiday"; ?> </body> </html>
Output :
Today is no holiday
Explanation :
In the above example if the current day is sunday then if block gets executed.
if ($curr_date == "Sun")
If the condition inside the if fails then else part will be executed.
Tips : PHP If-else
Any non zero number (including negative) inside if condition can be considered as true condition.
if (100) echo "True condition"; else echo "False condition";
We can opt out curly braces in the if or else block if single statement is written inside the block
if (1) { echo "True condition 1"; echo "True condition 2"; } else echo "False condition";