Home > Backend Development > PHP Tutorial > Detailed explanation of PHP basic conditional judgment statements

Detailed explanation of PHP basic conditional judgment statements

韦小宝
Release: 2023-03-20 18:12:02
Original
5741 people have browsed it

In PHP developmentConditional judgmentstatements are very common. Conditionaljudgment statementsThis is also a very basic thing in PHP. It is widely used in daily development of PHP, so we will It must be firmly grasped! Today let’s talk about basic condition judgment in PHP!

Many PHP programs are composed of a series of statements. A statement can be an assignment statement, a function call, a loop, or even a conditional statement that does nothing (an empty statement). Statements usually end with a semicolon. In addition, you can also use curly braces to encapsulate a group of statements into a statement group. The statement group itself can be thought of as a line of statements.

if
The if structure is one of the most important features of multilingualincluding php, which allows execution based on conditions code segment. PHP's if structure is similar to the C language:

if (expr)
    statement
Copy after login

As defined in the Expressions chapter, expr evaluates to Boolean. If the value of expr is true, php will execute the statement, if the value is false - the statement will be ignored. See the "Converting to Boolean Values" section for more information on which values ​​are considered false.

If $a is greater than $b, the following example will show that a is bigger than b:

<?php
if ($a > $b)
   echo "a is bigger than b";
?>
Copy after login

It is often necessary to execute more than one statement according to conditions. Of course, there is no need to add Previous if clause. These statements can be placed into statement groups. For example, if $a is greater than $b, the following code will display that a is bigger than b and assign the value of $a to $b:

<?php
if ($a > $b) {
   echo "a is bigger than b";
   $b = $a;
}
?>
Copy after login

if statements can be nested within other if statements indefinitely, This provides sufficient flexibility for conditional execution of different parts of the program.

else
It is often necessary to execute a statement when a certain condition is met, and execute other statements when the condition is not met. This is the purpose of else Function. else extends the if statement to execute the statement when the expression in the if statement evaluates to false. For example, the following code displays a is bigger than b when $a is greater than $b, and otherwise displays a is not bigger than b:

<?php
if ($a > $b) {
   echo "a is bigger than b";
} else {
   echo "a is not bigger than b";
}
?>
Copy after login
Copy after login

else statement is only used in if and elseif (if any) statements. Executed when the expression evaluates to false (see elseif).

elseif
elseif, as the name implies, is a combination of if and else. Like else, it extends the if statement and can execute a different statement when the original if expression evaluates to false. But unlike else, it only executes the statement when the conditional expression of elseif evaluates to true. For example, the following code will display a is bigger than b, a equal to b or a is smaller than b respectively according to the conditions:

<?php
if ($a > $b) {
   echo "a is bigger than b";
} elseif ($a == $b) {
   echo "a is equal to b";
} else {
   echo "a is smaller than b";
}
?>
Copy after login

There can be multiple elseif statements in the same if structure. The first elseif statement (if any) whose expression evaluates to true will be executed. In php, it can also be written as "else if" (two words), which behaves exactly like "elseif" (one word). There is a slight difference in the meaning of syntax analysis (it's the same behavior if you're familiar with C), but the bottom line is that both produce exactly the same behavior.

elseif statement is only executed when the previous if or elseif expression value is false and the current elseif expression value is true.

else
It is often necessary to execute a statement when a certain condition is met, and execute other statements when the condition is not met. This is the purpose of else Function. else extends the if statement to execute the statement when the expression in the if statement evaluates to false. For example, the following code displays a is bigger than b when $a is greater than $b, and otherwise displays a is not bigger than b:

<?php
if ($a > $b) {
   echo "a is bigger than b";
} else {
   echo "a is not bigger than b";
}
?>
Copy after login
Copy after login

else statement is only used in if and elseif (if any) statements Executed when the expression evaluates to false (see elseif).

Recommended similar articles:

In PHP, conditional statements mainly use statements such as if else, if elseif and switch case. These two statements are the most used...

php conditional statement if else and switch usage examples

I explained to you a kind of multiple Choose the elseif statement. Although this statement can make a variety of judgments, when used...

Recommended 10 articles on the use of PHP conditional control statement examples

The above is the detailed content of Detailed explanation of PHP basic conditional judgment statements. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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