{"id":418,"date":"2025-06-10T20:09:34","date_gmt":"2025-06-10T12:09:34","guid":{"rendered":"https:\/\/www.zhaozhao123.cn\/php\/php-application-manual\/jcyy\/418.html"},"modified":"2025-06-10T20:09:34","modified_gmt":"2025-06-10T12:09:34","slug":"%e6%8e%8c%e6%8f%a1php%e4%b8%ad%e7%9a%84while%e5%be%aa%e7%8e%af","status":"publish","type":"my1js","link":"https:\/\/www.zhaozhao123.cn\/php\/my1js\/418.html","title":{"rendered":"\u638c\u63e1PHP\u4e2d\u7684while\u5faa\u73af"},"content":{"rendered":"<p><body><\/p>\n<h2>&#20171;&#32461;<\/h2>\n<p>&#29702;&#35299;&ldquo;while&rdquo;&#24490;&#29615;&#30340;&#22522;&#26412;&#21407;&#29702;&#23545;&#20110;PHP&#32534;&#31243;&#26469;&#35828;&#38750;&#24120;&#37325;&#35201;&#12290;&#26412;&#25945;&#31243;&#23558;&#20174;&#22522;&#30784;&#24320;&#22987;&#65292;&#36880;&#27493;&#24341;&#23548;&#24744;&#21040;&#39640;&#32423;&#23454;&#29616;&#30340;&#38454;&#27573;&#65292;&#24110;&#21161;&#24744;&#22312;&#20195;&#30721;&#20013;&#32534;&#20889;&#39640;&#25928;&#19988;&#24378;&#22823;&#30340;&#24490;&#29615;&#12290;<\/p>\n<h2>&#24320;&#22987;&#20351;&#29992; &#8216;while&#8217; &#24490;&#29615;&#12290;<\/h2>\n<p>&#22312;PHP&#20013;&#65292;while &#24490;&#29615;&#26159;&#19968;&#31181;&#23398;&#20064;&#36215;&#26469;&#30456;&#23545;&#31616;&#21333;&#30340;&#25511;&#21046;&#32467;&#26500;&#12290;&#19968;&#20010; while &#24490;&#29615;&#20250;&#19968;&#30452;&#25191;&#34892;&#19968;&#32452;&#20195;&#30721;&#22359;&#65292;&#30452;&#21040;&#25351;&#23450;&#30340;&#26465;&#20214;&#20026;&#30495;&#12290;&#20197;&#19979;&#26159;&#22522;&#26412;&#35821;&#27861;&#65306;<\/p>\n<pre><code>&lt;?php\nwhile (condition) {\n    \/\/ code to be executed\n}\n?&gt;<\/code><\/pre>\n<p>&#35753;&#25105;&#20204;&#26469;&#30475;&#19968;&#20010;&#31616;&#21333;&#30340;&#20363;&#23376;&#65306;<\/p>\n<pre><code>&lt;?php\n$counter = 1;\nwhile ($counter &lt;= 5) {\n    echo 'Iteration ' . $counter++ . '&lt;br\/&gt;';\n}\n?&gt;<\/code><\/pre>\n<p>&#36825;&#20010;&#20195;&#30721;&#20250;&#25171;&#21360;&#20986;&#21069;&#20116;&#20010;&#25968;&#23383;&#20197;&#21450;&#25991;&#26412;&ldquo;&#36845;&#20195;&#12290;&rdquo;&#36825;&#26159;&#24744;&#24320;&#22987;&#23398;&#20064;&ldquo;while&rdquo;&#24490;&#29615;&#30340;&#31532;&#19968;&#27493;&#12290;<\/p>\n<h2>&#30830;&#20445;&#23433;&#20840;&#30340;&#24490;&#29615;<\/h2>\n<p>&#22312;&#20351;&#29992;&ldquo;while&rdquo;&#24490;&#29615;&#26102;&#65292;&#30830;&#20445;&#26377;&#19968;&#20010;&#26368;&#32456;&#20250;&#21464;&#20026;&#20551;&#30340;&#26465;&#20214;&#38750;&#24120;&#37325;&#35201;&#65307;&#21542;&#21017;&#21487;&#33021;&#20250;&#38519;&#20837;&#26080;&#38480;&#24490;&#29615;&#12290;&#19968;&#31181;&#24120;&#35265;&#30340;&#20570;&#27861;&#26159;&#22312;&#21069;&#38754;&#30340;&#20363;&#23376;&#20013;&#35774;&#32622;&#35745;&#25968;&#22120;&#12290;&#21478;&#19968;&#31181;&#21019;&#24314;&#23433;&#20840;&ldquo;while&rdquo;&#24490;&#29615;&#30340;&#26041;&#27861;&#22914;&#19979;&#65306;<\/p>\n<pre><code>&lt;?php\n$status = true;\n$attempts = 0;\n\nwhile ($status &amp;&amp; $attempts &lt; 10) {\n    \/\/ Perform some action\n    \/\/ ...\n\n    if (some_condition_met()) {\n        $status = false;\n    }\n    $attempts++;\n}\n?&gt;<\/code><\/pre>\n<p>&#36825;&#20010;&#24490;&#29615;&#21253;&#21547;&#19968;&#20010;&#38468;&#21152;&#30340;&#26465;&#20214;&#65292;&#35813;&#26465;&#20214;&#38480;&#21046;&#23581;&#35797;&#27425;&#25968;&#20197;&#38450;&#27490;&#28508;&#22312;&#30340;&#26080;&#38480;&#24490;&#29615;&#12290;<\/p>\n<h2>&#19982;&#25968;&#32452;&#24037;&#20316;<\/h2>\n<p>While loops are often used in conjunction with arrays to process each element. Let&#8217;s consider an example where we have an array of products, and we wish to display each one:<\/p>\n<pre><code>&lt;?php\n$products = ['Apple', 'Banana', 'Cherry'];\n$index = 0;\n\nwhile ($index &lt; count($products)) {\n    echo $products[$index] . '&lt;br\/&gt;';\n    $index++;\n}\n?&gt;<\/code><\/pre>\n<p>&#36825;&#23558;&#36941;&#21382;&#25968;&#32452;&#65292;&#30452;&#21040;&#19981;&#20877;&#26377;&#20135;&#21697;&#38656;&#35201;&#26174;&#31034;&#20026;&#27490;&#12290;<\/p>\n<h2>&#32467;&#21512;&ldquo;while&rdquo;&#24490;&#29615;&#19982;&#20989;&#25968;&#20351;&#29992;<\/h2>\n<p>&#24341;&#20837;&#20989;&#25968;&#21040;&ldquo;while&rdquo;&#24490;&#29615;&#20013;&#21487;&#20197;&#32534;&#20889;&#26356;&#24378;&#22823;&#21644;&#32452;&#32455;&#33391;&#22909;&#30340;&#20195;&#30721;&#12290;&#20197;&#19979;&#26159;&#19968;&#20010;&#20363;&#23376;&#65292;&#25105;&#20204;&#22312;&#25968;&#32452;&#20013;&#22788;&#29702;&#27599;&#20010;&#20803;&#32032;&#26102;&#20351;&#29992;&#19968;&#20010;&#20989;&#25968;&#65306;<\/p>\n<pre><code>&lt;?php\nfunction processItem($item) {\n    \/\/ Process the item\n    echo 'Processing ' . $item . '&lt;br\/&gt;';\n}\n\n$items = ['Item1', 'Item2', 'Item3'];\n$index = 0;\nwhile ($index &lt; count($items)) {\n    processItem($items[$index]);\n    $index++;\n}\n?&gt;<\/code><\/pre>\n<p>&#24490;&#29615;&#35843;&#29992;processItem()&#26041;&#27861;&#22788;&#29702;&#27599;&#20010;&#20803;&#32032;&#65292;&#20445;&#25345;&#20102;&#24490;&#29615;&#32467;&#26500;&#30340;&#25972;&#27905;&#21644;&#20195;&#30721;&#27169;&#22359;&#21270;&#12290;<\/p>\n<h2>&#39640;&#32423;&#24490;&#29615;&#25511;&#21046;<\/h2>\n<p>PHP &#25552;&#20379;&#20102;&#22312;&#24490;&#29615;&#20869;&#20462;&#25913;&#27969;&#31243;&#30340;&#26041;&#27861;&#12290;&#20363;&#22914;&#65292;&#20320;&#21487;&#20197;&#20351;&#29992; &#8216;break&#8217; &#35821;&#21477;&#31435;&#21363;&#36864;&#20986;&#24490;&#29615;&#65292;&#25110;&#32773;&#20351;&#29992; &#8216;continue&#8217; &#35821;&#21477;&#36339;&#36807;&#24403;&#21069;&#36845;&#20195;&#24182;&#32487;&#32493;&#21040;&#19979;&#19968;&#27425;&#36845;&#20195;&#12290;&#20197;&#19979;&#26159;&#22914;&#20309;&#22312; &#8216;while&#8217; &#24490;&#29615;&#20013;&#20351;&#29992;&#23427;&#20204;&#65306;<\/p>\n<pre><code>&lt;?php\n$counter = 1;\nwhile ($counter &lt;= 10) {\n    if ($counter == 5) {\n        echo 'Skipping number 5&lt;br\/&gt;';\n        $counter++;\n        continue;\n    }\n    if ($counter == 8) {\n        echo 'Stopping at number 8&lt;br\/&gt;';\n        break;\n    }\n    echo 'Number ' . $counter++ . '&lt;br\/&gt;';\n}\n?&gt;<\/code><\/pre>\n<p>&#36825;&#20010;&#24490;&#29615;&#20250;&#36339;&#36807;&#25968;&#23383;5&#65292;&#20572;&#27490;&#22312;&#25968;&#23383;8&#19978;&#12290;&#36825;&#20123;&#25511;&#21046;&#32467;&#26500;&#24110;&#21161;&#24744;&#31934;&#30830;&#31649;&#29702;&#24490;&#29615;&#30340;&#25191;&#34892;&#36923;&#36753;&#12290;<\/p>\n<h2>&#24102;&#26377;&#25968;&#25454;&#24211;&#30340;&#24490;&#29615;&#25805;&#20316;<\/h2>\n<p>&#8220;&#24403;&#20174;&#25968;&#25454;&#24211;&#26816;&#32034;&#25968;&#25454;&#26102;&#65292;&#8217;while&#8217;&#24490;&#29615;&#20063;&#21487;&#20197;&#38750;&#24120;&#26377;&#29992;&#12290;&#20320;&#21487;&#20197;&#20351;&#29992;&#8217;while&#8217;&#24490;&#29615;&#26469;&#20174;&#32467;&#26524;&#38598;&#20013;&#33719;&#21462;&#34892;&#65292;&#30452;&#21040;&#19981;&#20877;&#26377;&#26356;&#22810;&#34892;&#21487;&#29992;&#12290;&#36825;&#37324;&#26377;&#19968;&#20010;&#20351;&#29992;MySQLi&#30340;&#31034;&#20363;&#65306;&#8221;<\/p>\n<pre><code>&lt;?php\n$mysqli = new mysqli('hostname', 'username', 'password', 'database');\n\nif ($mysqli-&gt;connect_error) {\n    die('Connection failed: ' . $mysqli-&gt;connect_error);\n}\n\n$sql = 'SELECT name FROM users';\n$result = $mysqli-&gt;query($sql);\n\nwhile($row = $result-&gt;fetch_assoc()) {\n    echo $row['name'] . '&lt;br\/&gt;';\n}\n\n$mysqli-&gt;close();\n?&gt;<\/code><\/pre>\n<p>&#35813;&#31034;&#20363;&#20174;&ldquo;users&rdquo;&#34920;&#30340;&#27599;&#19968;&#34892;&#20013;&#26816;&#32034;&#24182;&#36820;&#22238;&ldquo;name&rdquo;&#21015;&#30340;&#25968;&#25454;&#12290;<\/p>\n<h2>&#32467;&#35770;&#12290;<\/h2>\n<p>while &#24490;&#29615;&#26159;PHP&#20013;&#30340;&#24378;&#22823;&#21151;&#33021;&#20043;&#19968;&#65292;&#23427;&#20204;&#22312;&#20174;&#31616;&#21333;&#30340;&#36845;&#20195;&#21040;&#26356;&#22797;&#26434;&#30340;&#25805;&#20316;&#27969;&#31243;&#26041;&#38754;&#37117;&#26377;&#29992;&#27494;&#20043;&#22320;&#12290;&#35831;&#30830;&#20445;&#24744;&#30340;&#24490;&#29615;&#26377;&#26126;&#30830;&#30340;&#12289;&#26377;&#38480;&#30340;&#32467;&#26463;&#26465;&#20214;&#20197;&#38450;&#27490;&#26080;&#38480;&#24490;&#29615;&#12290;&#24841;&#24555;&#32534;&#30721;&#65281;<\/p>\n<p><\/body><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#20171;&#32461; &#29702;&#35299;&ldquo;while&rdquo;&#24490;&#29615;&#30340;&#22522;&#26412;&#21407;&#29702;&#23545;&#20110;PHP&#32534;&#31243;&#26469;&#35828;&#038;..<\/p>\n","protected":false},"author":1,"featured_media":0,"menu_order":0,"template":"","meta":{"_acf_changed":false},"tags":[],"my1js2nav":[38],"tuisongtax":[],"class_list":["post-418","my1js","type-my1js","status-publish","hentry","my1js2nav-jcyy"],"acf":{"qian_art_seotitle":"","qian_art_seotitle_source":{"label":"SEO\u6807\u9898","type":"text","formatted_value":""},"qian_art_seokws":"","qian_art_seokws_source":{"label":"SEO\u5173\u952e\u8bcd","type":"text","formatted_value":""},"qian_art_stzhong":"","qian_art_stzhong_source":{"label":"\u4e2d | \u77ed\u6807\u9898","type":"text","formatted_value":""}},"_links":{"self":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js\/418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js"}],"about":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/types\/my1js"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/users\/1"}],"wp:attachment":[{"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/media?parent=418"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tags?post=418"},{"taxonomy":"my1js2nav","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/my1js2nav?post=418"},{"taxonomy":"tuisongtax","embeddable":true,"href":"https:\/\/www.zhaozhao123.cn\/php\/wp-json\/wp\/v2\/tuisongtax?post=418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}