php中for while循环语句学习笔记
本文章来给大家介绍php中基本的循环语句的使用方法,包括for循环,while循环与do while循环的使用方法,本文章很适合于php初学入门者哦。
用for语句可以控制多个变量,从而实现多重循环的高级应用。
下面看一个用for语句输出9*9乘法表的程序:
代码如下 | 复制代码 |
for($i=1;$i
for($j=1;$j
$sum=$i*$j; |
除了前面提到的while循环语句,php还提供了for循环语句实现同样的功能。而且for语句可以实现更为复杂更多功能的循环,任何while循环都可以用for循环来代替。
基本结构形式:
for(表达式1;表达式2;表达式3){
执行语句体
}
其执行过程如下:
1、先执行表达式1;
2、接着判断表达式2的真假,若为假则跳出for循环执行下一个php语句,若为真则进入for循环执行语句体;
3、然后执行表达式3;
4、返回第2步循环运行;
5、直至循环结束跳出for语句。
流程图:
实例:
代码如下 | 复制代码 |
for($i=0;$i
$sum+=$i; |
do…while循环语句是while循环的变体,功能类似于while,只是在其执行循环后再检查表达式是否为真,基本结构为:
do{;
语句体
}while(表达式)
do…while循环语句先执行一次语句体,然后判断表达式的条件,如果值为真则返回再循环一次,为假则跳出循环。
实例:
代码如下 | 复制代码 |
$i=1;
|
while循环语句
while语句是php程序中用来实现循环的语句,其基本结构如下:
while(判断语句){
执行语句体;
}
判断语句一般用关系运算符或者逻辑运算符作为判断条件。
当判断语句为真实则执行语句体,然后再检查表达式的值,如果仍然为真,则执行语句再次被执行。直到判断语句为假时退出循环。
实例:
代码如下 | 复制代码 |
$i=0; |
while和do…while的区别:
两者的主要区别是do…while语句的第一次循环肯定要执行。
若两者循环体执行语句相同,它们的运行结果一般也相同,但在表达式一开始为假时,两种循环的运行结果就有所不同。
实例:
代码如下 | 复制代码 |
/* while循环 */ |
php退出循环的方法有break和continu语句,它们的作用都是当判断条件满足时则跳出循环程序。
break语句用法:
当判断条件的值为真时提前结束整个循环,接着执行循环以外的语句。
实例:
代码如下 | 复制代码 |
/* 输出面积在100以内的圆形面积 */ for($r=1;;$r++){ $A=3.14*$r*$r; if($A>50) break; /* 如果没有break则形成死循环 */ echo $A ." "; } ?> |
continue语句用法:
continue语句的作用是结束本次循环而进入下一次循环,并不是退出整个循环程序。
实例:
代码如下 | 复制代码 |
/* 输出10以内的单数 */ |
break和continue语句的区别:
从上面的例子我们可以看到,break和continue语句在退出循环的作用是有着本质区别的。
continue只是结束本次循环,接着再返回循环体继续执行下次循环;
break则是立即终止整个循环,不再重复执行。

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
