Returnexitbreakcontiue in php _ PHP Tutorial

Source: Internet
Author: User
Returnexitbreakcontiue in php. Return, break, and contiue are language structures, just like if statements, but exit is a function. 1. function of exit: Outputamessageandterminatethecurrentscript outputs a return, break, and contiue. it is like an if statement, but exit is a function.

1. exit function

Purpose: Output a message and terminate the current script

Output a message and terminate the current script.

If a piece of text contains multiple End script, exit all scripts.

For example, if a php text contains code, it is not output as world.

Echo "hello ";

Exit;

?>

Echo "world ";

?>

Syntax format: void indicates no return value.

Void exit ([string $ status])

Void exit (int $ status)

If status is a string, this function prints the status just before exiting.

If status is a string, this function prints status before the script exits.

If status is an integer, that value will also be used as the exit status. exit statuses shoshould be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. the status 0 is used to terminate the program successfully.

If status is an integer, the integer is used as the exit status. The exit status should be from 0 to 254. exit status 255 is retained and disabled by PHP. Status 0 is used to indicate successful termination of the program.

2. use of the return language structure

Purpose: terminate the execution of a function and return a value from the function.

3. break and continue

Used in the for, foreach, while, do... while or switch structure.

Break ends the execution of the current for, foreach, while, do... while or switch structure.

The break can accept an optional numeric parameter to determine the number of loops to jump out.

Code:

$ Arr = array ('one', 'two', 'Three ', 'Four', 'stop', 'five ');

While (list (, $ val) = each ($ arr )){

If ($ val = 'stop '){

Break;

}

Echo "$ val
\ N ";

}

$ I = 0;

While (++ $ I ){

Switch ($ I ){

Case 5:

Echo "At 5
\ N ";

Break 1;

Case 10:

Echo "At 10; quitting
\ N ";

Break 2;

Default:

Break;

}

}

?>

Continue is used in the loop structure to skip the remaining code in this loop and start to execute the next loop of this loop structure.

Note: In PHP, the switch statement is considered as a loop structure for the purpose of continue.

Continue accepts an optional numeric parameter to decide to skip multiple cycles to the end of the loop.

Code:

While (list ($ key, $ value) = each ($ arr )){

If (! ($ Key % 2) {// skip odd members

Continue;

}

Do_something_odd ($ value );

}

$ I = 0;

While ($ I ++ <5 ){

Echo "Outer
\ N ";

While (1 ){

Echo "Middle
\ N ";

While (1 ){

Echo "Inner
\ N ";

Continue 3;

}

Echo "This never gets output.
\ N ";

}

Echo "Neither does this.
\ N ";

}

?>

Bytes. 1. function of exit: Output a message and terminate the current script to Output...

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.