Before you introduce the "if" statement, know that the "if" statement is to meet the expression when the execution of a PHP statement, when the condition is not satisfied, is not executed, but, in most cases, always meet the conditions when the execution of a PHP statement, when the condition is not satisfied with the execution of other PHP statements, At this point, we need to use the information we present to you today
"If...else" statementThe
Let's take a look at the syntax format of the "If...else" statement
if (conditional expression) { PHP statement 1;} else{ PHP statement 2;}
The statement is detailed:
The above statement means: When our conditional expression is true, execute PHP statement 1, and if the conditional expression is false, execute PHP statement 2. This satisfies the PHP statement execution when we satisfy the condition of the expression, and the PHP statement executes when the expression condition is not met.
Let's take a look at the Process Control chart for the "If...else" statement
If...else Statement Instance
This example will be based on an example of an if statement, and an example of an if statement will have the result output only if the expression is true, otherwise there will be no output. But when we use the If...else statement, regardless of the full expression condition, the output will result, the code is as follows
<?phpheader ("Content-type:text/html;charset=utf-8"), $num =rand (1,50), if ($num%2==0) { echo "variable $num is even";} else{ echo "variable $num is odd";}? >
Example Explanation:
The rand () function is to get a random integer.
First use the rand () function to generate a number between 1,50, and then the condition to determine whether the number is even, if it is even, the conditional expression is true, then the output "variable $num is even", anyway, the conditional expression is false, the output "variable $num is odd",
The result of the code operation is as follows:
Because it is a randomly generated number, each time you refresh the page, there will be different results. Try it!