Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand else in php

Shulou Source: shulou.com Published: 2022-06-03 10:38:13 11月10日 Update

How to understand else in php, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

In php, else means "other", and the else statement is a clause of the if statement, which can be executed when the value of the expression in the if statement is FALSE; the else statement must be used with the if statement and cannot exist alone. Syntax "if (judgment condition) {statement block 1;} else {statement block 2;}".

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

If statements can only perform operations on cases where the result is true, which is not enough in many cases, so there is a statement in the form of if...else. Different from the if judgment, the if...else statement performs operations not only on cases where the result is true, but also on cases that are not true.

The else statement extends the if statement, which can be executed when the value of the expression in the if statement is FALSE. It is also important to note that the else statement is a clause of the if statement and must be used with the if statement and cannot exist alone.

The syntax format of the if...else statement is as follows:

If (judgment condition) {statement block 1;} else {statement block 2;}

In the above format, if the judgment condition is true, statement Block 1 is executed; otherwise, statement Block 2 is executed. both statement Block 1 and statement Block 2 can contain multiple statements. Like the if statement, if both statement Block 1 and statement Block 2 contain only one statement, you can omit the curly braces {}, as shown below:

If (judgment condition) statement block 1 control else statement block 2

The execution flow of the if...else statement is shown in the following figure:

Example: use the rand () function to generate a random number $num, and then determine whether the random number is even or odd, the code is as follows:

The running results are as follows:

$num = 27, is odd!

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Tags: Statement situation condition result clause odd format expression syntax random number help different clear same not enough code even content function multiple Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Huawei OPPO Reno MySQL macOS