Loops come in several different flavors in PHP: for, while, do-while, and foreach. We will introduce you to each of them in 3 lessons and we will show you how they can making repetitive tasks straightforward and easy to maintain.
Lesson #1: The for Loop
The syntax of a for loop is:
for (expr1; expr2; expr3)
The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop. In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends. At the end of each iteration, expr3 is evaluated (executed). Let’s consider the example of generating a multiplication table for the number 6. You could calculate and display the values resulting from multiplying 12 by 1, 2, 3 etc. like so:
Maybe extending the code to 6×6 would be quick, a simple matter of copy, paste, and amend. But what if the calculation needed to go up to 30×6? The copy and paste approach is really an inefficient use of your time and can lead to unwieldy code down the road.
A solution is at hand – the for loop.
This example shows how few lines you need to create the multiplication table for any value.
There are three expressions used with a for loop which are separated by semicolons and contained within the parentheses:
The first expression $i = 1 initializes the variable $i to be used as a counter ($i is a commonly used variable name for loop counters). The counter will be used to keep track of how many times the loop has executed.
The second expression $i <= 6 determines whether to stop the loop or proceed. In this case, the loop will execute 6 times. To be more precise the loop will continue while $i has a value less than or equal to 6. Once $i has a higher value, the loop will stop.
The third expression $i++ updates the counter. $i is incremented by 1 each time after the loop as completed a cycle. A cycle of a loop is also known as an iteration.
Now what if you want to process in reverse i.e. multiplying 6 by 6, then 5, then 4, and so on? It’s easy!
Licenciada en Bellas Artes y programadora por pasión. Cuando tengo un rato retoco fotos, edito vídeos y diseño cosas. El resto del tiempo escribo en MA-NO WEB DESIGN END DEVELOPMENT.
We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".
These cookies are necessary for the website to function and cannot be disabled on our systems. They are generally only set in response to your actions in requesting services, such as setting your privacy preferences, logging in or completing forms. You can set your browser to block or alert you to these cookies, but some areas of the site will not work. These cookies do not store any personally identifiable information
These cookies allow us to count visits and traffic sources so that we can assess the performance of our site and improve it. They help us know which pages are the most or least visited, and how visitors navigate the site. All information collected by these cookies is aggregated and therefore anonymous. If you do not allow these cookies to be used, we will not know when you visited our site and will not be able to assess whether it worked properly
These cookies allow the website to provide better functionality and customization. They may be set by our company or by external providers whose services we have added to our pages. If you do not allow these cookies to be used, some of these services may not function properly
These cookies may be set through our site by our advertising partners. They may be used by those companies to profile your interests and display relevant ads on other sites. They do not directly store personal information, but are based on the unique identification of your browser and Internet device. If you do not allow these cookies to be used, you will see less targeted advertising