php中用for循環制作矩形,PHP中for循環語句的幾種變型

PHP中for循環語句的幾種變型

2021-01-22 10:21:42406

for語句可以說是PHP(同時也是多種語言)的循環控制部份最基本的一個語句了,for語句的執行規律和基礎用法在這里就不多說,可以參見PHP手冊for語句部分。PHP手冊中對它的語法定義如下:

for(expr1;expr2;expr3)

statement

下面說說for語句幾種有用的變型。

1、無限循環

首先是人盡皆知的無限循環(亦可稱“死循環”)。由于空表達式null在語法上是有效的,所以我們可以把for語句的三個表達式留空,這樣就會產生不斷執行for嵌套語句的效果。

for(;;){

//放置需要不斷執行的語句

}

?>

雖然有一些任務會使用到無限循環,但是大多數程序任務,特別是PHP所能涉及的領域,在使用無限循環時都會添加一些終止循環的條件。

for(;;){

//如果是公元2199年,則跳出循環

if(date('Y')=='2199'){

break;

}

}

?>

2、使用空表達式

接下來就是說說在初始化語句expr1中使用null語法,留空expr1最常見的作用就是完成更為復雜的初始化工作。

if(isset($i)){

unset($i);

if((int)date('')<2008){

$i=0;

}else{

$i=1;

}

}else{

$i=3;

}

for(;$i<10;$i++){

echo$i;

}

?>

同樣道理,迭代表達式expr3也可能留空,也可以利用這點編寫更為復雜的迭代式,比如說根據不同的條件調用不同的迭代式。

而for語句中的條件語句expr2留空則是上面所說的無限循環,當然也可以添加一些更為復雜的條件去判斷什么時候跳出循環,在此不在重復。

3、多重循環

使用多重循環來控制多個變量也是在for語句中使經常被忽略的一個特性。如下面的例子,在一般的任務中用到的一般會是雙重循環,三重以上的循環一般意義不大。

for($i=0,$j=10;$i<=10;$i++,$j--){

echo"$i+$j=10\r\n";

}

?>

以上代碼將輸出:

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

4、更為復雜的表達式

如果把for語句的三個表達式寫得復雜一些,則可以用于優化算法。甚至可以使用沒有循環體的for語句來完成一些任務。比如計算累加或階乘:

//計算1-5的累加結果,斌值到$j

for($i=1,$j=0;$i<=5;$j+=$i++);

echo$j;

//計算1-5的階乘結果,斌值到$j

for($i=1,$j=1;$i<=5;$j*=$i++);

echo$j;

?>

PHP借助了C語言的語法,一定程度上也會擁有C的特性,比如說強大的for循環語句就是一個典型的例子。

點擊這里復制本文地址

以上內容由聚米學院網友整理呈現,如對侵犯您的權益,請聯系郵箱:fzsbm@qq.com

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541376.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541376.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541376.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c語言用命令行編譯運行程序_使用C程序執行系統命令

c語言用命令行編譯運行程序Sometimes, we may need to execute Linux/Windows DOS commands through our C program. (Note: the code given below is compiled and executed on Linux GCC compiler, so here we are testing Linux commands only). 有時&#xff0c;我們可能需…

python 熊貓,Python熊貓

我試圖連續分組和計算相同的信息&#xff1a;#Functionsdef postal_saude ():global df, lista_solic#List of solicitantes in Postal Saudelist_sol [lista_solic["name1"], lista_solic["name2"]]#filter Postal Saude Solicitantesdf df[(df[Cliente…

Spring的兩種任務調度Scheduled和Async

Spring提供了兩種后臺任務的方法,分別是: 調度任務&#xff0c;Schedule異步任務&#xff0c;Async當然&#xff0c;使用這兩個是有條件的&#xff0c;需要在spring應用的上下文中聲明<task:annotation-driven/>當然&#xff0c;如果我們是基于java配置的&#xff0c;需要…

建立單鏈表 單鏈表的插入_單鏈列表插入

建立單鏈表 單鏈表的插入All possible cases: 所有可能的情況&#xff1a; Inserting at beginning 開始插入 Inserting at the ending 在末尾插入 Inserting at given position 在給定位置插入 Algorithms: 算法&#xff1a; 1)開始插入 (1) Inserting at the beginning) In…

mysql學習筆記(1-安裝簡介)

mysql的安裝方式&#xff1a;(1)通過系統提供的默認版本(rpm包&#xff0c;穩定版&#xff0c;該版本滿足了使用的需求&#xff0c;建議使用&#xff0c;os vendor)(2)mysql官方提供官方提供的通用rpm安裝包通用二進制格式的程序包(直接下載文件&#xff0c;解壓到指定目錄&…

存儲器間接尋址方式_8086中的數據存儲器尋址模式

存儲器間接尋址方式In this type of addressing mode, first the offset address is calculated, then the memory address is calculated and then the operand form that memory location is fetched. There are following modes which lie under the Data Addressing Mode: …

