while


php128 apg

Full code fragment that perform REPEATED ACTIONS until a desired result is reached.


Behavior while loops are the simplest type of loops in PHP.

This function works as its analog in C language.



FIRST SYNTAX TYPE


In the following, we will show the FIRST SYNTAX TYPE of -  while  - conditional structure:



<?php

// STRUCTURE #1 

while( expr )
    
statement


where
,

expr =  expression to be evaluated

statement 
code to be executed

?>

SECOND SYNTAX TYPE


In the following, we will show the SECOND SYNTAX TYPE of -  while  - conditional structure:



<?php

// STRUCTURE #2

while ( expr )
{
    
statement
}

where,

expr =  expression to be evaluated

statement 
code to be executed

?>

THIRD SYNTAX TYPE


In the following, we will show the THIRD SYNTAX TYPE of -  while  - conditional structure:



<?php

// STRUCTURE #3

while ( expr ):
     
statement
     
     
. . . . . . . . . .
     
     endwhile;


where,

expr =  expression to be evaluated

statement 
code to be executed
             
?>

  1 EXERCISE   

<?php

$n01 
2;

while (
$n01 <= 9)

echo 
$n01++ . '&nbsp;&nbsp;';

?>

 RESULT   

2 3 4 5 6 7 8 9

  2 EXERCISE   

<?php

$n02 
2;

while (
$n02 <= 9):

echo 
$n02++ . '&nbsp;&nbsp;';

endwhile;

?>

 RESULT   

2 3 4 5 6 7 8 9

  3 EXERCISE   

<?php

$n03 
= [ 2818323218];

$n03i = -1;

$n03e = (count($n03) - 1);

while (
$n03i $n03e):
$n03i++;

echo 
$n03[$n03i] . '&nbsp;&nbsp;';

endwhile;

?>

 RESULT   

2 8 18 32 32 18 8

  4 EXERCISE   

<?php

$n 
0;

$n04 = [ 2818323218];

while (
$n count($n04))
{
       echo 
$n04[$n] . '&nbsp;&nbsp;';
     
$n++;
}    


?>

 RESULT   

2 8 18 32 32 18 8

  5 EXERCISE   

<?php

$nb 
'K';

$ne 'Q';

$n05 = [ 'K' => 2'L' => 8
             
'M' => 18'N' => 32
             
'O' => 32'P' => 18
             
'Q' => ];

while (
$nb <= $ne)
{
       echo 
'\'' $nb '\' => ' $n05[$nb] . '<br>';
     
$nb++;
}    


?>

 RESULT   

'K' => 2,
'L' => 8
'M' => 18
'N' => 32
'O' => 32
'P' => 18
'Q' => 8