Detailed explanation of 'if...else' statement examples of PHP control statements

怪我咯
Release: 2023-03-07 13:04:01
Original
2762 people have browsed it

I introduced the "if" statement to you before. You know that the "if" statement executes a PHP statement when the expression is satisfied. When the condition is not satisfied, it is not executed. However, in most cases , always execute a PHP statement when the condition is met, and execute other PHP statements when the condition is not met. At this time, you need to use the "if...else" statement we introduce to you today.

Let’s take a look at the syntax format of the "if...else" statement

if(条件表达式){
            PHP语句1;
}else{
            PHP语句2;
}
Copy after login

Detailed explanation of the statement:

Above The meaning of the statement is: when our conditional expression is true, execute PHP statement 1; if the conditional expression is false, execute PHP statement 2. This satisfies the need for PHP statements to be executed when the expression conditions are met, and PHP statements to be executed when the expression conditions are not met.

Let’s take a look at the flow control chart of the “if...else” statement

Detailed explanation of if...else statement examples of PHP control statements


if...else statement example

This example will be based on "if statement example"," The example of if" statement will have a result output only when the expression is true, otherwise, there will be no output result. But when we use the if...else statement, the result will be output regardless of whether the expression condition is met or not. The code is as follows

<?php
header("content-type:text/html;charset=utf-8");
$num=rand(1,50);
if($num%2==0){
    echo "变量$num 是偶数";
}else{
    echo "变量$num 是奇数";
}
?>
Copy after login

Example explanation:

Detailed explanation of if...else statement examples of PHP control statements rand() function The function is to obtain a random integer.

First use the rand() function to generate a number between 1 and 50, and then conditionally determine whether the number is an even number. If it is an even number and the conditional expression is true, then "the variable $num is an even number" is output. , anyway, the conditional expression is false, and "the variable $num is an odd number" is output.

The code running result is as follows:

Detailed explanation of if...else statement examples of PHP control statements

Because it is randomly generated number, so every time you refresh the page, there will be different results. Give it a try!


The above is the detailed content of Detailed explanation of 'if...else' statement examples of PHP control statements. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template