Phpifelseswicth conditional control statement learning NOTE _ PHP Tutorial

Source: Internet
Author: User
Phpifelseswicth learning notes for conditional control statements. In php, the most commonly used conditional control statements are ifelse or switch statements, next, I will introduce to you in detail the notes on using ifelse conditional control statements in php. if you need the most conditional control statements in php, you can use the if else or switch statements, next, I will give you a detailed introduction to my notes on using the if else conditional control statement in php. if you need it, please refer to it.

In php, the basic process control structures include sequence structure, branch structure, and cyclic structure.

Common control statements include:

If, if... Else judgment statement
Switch branch statement
While, do... While loop statement
For loop statement
Break and continue interrupt statements

The php if statement is a simple judgment statement that implements conditional judgment. When a program executes a statement, the program selects to execute the corresponding statement by judging whether the statement value meets the conditions. Therefore, if statements are the most basic php conditional expressions.


The basic structure is:

If (judgment statement)

{

Execution statement body

}

Instance:

The code is as follows:

$ A = 8;
$ B = 4;
If ($ a> $ B) {/* if $ a> $ B, execute the statement in braces */
$ A ++;
}
$ C = $ a + $ B;
Echo "a + B =". $ c;
?>

If statements can select whether to execute the statement body, while if... The else statement is either of the two statements. you must select one of the two statement bodies for execution. It can be interpreted as "how to deal with what happens, otherwise how to solve it", so if... Else is essentially a selective statement.

If the value is not 0 (true), the execution statement body 1, the value is 0 (false), then the execution statement Body 2.

If... The basic structure of the else statement is:

If (judgment statement)

{

Execution statement body 1

}

Else

{

Execution statement Body 2

}

Instance:

The code is as follows:

$ A = 11;
$ B = 9;
If ($ a> $ B ){
$ C = $ a + $ B;
}
Else {
$ C = $ a-$ B;
}
Echo $ c;
?>

If... The else statement can only implement two branches. to implement multiple branches, use multiple if... Nested else statements. The structure is as follows:

If (judgment statement 1 ){

Execution statement body 1

}

Else if (judgment statement 2 ){

Execution statement Body 2

}

Else if...

Else...

Instance:

The code is as follows:

$ Score = 61;
If ($ score> = 90 ){
Echo "excellent performance ";
}
Else if ($ score> = 60 ){
Echo "pass .";
}
Else if ($ score <60 ){
Fail;
}
Else echo "incorrect score ";
?>

Multiple if () statements can be nested in an if statement to determine multiple parameters. it is generally called an if statement with multiple nested statements. its basic structure is as follows:

If (judgement 1)

If (judgement 2) statement body 1

Else statement Body 2

Else

...

Attention should be paid to the pairing relationship between if and else. starting from the inner layer, else is always paired with the nearest if in it, so be especially careful when programming.

Instance:

The code is as follows:

$ Gender = "female ";
$ Age = 28;
If ($ gender = "male "){
If ($ age> = 18 ){
Echo "you are a man ";
}
Else if ($ age <18 ){
Echo "you are a boy ";
}
}
Else {
If ($ age> = 18 ){
Echo "you are a woman ";
}
Else if ($ age <18 ){
Echo "you are a girl ";
}
}
?>


We have introduced if... The else nested structure can implement the multi-branch selection function, but the code of this method is relatively lengthy. Php also provides the switch statement to implement the multi-branch selection function. It becomes more concise to use switch statements.

Basic structure:

Switch (expression ){
Case 1:
Execution statement body 1;
Break;
Case 2:
Execution statement Body 2;
Break;
...
Default:
Execution statement body n;
Break;
}
The expression value first matches the case statement one by one. if the expression matches, the statement body is executed and then jumps out of the loop. If no case statement matches, execute the statement after default.

Instance:

The code is as follows:

$ I = 0;
Switch ($ I ){
Case (0 ):
Echo "you are a girl
";
Break;
Case (1 ):
Echo "you are a boy
";
Break;
Default:
Echo "it's an animal ";
}
?>

Else or switch statement. next I will introduce you in detail to some friends who need to take notes on using the if else conditional control statement in php...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.