PHP if Else SWICTH conditional control Statement Learning Notes _php Tutorial

Source: Internet
Author: User
In PHP, the conditional control statement is the most used if else or switch statement, let me give you a detailed introduction I am in the study of PHP using if Else condition control statement notes need to refer to friends.

In the PHP language, the basic process Control structure is: Sequential structure, branch structure, loop structure.

The most commonly used control statements are:

If, If...else judgment statement
Switch Branch statement
While, Do...while loop statements
For Loop statement
Break, continue interrupt statement

The PHP if statement is a simple judgment statement, which is the ability to make conditional judgments. When a program executes a sentence, it encounters a two-fork intersection and chooses to execute the corresponding statement by judging whether the value of the statement satisfies the condition. So the IF statement is the most basic type of PHP conditional expression.


The basic structure is:

if (Judgment statement)

{

Execute Statement Body

}

Instance:

The code is as follows Copy Code

$a = 8;
$b = 4;
if ($a > $b) {/* if $a> $b, execute statement inside curly braces */
$a + +;
}
$c = $a + $b;
echo "a+b=". $c;
?>

The IF statement can choose whether to execute the statement body, while the If...else statement is two, and must be selected in the two statement body to execute. It can be interpreted as "what to do if something happens, or how to fix it", so if...else is essentially a selective statement.

If the value is not 0 (that is, true), the statement body 1 is executed, and the value is 0 (that is, false), then the statement body 2 is executed.

The basic structure of the IF...ELSE statement is:

if (Judgment statement)

{

Executing statement body 1

}

Else

{

Executing Statement body 2

}

Instance:

The code is as follows Copy Code

$a = 11;
$b = 9;
if ($a > $b) {
$c = $a + $b;
}
else{
$c = $a-$b;
}
Echo $c;
?>

If...else statement can only implement two branches, to implement a multi-branch with multiple if...else statements nested. It has the following structural form:

if (Judgment statement 1) {

Executing statement body 1

}

else if (Judgment statement 2) {

Executing Statement body 2

}

else If ...

else ...

Instance:

The code is as follows Copy Code

$score = 61;
if ($score >=90) {
echo "Excellent performance";
}
else if ($score >=60) {
echo "Pass.";
}
else if ($score <60) {
Fail to pass;
}
else echo "score is wrong";
?>

In the IF statement can be nested multiple if () statements, to achieve the judgment of more than one parameter, commonly referred to as the IF statement a variety of nesting, the basic structure of the following form:

if (judgment 1)

if (Judge 2) statement Body 1

Else Statement Body 2

Else

...

You should be aware of the if and else pairing relationships, starting with the inner layer, and else always pairing with the nearest if, and be especially careful when programming.

Instance:

The code is as follows Copy Code

$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";
}
}
?>


The IF...ELSE nested structure can be used to achieve multi-branch selection, but this method is verbose. The ability to implement multi-branch selection, PHP also provides a switch statement. The procedure becomes more concise with the switch statement.

Basic structure:

switch (expression) {
Case 1:
Execution statement body 1;
Break
Case 2:
Execution statement body 2;
Break
...
Default
Executes the statement body N;
Break
}
The value of the expression is first matched to the case statement, if it matches the body of the statement, and then jumps out of the loop. If there is no case statement match, then the execution statement after default is executed.

Instance:

The code is as follows Copy Code

$i = 0;
Switch ($i) {
Case (0):
echo "You are a girl
";
Break
Case (1):
echo "You is a boy"
";
Break
Default
echo "It s an animal";
}
?>

http://www.bkjia.com/PHPjc/628841.html www.bkjia.com true http://www.bkjia.com/PHPjc/628841.html techarticle in PHP, the conditional control statement is the most used if else or switch statement, let me give you a detailed introduction I am learning PHP using if Else condition control statement notes need friends ...

  • 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.