PHP break and Continue statements, and GOTO statements and PHP constants

Source: Internet
Author: User

1. Loop control Break statement:

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

Break can accept an optional numeric parameter to decide to jump out of a few loops.

<?php$i = 0;while (+ + $i) {switch ($i) {case 5:echo ' quit at 5 <br> '; break;case 10:echo ' quit at 10<br> '; Break 2 ;  Jumping out of the switch structure and the while loop default:break;//only out of the switch structure}}echo "$i =". $i; <span style= "font-size:18px;" >?></span>

The results of the implementation are as follows:

Quit at 5 quit at 1010=10


2. Loop control Continue statement:

Continue is used in the loop structure to skip the remaining code in this loop and to start the next loop when the condition evaluates to true, continue can accept an optional numeric parameter to decide to skip a few loops to the end of the loop.

<?php for ($i =0; $i <2; $i + +) {for ($j =1; $j <4; $j + +) {if ($j ==2) {continue 2;   Jump out of the nearest 2 cycles of A For loop (j=2 and j=3)}echo ' $i = '. $i. ' $j = '. $j. " <br> ";}} ?>

Output Result:

$i =0$j=1$i=1$j=1

3. Goto statement:

PHP5.3 above version added goto statement;

The goto statement in PHP has a certain limit and can only jump in the same file and scope, meaning you cannot jump out of a function or class method and skip to another function. A common use is to jump out of a loop or switch, instead of a multi-layered break.

<?php for ($i =0, $j =50; $i <100; $i + +) {while ($j--) {if ($j ==7) goto a;//a can be named echo "i= $i <br>";} a://jump to the second echo ' j=17 ';?>

Output Result:

i=0i=0i=0i=0i=0i=0i=0i=0i=0i=0i=0i=0j=37

4. PHP Constants:

Constants are similar to variables, but constants cannot be changed or revoked once defined.

PHP constants and variables have the following differences:

No dollar sign ($) before constant

Constants are defined with the Define () function or const, but not by an assignment statement

Constants can be defined and accessed anywhere, regardless of the scope of the variable (unlike variables, constants are automatically global throughout the script.) )

Constants must be assigned initial values when defined and cannot be redefined or undefined once defined

The value of a constant is a scalar, which can be (string, Integer, Float, Boolean), and so on

Valid constant names begin with a character or underscore, and are indicated by default in uppercase letters, with an underscore interval.


Set PHP constants:

To set a constant, use the Define () function-it uses three parameters:

1. Name of the first parameter definition constant

2. The second parameter defines the value of a constant

3. The optional third parameter specifies whether the constant name is case-sensitive. The default is False.

In the program we do not want a value to change, you can consider using constants, such as pi, tax rate.

<?php//The first definition of constant method: define ("Tax_rate", 0.08); Echo tax_rate. " <br> ";///The second definition of constant method: const TAX_RATE2 = 0.07;echo tax_rate2;? 

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.