PHP Getting Started Tutorial-if else conditional Statement tutorial

Source: Internet
Author: User

/*
if (expr) {statement}

Example: This example omits curly braces.

The code is as follows Copy Code
<?php
if ($state ==1) echo "haha";
?>

To determine whether equality is = = instead of =,asp programmers can often make this error, = is the assignment.
Example: The execution section of this example has three lines and cannot omit curly braces.

The code is as follows Copy Code
<?php
if ($state ==1) {
echo "haha;
echo "<br>";
}
?>

The second is in addition to if, plus the condition of else, can be explained as "if something happened how to deal with, otherwise how to solve". The syntax is as follows:
if (expr) {statement1} else {Statement2}
Example: The above example is modified to a more complete processing. Where else is due to only one line of instructions, so do not add curly braces

The code is as follows Copy Code
<?php
if ($state ==1) {
echo "haha";
echo "<br>";
}
else{
echo "hehe";
echo "<br>";
}
?>


The third type is recursive if. else loops, usually used in a variety of decision judgments. It will be several if.. Else take to combine the use of processing.
Look directly at the following example:

The code is as follows Copy Code
<?php
if ($a > $b) {
echo "A greater than B";
} elseif ($a = = $b) {
echo "a equals B";
} else {
echo "A is smaller than B";
}
?>

If.. else loop, to compare A and b two variables. You actually want to use this recursive if. else loop, please use caution, because too many layers of the loop can make the logic of the design problem, or less curly braces and so on, will cause the program to appear inexplicable problems
*/

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.