PHP Process Control

Source: Internet
Author: User

Process control is essential for any programming language.

3 Process controls: Sequential structure, branching structure, and loop structure

    1. Branching structure

4 Branching structures: single-branch, dual-branch, multi-branch and branch-nesting

① Single Branch

if ( an expression )

{

code block

}

② Dual Branch

if ( an expression )

{

code block

}

else{

code block

}

③ Multi-Branch

multiple branches are divided into two:if and elseif, switch and case collocation

The main need to pay attention to is the phenomenon of jumping

Switch statement when writing to pay attention to write a break;

Nesting of ④ branches

Branch statement inside a set of branch statements

If (An expression)

{

If (An expression)

{

Switch ()

{

code block

}

}

}

If and switch and ElseIf can all be nested together

Classroom Exercise: Enter a person's age, determine whether he is retired or not retired , the man retired at 60 years old, the lady is 55 years old retired, if retired, print out how many years retired, if not retired, print out how many years to retire

here first there are two pages,test.php and test2.php

test.php

test2.php

Comprehensive exercise: a simple computer

HTML Part code:

PHP Section Code:

    1. Loop structure

the loop structure is the same as other languages, roughly divided into 3 types: For loop, while loop, do. While loop

For ( initialize ; Conditional Expressions ; increments )

{

Loop body

}

while (conditional expression)

{

Loop body

}

do{

Loop body

}while (conditional expression)

Classroom Exercise 1: Use the for loop to print out the Inverted 99 multiplication table

Classroom Exercise 2: The user enters a number to determine whether it is a palindrome number

Classroom Exercise 3: Hitting the stars

Classroom Exercise 4: Print out s=a+aa+aaa+aaaa ...

Forech an array , you can easily Modify the elements of the arrays by adding & to them before $value. This method assigns a value to a reference instead of copying a value.

Special Process Control Statements

①break

used in switch and loop inside, if it is used in switch inside, the representative jumps out of the current case, if it is used in the loop, the representative jumps out of the loop.

but. PHP Break is more powerful than C, JS break

Break in PHP can specify how many layers you want to jump out of

Below :case6 break not only jumps out of switch, but also jumps out while

②continue

Function: End this cycle and go directly to the next cycle. PHP inside the Continue is also more powerful than C and JS, and break, you can specify which loop to end

Continue accepts an optional numeric parameter to decide to skip a few loops to the end of the loop. The default value is 1, which jumps to the end of the current loop.

for (;;)

{

for (;;)

{

for (;;)

{

Continue 3;

}

}

}

Continue,break can specify to jump out of multilayer

③exit statements

when The PHP script executes to exit () and exits the current script, no matter what structure it is in. You can pass a string of characters to the function as a message prompt.

there is another need to note: after the use of exit () after the script will not get into the

As follows:

Effect: The script behind it is not accessible.

the function has an alias function, called the Die () function, the function is the same, but the name is different

Die ("problem");

Exit the script and give a hint

④goto statements

The goto statement is a newly added feature after PHP5.3. The function is to jump to a location in the program.

The use of Goto needs to be marked with a

K:

。。。。。

。。。。。

。。。。。

Goto K;

Goto statement cannot jump into a function or a loop

As follows:

Effect: because Goto statement cannot jump into function or loop, so direct error

Goto operator can be used to jump to another location in the program. The target location can be marked with a colon for the target name, and the jump instruction is the mark after Goto followed by the target location. There are certain restrictions on goto in PHP , where the target location can only be in the same file and scope, that is, you cannot jump out of a function or class method, or you can skip into another function. You cannot jump into any loop or switch structure. You can jump out of a loop or switch, usually using goto Instead of a multi-layered break.

PHP Process Control

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.