oracle asm 刪除diskgroup,ASM磁盤組刪除DISK操作

沒想到這么簡單的操作&#xff0c;由于不熟悉還碰到了兩個小問題。[oracledbserver1 ~]$ sqlplus / as sysdbaSQL*Plus: Release 11.2.0.2.0 Production on Tue Aug 9 10:08:062011Copyright (c) 1982, 2010, Oracle.All rights reserved.Connected to:Oracle Database 11g Ent…

intellij idea 最常用的快捷鍵

F2&#xff0c; 可以快速的向下跳走 CtrlF7&#xff0c;可以查詢當前元素在當前文件中的引用&#xff0c;然后按 F3 可以選擇AltQ&#xff0c;可以看到當前方法的聲明CtrlP&#xff0c;可以顯示參數信息CtrlAltV&#xff0c;可以引入變量。例如&#xff1a;new String(); 自動導…

如何在Java中檢查字符串是否為數字?

We will check whether string is a number or not – with the help of logic we will solve this problem, 我們將檢查字符串是否為數字-借助邏輯&#xff0c;我們將解決此問題&#xff0c; In the first step, we will take a string variable named str and store any val…

oracle清理告警日志,Oracle 跟蹤/告警/監聽日志的清理腳本

[root ~]# cat del_oracle_log.sh#!/bin/bashsource /home/oracle/.bash_profilefunction audit_log(){ #---audit_log日志跟蹤文件#audit_log$(strings $ORACLE_HOME/dbs/spfile$ORACLE_SID.ora|grep -i audit_file_dest|awk -F {print $NF}|sed "s///g")audit_lo…

python爬蟲之scrapy框架

Scrapy是一個為了爬取網站數據&#xff0c;提取結構性數據而編寫的應用框架。 其可以應用在數據挖掘&#xff0c;信息處理或存儲歷史數據等一系列的程序中。其最初是為了頁面抓取 (更確切來說, 網絡抓取 )所設計的&#xff0c; 也可以應用在獲取API所返回的數據(例如 Amazon As…

oracle中的事務回滾,ORACLE 死事務的回滾

死事務出現在異常關閉數據庫或者事務進程不正常結束&#xff0c;比如KILL -9&#xff0c;shutdown abort的情況下。當前數據庫里的死事務可以通過查詢內部表x$ktuxe來獲得。select ADDR,KTUXEUSN,KTUXESLT,KTUXESQN,KTUXESIZ from x$ktuxe where KTUXECFLDEAD;ADDR …

大數據數據可視化設計原則_數據可視化設計的8頂帽子

大數據數據可視化設計原則8 hats of data visualization are basically the important persons and their roles that are basically required to carry out data visualization are as follows: 數據可視化有8個基本要素&#xff0c;而進行數據可視化所需的基本角色如下&#…

debian8.8安裝谷歌瀏覽器

第一步&#xff1a;下載&#xff1a; wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb //32位 wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb //64位第二步&#xff1a;安裝dpkg -i google-chrome*.deb…

MB_LEN_MAX常數,帶C ++示例

C MB_LEN_MAX宏常量 (C MB_LEN_MAX macro constant) MB_LEN_MAX constant is a macro constant which is defied in climits header, it is used to get the maximum number of bytes in a multibyte character, for any locale, it returns maximum number of bytes that a m…

php谷歌收錄接口,php實現查詢百度google收錄情況(示例代碼)

PHP$SEOdetail array();$domain !empty($_GET[q]) ? $_GET[q] : www.mycodes.net;baidudetail($domain);googledetail($domain);var_dump($SEOdetail);function baidudetail($domain) {$baidu_site http://www.baidu.com/baidu?wordsite%3A . $domain;$baidu_link http:/…

Linux學習第三步(Centos7安裝mysql5.7數據庫)

版本&#xff1a;mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 前言&#xff1a;在linux下安裝mysql不如windows下面那么簡單&#xff0c;但是也不是很難。本文向大家講解了如何在Centos7下如何安裝mysql5.7版本,如果有什么問題和錯誤的地方&#xff0c;歡迎大家指出。 注釋&…

linux oracle刪除恢復數據恢復,Linux下Oracle誤刪除數據文件恢復操作

檢查數據文件的位置如下&#xff1a;SQL> select name from v$datafile;NAME--------------------------------------------------------------------------------/u01/app/Oracle/oradata/marven/system01.dbf/u01/app/oracle/oradata/marven/undotbs1.dbf/u01/app/oracle/…

數據庫如何處理數據庫太大_網絡數據庫中的數據處理

數據庫如何處理數據庫太大Before learning the data manipulation in a network model, we are discussing data manipulation language, so what is the data manipulation language? 在學習網絡模型中的數據操作之前&#xff0c;我們正在討論數據操作語言&#xff0c;那么什…