Adjusted indentation and code syntax (#34819)
The section titles were in the code examples and it was hard to distinguish the sections from each other.
This commit is contained in:
@ -14,107 +14,105 @@ PHP works with 4 different types of loops:
|
|||||||
|
|
||||||
The `while` loop continues to excecute as long as the specified condition is true.
|
The `while` loop continues to excecute as long as the specified condition is true.
|
||||||
|
|
||||||
`php
|
```php
|
||||||
<?php
|
<?php
|
||||||
while(condition is true)
|
while(condition is true)
|
||||||
{
|
{
|
||||||
execute code;
|
execute code;
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
```
|
||||||
|
Example:
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
$x = 1;
|
||||||
|
while($x <= 3)
|
||||||
|
{
|
||||||
|
echo "x=$x ";
|
||||||
|
$x++;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
Output:
|
||||||
|
```php
|
||||||
|
x=1 x=2 x=3
|
||||||
|
```
|
||||||
|
|
||||||
|
## Do...while loop
|
||||||
|
|
||||||
Example:
|
In the `do...while` loop the block of code is executed before the condition is checked.
|
||||||
```php
|
|
||||||
<?php
|
|
||||||
$x = 1;
|
|
||||||
while($x <= 3)
|
|
||||||
{
|
|
||||||
echo "x=$x ";
|
|
||||||
$x++;
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
```
|
|
||||||
```
|
|
||||||
Output:
|
|
||||||
x=1 x=2 x=3
|
|
||||||
```
|
|
||||||
|
|
||||||
## Do...while loop
|
```php
|
||||||
|
<?php
|
||||||
In the `do...while` loop the block of code is executed before the condition is checked.
|
do {
|
||||||
|
execute code;
|
||||||
```php
|
} while (condition);
|
||||||
<?php
|
?>
|
||||||
do {
|
```
|
||||||
execute code;
|
Example:
|
||||||
} while (condition);
|
```php
|
||||||
?>
|
<?php
|
||||||
```
|
|
||||||
Example:
|
|
||||||
|
|
||||||
```php
|
|
||||||
<?php
|
|
||||||
$x= 1;
|
$x= 1;
|
||||||
do {
|
do {
|
||||||
echo "x=$x ";
|
echo "x=$x ";
|
||||||
$x++;
|
$x++;
|
||||||
} while ($x < 5);
|
} while ($x < 5);
|
||||||
?>
|
?>
|
||||||
```
|
```
|
||||||
```
|
Output:
|
||||||
Output:
|
```php
|
||||||
x=1 x=2 x=3 x=4
|
x=1 x=2 x=3 x=4
|
||||||
```
|
```
|
||||||
|
|
||||||
## For loop
|
## For loop
|
||||||
|
|
||||||
The `for` loop is used when the number of times the block is to be executed is known in advance.
|
The `for` loop is used when the number of times the block is to be executed is known in advance.
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
for (variable initialisation; test condition; increment)
|
for (variable initialisation; test condition; increment)
|
||||||
{
|
{
|
||||||
execute code;
|
execute code;
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
```
|
```
|
||||||
Example:
|
Example:
|
||||||
|
```php
|
||||||
|
<?php
|
||||||
|
for ($x=1 ; $x <= 4 ; $x++)
|
||||||
|
{
|
||||||
|
echo "x= $x ";
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
```
|
||||||
|
Output:
|
||||||
|
```php
|
||||||
|
x=1 x=2 x=3 x=4
|
||||||
|
```
|
||||||
|
|
||||||
```php
|
## Foreach loop
|
||||||
<?php
|
|
||||||
for ($x=1 ; $x <= 4 ; $x++)
|
|
||||||
{
|
|
||||||
echo "x= $x ";
|
|
||||||
}
|
|
||||||
?>
|
|
||||||
```
|
|
||||||
```
|
|
||||||
Output:
|
|
||||||
x=1 x=2 x=3 x=4
|
|
||||||
```
|
|
||||||
|
|
||||||
## Foreach loop
|
The `foreach` loop helps in traversing through arrays.
|
||||||
|
|
||||||
The `foreach` loop helps in traversing through arrays.
|
```php
|
||||||
|
<?php
|
||||||
```php
|
foreach ($array as $value)
|
||||||
<?php
|
{
|
||||||
foreach ($array as $value)
|
executable code;
|
||||||
{
|
}
|
||||||
executable code;
|
?>
|
||||||
}
|
```
|
||||||
?>
|
Example
|
||||||
```
|
```php
|
||||||
Example:
|
<?php
|
||||||
|
$numbers= array("One", "Two", "Three");
|
||||||
```php
|
foreach ($numbers as $value)
|
||||||
<?php
|
{
|
||||||
$numbers= array("One", "Two", "Three");
|
echo "$value ";
|
||||||
foreach ($numbers as $value)
|
}
|
||||||
{
|
?>
|
||||||
echo "$value ";
|
```
|
||||||
}
|
Output:
|
||||||
?>
|
```php
|
||||||
```
|
One Two Three
|
||||||
```
|
```
|
||||||
Output:
|
|
||||||
One Two Three
|
|
||||||
|
Reference in New Issue
Block a user