Basic syntax for PHP (iii)

Source: Internet
Author: User

VIII. Conditional 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, and if the condition is false, the other end code is executed

3. If...elseif....else statement

Select one of several segment code blocks to execute

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

Operation Result:$num=3

4. Switch statement

Selectively execute one of several blocks of code

Working principle:

    • Perform a single calculation on an expression (usually a variable)
    • Compare the value of an expression with the value of a case in the structure
    • If there is a match, the code associated with the case is executed
    • After the code executes, the break statement prevents the code from jumping into the next case to continue execution
    • If no case is true, use the default statement
$x=4;switch ($x){case1:  echo"Number 1";  break;case2:  echo"Number 2";  break;default:  echo"No number between 1 and 2";}

Operation Result:No number between 1 and 2

IX. Circular Statement 1, while

Loop code block whenever you specify a condition that is true

2, Do...while

Executes the code block one time and then repeats the loop whenever the condition is specified as True

3. For

Loop code block specified number of times

4. foreach

Iterate through each element in the array and loop through the code block

<?PHP$its = array("Apple","Google","Microsoft","Solidot"); foreach ($its as $it) {  echo "$it <br>";}?>

Operation Result:
Apple
Google
Microsoft
Solidot

X. Function 1, type of function

a function is a block of statements that can be reused in a program.
The function does not execute immediately when the page loads.
Functions are executed only when they are called.

    • Built-in functions: more than 1000 built-in functions
    • Custom functions: Start with a "function" keyword, which can start with a letter or underscore (not a number), is insensitive to case, and should reflect the task performed by the function.
2. Several elements of the function
    • php function Parameters : Parameters are defined after the function name, inside the parentheses. You can add as many arguments as you want by separating them with commas.
    • PHP function default parameter : assigns a value directly after the parameter, and automatically assigns the default value when we call a function that does not contain parameters.
    • PHP function return value : Returns the value using the return statement.
<?phpfunction passwd($account,$password=123456){return$password;}$pLee = passwd("Lee");echo  "$pLee";?>

Operation Result:123456

Copyright NOTICE: This article for Lshare original article, need to reprint please contact me, have questions welcome comments or private messages.

Basic syntax for PHP (iii)

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.