---
title: "PHP中break语句的应用"
author: "cakin24"
date: "2016-12-25T06:36:37Z"
tags: ["PHP", "break"]
categories: ["编程语言"]
slug: "php-break"
draft: false
lastmod: "2016-12-25T06:37:53Z"
id: 2347160
url: "https://cakin24.iteye.com/blog/2347160"
---

**一 实例**

```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>使用break关键字跳出多重循环</title>
<style type="text/css">
<!--
body {
	background-color: #CCFF00;
}
-->
</style></head>
<body>
<?php
while(true){
	for(;;){
		for($i=1;$i<=4;$i++){
			echo $i." <img src='images/$i.jpg'>"."\t";
			if($i == 3){
				echo "<p>变量\$i等于3，跳出一重循环。<p>";
				break 1;
			}
		}
		for($j = 1; $j < 5; $j++){
			echo $j." <img src='images/$j.jpg'>"."\t";
			if($j == 4){
				echo "<p>变量\$j等于4，跳出最外重循环。";
				break 3;
			}
		}
	}
	echo "看不到我吧，呵呵!";
}
?> 
</body>
</html>
```

 

**二 运行结果**

![](http://dl2.iteye.com/upload/attachment/0122/1842/c21f7e12-f5d2-3461-91c6-d8c52337a8ad.png)  
 

**三 注意事项**

本实例共3层循环，最外层的while循环和中间的for循环是无限循环，最里面并列两个for循环：程序首先执行第1个循环，循环3次，跳出当前循环（一重循环），然后继续执行第二个for循环，循环4次，然后跳出最外层的循环（体会break后的数字的含义，跳出针对自己的第3层循环）。