conditional Statements
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 statements
Executes a piece of code when the condition is established, and executes another piece of code when the condition is not true
ElseIf statements
Used in conjunction with If...else to execute a block of code if one of several conditions is established ... Else statement
Use the If....else statement if you want to execute some code when a condition is set up and execute another code when the condition is not true.
Grammar
if (condition)
Code to being executed if condition is true;
Else
Code to being executed if condition is false;
Instance
If the current date is Friday, the following code will output "a nice weekend!", otherwise it will output "a nice day!" :
Copy the Code code as follows:
$d =date ("D");
if ($d = = "Fri")
echo "has a nice weekend!";
Else
echo "has a nice day!";
?>
If you need to execute multiple lines of code when the condition is set up or not, you should include these lines of code in curly braces:
Copy the Code code as follows:
$d =date ("D");
if ($d = = "Fri")
{
echo "hello!
";
echo "has a nice weekend!";
echo "See your on monday!";
}
?>
ElseIf statements
If you want to execute code when one of several conditions is true, use the ElseIf statement:
Grammar
Copy the Code code as follows:
if (condition)
Code to being executed if condition is true;
ElseIf (condition)
Code to being executed if condition is true;
Else
Code to being executed if condition is false;
Instance
If the current date is Friday, the following example outputs "a nice weekend!", and if it is Sunday, the output "has a nice sunday!", otherwise the output "has a nice day!" :
Copy the Code code as follows:
$d =date ("D");
if ($d = = "Fri")
echo "has a nice weekend!";
ElseIf ($d = = "Sun")
echo "has a nice sunday!";
Else
echo "has a nice day!";
?>
http://www.bkjia.com/PHPjc/326679.html www.bkjia.com true http://www.bkjia.com/PHPjc/326679.html techarticle conditional statements 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 in Condition ...