PHP conditional statements If else and switch usage examples are detailed

Source: Internet
Author: User
Tags readfile
In PHP, conditional statements mainly use statements such as if else and if ElseIf and Swicth case, which are used to the most.

One, if...else statements

If...else statements

Executes a piece of code when the condition is established, and executes another piece of code when the condition is not true

Grammar:

1. Simple conditions

if (condition) {        ...    }    else{...    }
<?PHPIF (Date ("D") = = "Sat") echo "reminds you of the weekend, carnival go"; >

Example: The execution section of this example has three rows and cannot omit curly braces.

<?phpif (file_exists ("/usr/local/lib/php3.ini")) {  echo "Below is PHP3 configuration file <p><pre>n";  ReadFile ("/usr/local/lib/php3.ini");  echo "</pre>n";}? >

2. Complex conditions

ElseIf statements

Used in conjunction with If...else to execute a block of code when one of several conditions is established

if (condition) {        ...    }    ElseIf (condition) {        ...    }    else{...        }

Example: The above example is modified to a more complete processing. The else in which there is only one line of instructions, so do not add braces.

<?php$f= "/usr/local/lib/php3.ini"; if (file_exists ($f)) {  echo "Below is PHP3 configuration file <p><pre>n";  ReadFile ($f);  echo "</pre>n";} else echo "Sorry, can't find $f";? >

The third type is recursive if. else loops, often used in a variety of decision-making judgments. It will be a few if. Else to combine the use of processing.

Look directly at the following example

<?PHPIF ($a > $b) {  echo "A is larger than B";} elseif ($a = = $b) {  echo  "a equals B";} else {  echo "A is smaller than B";}? >

The above example uses only two layers of if. else loop, which is used to compare the two variables of a and B. This recursive if is actually to be used. else loop, please be careful to use, because too many layers of the loop is easy to make the logic of the design problems, or less curly braces, etc., will cause the program to appear inexplicable problem.

Second, switch statement

1 Syntax:

switch (expression) {case    value 1:                    statement break                    ;    Case Value 2:                    statement break                    ;    Default:                statement executed when there are no matching values}

Working principle:

1. One calculation of an expression (usually a variable)
2. Compare the value of an expression to the value of a case in the structure
3. If there is a match, execute the code associated with the case
4. After the code executes, the break statement prevents the code from jumping into the next case to continue execution
5. If no case is true, use the default statement

Using switch to achieve a page multi-purpose, first establish the test.php page:

<?phpecho "<a href= ' Solution.php?action=add ' > Add </a><br><br>"; echo "<a href= ' Solution.php?action=del ' > Delete </a><br><br> '; Echo ' <a href= ' Solution.php?action=search ' > Find </a><br><br> "; echo" <a href= ' solution.php?action=update ' > Update </a> ";? >

Next, let's take a look at how solution.php handles these four kinds of operations.

<?php$action=$_get["Action"];switch ($action) {case ' Add ': Echo ' now allows for added functionality! "; Break Case "del": Echo "can now implement the DELETE function!" "; Break Case "Search": echo "can now implement the query function!" "; Break Case "Update": echo "can now implement the update function!" "; break;}? >

The

is simple, we first receive the value of the action, and use the switch statement to give it the appropriate action based on the difference in the action value.

Related Article

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.