php for循环语句的几种用法分析
for 循环的语法是:
代码如下 复制代码
for (expr1; expr2; expr3)
statement
下面说说for语句几种有用的变型。
1、无限循环
这种也叫死循环,没有开始和结束一直就这样下去
for (;;) {
//放置需要不断执行的语句
}
?>
死循环如果在当中配合if else ,break也可以跳出循环
代码如下 复制代码
for (;;) {
//如果是公元2199年,则跳出循环 http://www.hzhuti.com/nokia/n93/
if (date('Y') == '2199') {
break;
}
}
?>
2、使用空表达式
接下来就是说说在初始化语句expr1中使用null语法,留空expr1最常见的作用就是完成更为复杂的初始化工作。
代码如下 复制代码
if (isset($i)) {
unset($i);
if ((int) date('')
$i = 0;
} else {
$i = 1;
}
} else {
$i =3;
}
for (;$i
echo $i;
}
?>
同样道理,迭代表达式expr3也可能留空,也可以利用这点编写更为复杂的迭代式,比如说根据不同的条件调用不同的迭代式。
而for语句中的条件语句expr2留空则是上面所说的无限循环,当然也可以添加一些更为复杂的条件去判断什么时候跳出循环,在此不在重复。
3、多重循环
使用多重循环来控制多个变量也是在for语句中使经常被忽略的一个特性。如下面的例子,在一般的任务中用到的一般会是双重循环,三重以上的循环一般意义不大。
for ($i = 0, $j = 10;$i
echo "$i + $j = 10rn";
}
?>
以上代码将输出:
0 + 10 = 10
1 + 9 = 10
2 + 8 = 10
3 + 7 = 10
4 + 6 = 10
5 + 5 = 10
6 + 4 = 10
7 + 3 = 10
8 + 2 = 10
9 + 1 = 10
10 + 0 = 10
如果我们要半路跳出循环怎么操作,实例
看下面这个多重循环嵌套的例子:
for($i = 1;$i
for($j = 1;$j
$m = $i * $i + $j * $j;
echo”$m n
”;
if($m 190) {
break 2;
}
}
}
break 2跳出了两重循环,你可以试验一眼,将2去掉,得到的结果是完全不一样的。如果不使用参数,跳出的只是本次循环,第一层循环会继续执行下去。
注:
break是被用在上面所提的各种循环和switch语句中的。他的作用是跳出当前的语法结构,执行下面的语句。break语句可以带一个参数n,表示跳出循环的层数,如果要跳出多重循环的话,可以用n来表示跳出的层数,如果不带参数默认是跳出本重循环。
4、更为复杂的表达式
如果把for语句的三个表达式写得复杂一些,则可以用于优化算法。甚至可以使用没有循环体的for语句来完成一些任务。比如计算累加或阶乘:
//计算1-5的累加结果,斌值到$j
for ($i = 1,$j = 0; $i
echo $j;
//计算1-5的阶乘结果,斌值到$j
for ($i = 1,$j = 1; $i
echo $j;
?>
如果我想执行到一个地方,自动调出当前循环执行一下-实例
for($i = 1;$i
if($i % 3 == 0 || $i % 7 == 0){
continue;
}
}else{
echo”$i n
”;
}
}
?>
PHP的代码片段的作用是输出100以内,既不能被7整除又不能被3整除的那些自然数,循环中先用if条件语句判断那些能被整除的数,然后执行continue;语句,就直接进入了下个循环。不会执行下面的输出语句了。
注:
continue 是用来用在循环结构中,控制程序放弃本次循环continue语句之后的代码并转而进行下一次循环。continue本身并不跳出循环结构,只是放弃这一次循环。如果在非循环结构中(例如if语句中,switch语句中)使用continue,程序将会出错。
摘自 php开发

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
