ການຫາສະເຫນີ while PHP
- ຫນ້າຫນັງກ່ອນ PHP Switch
- ຫນ້າຫນັງ PHP For 循环
PHP while 循环在指定条件为 true 时执行代码块。
PHP 循环
ບໍ່ມີຫຼັງຈາກທີ່ທ່ານຈະຂຽນຄອມພິດລະບົບ ພວກເຮົາຈະຕ້ອງດຳເນີນຄອມພິດລະບົບດຽວກັນຫຼືຫຼາຍຄັ້ງ. ພວກເຮົາສາມາດໃຊ້ລະລືກການດຳເນີນຄອມພິດລະບົບເພື່ອດຳເນີນການດຽວກັນຫຼືຫຼາຍຄັ້ງທີ່ຈະຖືກພິຈາລະນາວ່າເປັນຕາມຈິງ ແທນທີ່ຈະເພີ່ມຄອມພິດລະບົບທີ່ບໍ່ຫຼາຍຫຼືຫຼາຍຄັ້ງໃນຄິດແຜງ
ໃນ PHP ພວກເຮົາມີຄຳສັ່ງລະລືກການດຳເນີນຄອມພິດລະບົບທີ່ຫຼາຍຫຼືຫຼາຍ
- while - ລະລືກການດຳເນີນຄອມພິດລະບົບທີ່ກ່ຽວກັບເງື່ອນໄຂທີ່ຈະຖືກພິຈາລະນາວ່າເປັນຕາມຈິງ
- do...while - ການດຳເນີນຄອມພິດລະບົບຄັ້ງທຳອິດ ແລະຫຼັງຈາກນັ້ນກໍ່ຈະລະລືກການດຳເນີນຄອມພິດລະບົບທີ່ກ່ຽວກັບເງື່ອນໄຂທີ່ຈະຖືກພິຈາລະນາວ່າເປັນຕາມຈິງ
- for - Loop the code block a specified number of times
- foreach - Traverse each element in the array and loop the code block
ການຫາສະເຫນີ while PHP
As long as the specified condition is true, the while loop will execute the code block.
syntax
while (condition is true) { code to be executed; }
Example first sets the variable $x to 1 ($x=1). Then, execute the while loop as long as $x is less than or equal to 5. Each time the loop runs, $x will increment by 1:
ຕົວຢ່າງ
<?php $x=1; while($x<=5) { echo "ຈຳນວນທີ່: $x <br>"; $x++; } ?>
PHP do...while loop
do...while loop will first execute the code block, then check the condition, if the specified condition is true, then repeat the loop.
syntax
do { code to be executed; } while (condition is true);
ຕົວຢ່າງທີ່ຫນ້າຫນັງ $x ທີ່ 1 ($x=1). ການດຳເນີນ do while 循环ຈະສະແດງການສະແດງຕົວຢ່າງ, ແລະ ການກັບຄັນ $x 1. ຫນ້າຫນັງການກວດສອບສັນຍາຫນັງ ($x ຍັງມີຄວາມຍິງຫນັງຫລາຍກວ່າ 5).
ຕົວຢ່າງ
<?php $x=1; do { echo "ຈຳນວນທີ່: $x <br>"; $x++; } while ($x<=5); ?>
ສະແດງຄວາມຫນັງ: do while 循环ຈະກວດສອບສັນຍາຫນັງພຽງແຕ່ຫນັງສົບສວນພາຍໃນການດຳເນີນການຂອງສັນຍາຫນັງ. ຄວາມວ່າ do while 循环ຈະດຳເນີນການສັນຍາຫນັງພຽງແຕ່ຄັ້ງດຽວ, ເຖິງວ່າສັນຍາຫນັງກວດສອບໃນຄັ້ງທຳອິດແມ່ນບໍ່ສຳເລັດ.
ຕົວຢ່າງທີ່ຫນ້າຫນັງ $x ທີ່ 6, ແລະ ການດຳເນີນການສົບສວນ,ການກວດສອບສັນຍາຫນັງ:
ຕົວຢ່າງ
<?php $x=6; do { echo "ຈຳນວນທີ່: $x <br>"; $x++; } while ($x<=5); ?>
ການສະແດງຫນ້າຫນັງຕໍ່ມາຈະສະແດງການສະແດງ for 循环 ແລະ foreach 循环.
- ຫນ້າຫນັງກ່ອນ PHP Switch
- ຫນ້າຫນັງ PHP For 循环