PHP Conditional statement (if else/switch) syntax and example

Source: Internet
Author: User
Tags ini readfile

First, If...else statement

If...else statement

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

Grammar:

1. Simple condition

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

Cases

The code is as follows Copy Code

<?php
if (Date ("D") = = "Sat") echo "http://www.111cn.net remind you weekend, carnival go";
?>

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

The code is as follows Copy Code

<?php
if (file_exists ("/usr/local/lib/php3.ini")) {
echo "The following is the PHP3 profile <p><pre>n";
ReadFile ("/usr/local/lib/php3.ini");
echo "</pre>n";
}
?>


2. Complex conditions

ElseIf statement

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

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

Cases

Example: The above example is modified to a more complete processing. Where else is due to only one row of instructions, so no curly braces are added.

The code is as follows Copy Code
<?php
$f = "/usr/local/lib/php3.ini";
if (file_exists ($f)) {
echo "The following is the PHP3 profile <p><pre>n";
ReadFile ($f);
echo "</pre>n";
else echo "I'm sorry, I can't find $f";
?>

--------------------------------------------------------------------------------

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 example below

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

The previous example uses only the two-level 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

Second, switch statement

1 Syntax:

switch (expression) {
Case value 1:
Statement
Break
Case Value 2:
Statement
Break
Default
Statement executed when there is no matching value
}


Working principle:

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

Cases

The code is as follows Copy Code

<?php
Switch ($d =date ("D"))
{
Case "Mon";
echo "Monday";
Break
Case "Tue";
echo "Tuesday";
Break
Case "Wed";
echo "Wednesday";
Break
Case "Thu";
echo "Thursday";
Break
Case "Fir";
echo "Friday";
Break
Case "Sat";
echo "Saturday";
Break
Case "Sun";
echo "Sunday";
Break
}
?>

Another example, using switch to achieve a page multi-purpose, first set up test.php page:

The code is as follows Copy Code

<?php
echo "<a href= ' solution.php?action=add ' > Increase www.111cn.net</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.

The code is as follows Copy Code

<?php
$action =$_get["Action"];

Switch ($action)
{
Case "Add":
echo "Can now achieve the 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 feature! ";
Break
}
?>

Very simply, 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. How much simpler is it than everyone imagined?

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.