Conditional statement
When you write code, you often need to perform different actions for different judgments.
You can use conditional statements in your code to accomplish this task.
If...else statement
Executes a piece of code when the condition is set, and executes another piece of code when the condition is not established
ElseIf statement
In conjunction with If...else, a code block is executed when one of several conditions is true ... Else statement
Use the If....else statement if you want to execute some code when a condition is set up and execute some other code when the condition is not valid.
Grammar
if (condition)
Code to was executed if condition is true;
Else
Code to was executed if condition is false;
Instance
If the current date is Friday, the following code will output "Have a nice weekend!" or it will output "Have a nice day!" :
Copy Code code as follows:
<body>
<?php
$d =date ("D");
if ($d = = "Fri")
echo "Have a nice weekend!";
Else
echo "Have a nice day!";
?>
</body>
If you need to execute multiple lines of code when conditions are set up or not, you should include these lines of code in curly braces:
Copy Code code as follows:
<body>
<?php
$d =date ("D");
if ($d = = "Fri")
{
echo "Hello!<br/>";
echo "Have a nice weekend!";
echo "on monday!";
}
?>
</body>
ElseIf statement
If you want to execute code when one of several conditions is true, use the ElseIf statement:
Grammar
Copy Code code as follows:
if (condition)
Code to was executed if condition is true;
ElseIf (condition)
Code to was executed if condition is true;
Else
Code to was executed if condition is false;
Instance
If the current date is Friday, the following example outputs "Have a nice weekend!" and, if it is Sunday, outputs "Have a nice sunday!", otherwise the output "Have a nice day!" :
Copy Code code as follows:
<body>
<?php
$d =date ("D");
if ($d = = "Fri")
echo "Have a nice weekend!";
ElseIf ($d = = "Sun")
echo "Have a nice sunday!";
Else
echo "Have a nice day!";
?>
</body>