While Loops - PHP - P29

Published: 17 May 2020
on channel: Dino Cajic
116
3

An introduction to loops with the while loop in PHP. If you’ve never been introduced to the concept of a loop, think about it this way. Let’s say that you’re looking at an excel sheet. In that excel sheet you have rows that go down the sheet. Each row has some data inside of it. To read the contents of the next row, you move down. You can keep reading the contents of each row up until you reach the last row of that excel sheet.

With a loop, you can read the information from a particular row, add 1 to the current row number, and move to the next row. You can then read the contents of the next row.

while ( expression is true ) {
do something
}

The while loop says, “while the expression is true, do something inside of the loop body.” If we passed the value “true” to the conditional expression, the loop would run forever (well, as long as PHP allows it to run before it crashes). Programming languages like Java do allow you to run the loop forever and it’s actually pretty common to see a while(true) loop; in PHP, the always true loop will run until it times out; at that point an error is returned.

The big takeaway: make sure that your while loop ends eventually.

What kind of expression are we testing? You’ve already done this type of testing with if statements. It’s just a conditional.

Inside of the loop body, you have your statements that execute. You also need to have a way to break the conditional expression. For example, if you are testing while( $x != 10 ), the value of $x will eventually have to change to a value that’s equal to 10 in order for the loop to stop executing.

Read the full article on my website
https://www.dinocajic.com/php-while-l...

Code for this tutorial
https://github.com/dinocajic/php-7-yo...

Full Code
https://github.com/dinocajic/php-7-yo...

PHP Playlist
   • PHP Tutorial  

--
Dino Cajic
Author and Head of IT

Homepage: https://dinocajic.com
GitHub: https://github.com/dinocajic
Medium:   / dinocajic  
Instagram:   / think.dino  
LinkedIn:   / dinocajic  
Twitter:   / dino_cajic  

My Books
An Illustrative Introduction to Algorithms
https://www.amazon.com/dp/1686863268

Laravel Excel: Using Laravel to Import Data
https://amzn.to/4925ylw

Code Along With Me - PHP: From Basic to Advanced PHP Techniques
https://amzn.to/3M6tlGN


On this page of the site you can watch the video online While Loops - PHP - P29 with a duration of hours minute second in good quality, which was uploaded by the user Dino Cajic 17 May 2020, share the link with friends and acquaintances, this video has already been watched 116 times on youtube and it was liked by 3 viewers. Enjoy your viewing!