PHP basic syntax (3)

Source: Internet
Author: User
PHP basic syntax (3) 8. condition statement 1. if the specified condition is true, code 2, if... If the else statement is true, the code is executed. if the condition is false, the code at the other end is executed... Elseif .... The else statement selects one of several code blocks to execute $ num3; if ($ num & gt; 3) echo $ num & gt; 1; elseif ($ num3 PHP basic syntax (3)

VIII. condition statement 1. if statement

If the specified condition is true, the code is executed.

2. if... Else statement

If the condition is true, the code is executed. if the condition is false, the code at the other end is executed.

3. if... Elseif .... Else statement

Select one of several code blocks to execute

$num=3;if($num>3)echo "\$num>1";elseif($num==3)echo "\$num=3";elseecho "\$num<3";

Running result:$num=3

4. switch statement

Selectively execute one of several code blocks

Working principle:

  • Perform a calculation on the expression (usually a variable ).
  • Compare the expression value with the case value in the structure
  • If a match exists, execute the code associated with the case
  • After the code is executed, the break statement prevents the code from jumping into the next case for further execution.
  • If no case is true, use the default statement.
$x=4;switch ($x){case 1:  echo "Number 1";  break;case 2:  echo "Number 2";  break;default:  echo "No number between 1 and 2";}

Running result:No number between 1 and 2

9. loop statement 1. while

Loop code block as long as the specified condition is true

2. do... While

Execute the code block once, and then repeat the loop as long as the specified condition is true.

3.

Number of times a circular code block is specified

4. foreach

Traverse each element in the array and loop the code block


  ";}?>

Running result:
Apple
Google
Microsoft
Solidot

10. function 1. function type

A function is a statement block that can be reused in a program.
The function is not executed immediately when the page is loaded.
The function is executed only when it is called.

  • Built-in functions: more than one thousand built-in functions
  • User-defined function: starts with the keyword "function". the function name can start with a letter or underline (rather than a number). it is case-insensitive and should reflect the task executed by the function.
2. several elements of a function
  • PHP function parameters: The parameter is defined in brackets after the function name. You can add any number of parameters separated by commas.
  • Default PHP function parameters: Assign values directly after the parameter. the default value is automatically assigned when a function without parameters is called.
  • PHP function return value: Return value using the return statement.

  

Running result:123456

Copyright Disclaimer: This article is an original Lshare article. if you need to reprint it, please contact me. if you have any questions, please comments or private messages.

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.