PHP Loops

0
2K

In the following chapters you will learn how to repeat code by using loops in PHP.


PHP Loops

Often when you write code, you want the same block of code to run over and over again a certain number of times. So, instead of adding several almost equal code-lines in a script, we can use loops.

Loops are used to execute the same block of code again and again, as long as a certain condition is true.

In PHP, we have the following loop types:

  • while - loops through a block of code as long as the specified condition is true
  • do...while - loops through a block of code once, and then repeats the loop as long as the specified condition is true
  • for - loops through a block of code a specified number of times
  • foreach - loops through a block of code for each element in an array

The following chapters will explain and give examples of each loop type.

Search
Categories
Read More
Other
PHP Shorthand if Statements
Short Hand If To write shorter code, you can write if statements on one line....
By PHP Tutorial 2024-05-17 07:47:03 0 2K
Other
PHP Multidimensional Arrays
In the previous pages, we have described arrays that are a single list of key/value pairs....
By PHP Tutorial 2024-05-17 08:07:20 0 4K
Other
PHP Arrays
An array stores multiple values in one single variable: ExampleGet your own PHP Server $cars...
By PHP Tutorial 2024-05-17 07:53:31 0 3K
Other
PHP - Concatenate Strings
String Concatenation To concatenate, or combine, two strings you can use...
By PHP Tutorial 2024-05-17 07:30:24 0 5K
Other
PHP Multiline Comments
Multi-line Comments Multi-line comments start with /* and end with */. Any text...
By PHP Tutorial 2024-05-17 07:15:48 0 3K