PHP conditional Statements BASIC syntax Structure _php tutorial

Source: Internet
Author: User
The conditional statements in PHP are the if else and swicth and trinocular operators, so let's introduce some of the syntax for these conditional statements.

Basic syntax structure of an if single branch

if (conditional expression) {

Execute the statement;

}

If dual Branch

BASIC syntax structure

if (conditional expression) {

Execute the statement;

}else{

Execute the statement;

}

If multiple branches

The basic syntax structure is

if (conditional expression) {

Execute the statement;

}else if (conditional expression) {

Execute the statement;

}else if (conditional expression) {

Execute the statement;

}else{//there may be more else if
Execute the statement;
}

Please note the above structure:

1) Else if you can have one, or you can have multiple

2) Else may not

Cases

The code is as follows Copy Code



if ($account = = "Lord" && $password = = "Pass") {
echo "Welcome $_post[account", your password is $_post[password] ";
}elseif ($_post[account] = = "God" && $_post[password] = = "Dog") {
$y = Date ("Y")-1911;
$m = Date ("M");
$d = Date ("D");
echo "Welcome $_post[account", today is the Republic ". $y." Year ". $m." Month ". $d." Day ";
}else{
echo "Login failed: Input data:
Account number: $_post[account]
Password: $_post[password] ";
}
?>


Switch Branch statement

BASIC syntax structure

switch (expression) {


CASE constant 1:

Execute the statement;
Break

Case constant 2:

Execute the statement;
Break

Defual:

Execute the statement;
Break

}

Cases

The following two examples implement the same thing in different ways, the first one is with the If...elseif...else statement and one using the switch statement.

The code is as follows Copy Code
else {
echo "I am not equal to 0, 1 or 2";
}
?>

The above PHP code means: If the variable $i equals 0, then the output "I equals 0", if the variable $i equals 1, the output "I equals 1", if the variable $i equals 2, the output "I equals 2", if not, then output "I is n OT equal to 0, 1 or 2 ".


Comparison of switch statements and ElseIf statements
In a switch statement, the condition is only one time, and then compared to each case, and in the ElseIf statement, the condition is evaluated again. If your condition is more complex, or multiple loops, the switch statement will be faster.

The code is as follows Copy Code

Isempty= "";

$isEmpty = "non-null";

$test = Empty ($isEmpty)? ": Trim ($isEmpty);

The trinocular operator?: Empty ($isEmpty) True or false, based on the preceding conditions, evaluates to TRUE when evaluated: The following expression, False, evaluates to.

If you are not accustomed to it can be changed to the following code

The code is as follows Copy Code

if (empty ($isEmpty))//condition established

{
$cookie = "";
}
Else

{

$cookie = Trim ($isEmpty);

}


Please note:

1) Case statement has one to many
2) Defaul statement can not (according to the business logic of your own code)
3) Typically, after a case statement, break is taken to indicate exit of the switch statement
4) Type of constant (int, float, string, Boolean)


Add

The comparison operator.

$a = = $b equals TRUE If the $a equals $b.
$a! = $b unequal to TRUE if $a is not equal to $b.
$a <> $b Not equal to TRUE if $a is not equivalent to $b.
$a < $b small with TRUE if the $a is strictly less than $b.
$a > $b is greater than TRUE if $a strictly $b.
$a <= $b is less than or equal to TRUE if $a is less than or equal to $b.
$a >= $b is greater than or equal to TRUE if $a is greater than or equal to $b.

The logical operator.
$a and $b and (logical AND) true if both $a and $b are true.
$a or $b or (logical OR) true if either $a or $b is true.
$a XOR $b xor (Logical XOR) True if $a or $b any one is true, but not both.
! $a not (logical not) True if $a is not true.
$a && $b and (logical AND) true if $a and $b are true.
$a | | $b or (logical OR) true if either $a or $b is true.

http://www.bkjia.com/PHPjc/632630.html www.bkjia.com true http://www.bkjia.com/PHPjc/632630.html techarticle The conditional statements in PHP are the if else and swicth and trinocular operators, so let's introduce some of the syntax for these conditional statements. The basic syntax structure if single branch if (conditional expression) {...

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