Php if else swicth condition control statement learning notes

Source: Internet
Author: User

In php, if else or switch statements are used most frequently, 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: Copy code

<? Php
$ 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: Copy code

<? Php
$ 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: Copy code

<? Php
$ 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: Copy code

<? Php
$ 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: Copy code

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

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.