循环语句与条件语句_在PHP中混合条件语句和循环
循環(huán)語(yǔ)句與條件語(yǔ)句
As mentioned earlier, the looping statement is executing a particular code as long as a condition is true. On the order hand, conditional statements are statements that can only be executed based on the fulfillment of a particular condition(s).
如前所述,只要條件為真, 循環(huán)語(yǔ)句就會(huì)執(zhí)行特定的代碼。 在順序方面, 條件語(yǔ)句是只能基于滿足特定條件才能執(zhí)行的語(yǔ)句。
Example:
例:
We have 3 phones and 3 laptops altogether 6 gadgets. Let's write a basic program that displays "phone" 3 times and "laptop" 3 times assuming the phones are labeled 1 to 3 and the laptops 4 to 6.
我們有3部手機(jī)和3臺(tái)筆記本電腦,共6個(gè)小工具。 讓我們編寫(xiě)一個(gè)基本程序,假設(shè)手機(jī)分別標(biāo)記為1到3以及筆記本電腦4到6,它會(huì)顯示3次“手機(jī)”和3次“筆記本電腦”。
We are going to see how we can use the conditional statements and the loop statements to accomplish this. Since we have had a mastery of the syntaxes form the previous articles we will go straight to their implementation.
我們將看到如何使用條件語(yǔ)句和循環(huán)語(yǔ)句來(lái)完成此任務(wù)。 由于我們已經(jīng)掌握了前幾篇文章中的語(yǔ)法,因此我們將直接介紹它們的實(shí)現(xiàn)。
使用for循環(huán)和if ... else (Using for loop and if...else)
<?php for ($l = 1;$l <= 6;$l++) {if ($l <= 3) {echo "<br>phone";} else {echo "<br>laptop";} } ?>Output
輸出量
phone phone phone laptop laptop laptop使用while循環(huán)以及if ... else (Using while loop and if...else)
<?php $x = 1; while ($x <= 6) {if ($x <= 3) {echo "<br>phone";} else {echo "<br>laptop";}$x++; } ?>Output
輸出量
phone phone phone laptop laptop laptop使用do while循環(huán)和if ... else (Using do while loop and if...else)
<?php $x = 1; do {if ($x <= 3) {echo "<br>phone";} else {echo "<br>laptop";}$x++; } while ($x <= 6); ?>Output
輸出量
phone phone phone laptop laptop laptop翻譯自: https://www.includehelp.com/php/mixing-conditional-statements-and-loops.aspx
循環(huán)語(yǔ)句與條件語(yǔ)句
總結(jié)
以上是生活随笔為你收集整理的循环语句与条件语句_在PHP中混合条件语句和循环的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: javascript对话框_JavaSc
- 下一篇: kotlin 判断数字_Kotlin程序
