PHP Condition statements
If statement and If...else statements are used to perform the different action based on the different conditions.
IF...Else Statement:
If you want to execute some code if a condition is true and another code if a condition is false,use this if..else statement.
Syntax:
If(condition)
//code will be executed if condition is true
else
//code will be executed if condition is false
Code:
<!DOCTYPE html>
<html>
<body>
<?php
$d=date("D");
if($d = = "Wed")
echo"Have a nice month";
else
echo"Have a nice day";
?>
</body>
</html>
Sometimes the condition line are more than one line the conditions should be enclosed within curly braces:
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$d=date("D");
if($d = = "Wed")
{
echo"Hi Students <br> " ;
echo"Have a nice month";
echo"See you later";
?>
</body>
</html>
TheElse If Statement:
If you want to execute some code if one of several conditions are true use the else if statement.
Syntax:
If(condition)
//code will be executed if condition is true
elseIf
//code will be executed if condition is true
else
//code will be executed if condition is false
Example:
<!DOCTYPE html>
<html>
<body>
<?php
$d=date("D");
if($d = = "Wed")
{
{
echo"Have a nice holiday" ;
elseif($d = = "mon")
elseif($d = = "mon")
echo"Have a nice monday";
else
else
echo"See you later";
}
}
?>
</body>
</html>
In the above code, the first condition is true the output will be "Have a nice holiday" or the second condition is true the output will be "Have a nice Monday".First two conditions are false only last condition is true the output will be "See you later".
In the above code, the first condition is true the output will be "Have a nice holiday" or the second condition is true the output will be "Have a nice Monday".First two conditions are false only last condition is true the output will be "See you later".
No comments:
Post a Comment