Code Examples of PHP control structure if,switch,while,for, etc.

Source: Internet
Author: User
Control Structure

1. If condition Judgment statement

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/> <?php $a = 10; if ($a > 0) {echo ' integer greater than 0 ';} echo ' <br/> '; if ($a > 0) {echo ' integer greater than 0 ';} else if ($a < 0) {echo ' integer is less than 0 ';} else {echo ' integer equals zero ';}?>

2. Switch statement

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/> <?php $role = ' admin '; Switch ($role) {case ' admin ': echo ' admin ', break, case ' user ': Echo ' ordinary users '; break, Case ' guest ': echo ' visitor '; break; Defa Ult:echo ' visitors '; Break }?>

3. While loop statement

<?php $a = 10; while ($a > 0) {echo $a-; Echo ' <br> ';}?>


4. Do and loop statements

<?php $a = 10; Do {echo $a--echo ' <br/> ',} while ($a > 0)?>

5. For loop statements

<?php for ($a = 0; $a < $a + +) {echo $a; echo ' <br/> ';}?>

6. Break statement

<meta http-equiv= "Content-type" content= "Text/html;charset=utf-8"/> <?php for ($a = 0; $a < $a + +) {echo $a ; Echo ' <br/> '; if ($a ==5) {break;//terminates the loop, but executes the statement following the loop}} echo ' Loop end ';?>

7. Exit statement

<?php for ($a = 0; $a <, $a + +) {echo $a; echo ' <br/> '; if ($a ==5) {exit;//exit directly, the statement following the loop does not execute}} echo ' Loop end '; ?>

8. Continue statements

<?php for ($a = 0; $a <; $a + +) {echo $a; echo ' <br/> '; if ($a ==5) {Co ntinue;//end this cycle, continue the next loop, the statement behind the loop still executes}} echo ' Loop over ';? 

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.