for


php128 apg

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

Performs ITERATION of a variable executed in THREE PARAMETERIZED PHASES:


    1 . Assigning an INITIAL VALUE to the variable.

    2 . Establishment of the ITERATIVE CALCULATION that will be submitted to the variable.

    3 . Completion of the ITERATIVE calculation.

Behavior for loops are the most complex 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 -  for  - conditional structure:



<?php

// STRUCTURE #1

for ( expr1; expr2; expr3)
    
statement


where
,

expr1 = first expression to be evaluated once unconditionally 
             at the beginning of the loop
             
expr2 
= second expression, related to expr1

expr3 
= third expression - the end of each iteration

?>

SECOND SYNTAX TYPE


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



<?php 

// STRUCTURE #2 

for ( expr1; expr2; expr3) 
{ 
    
statement 
} 


where, 

expr1 = first expression to be evaluated once unconditionally 
             at the beginning of the loop 
              
expr2 
= second expression, related to expr1 

expr3 
= third expression - the end of each iteration 

?>
 

  1 EXERCISE   

<?php

for ($n01 = 0; $n01 <= 9; $n01++)
echo 
$n01 . '&nbsp;&nbsp;';

?>

 RESULT   

0 1 2 3 4 5 6 7 8 9


  2 EXERCISE   

<?php

for ($n02 = 9; $n02 >= 0; $n02--)
{
echo 
$n02 . '&nbsp;&nbsp;';
}

?> 

 RESULT   

9 8 7 6 5 4 3 2 1 0


  3 EXERCISE   

<?php

$a03 
= array('Sunday', 
                     
'Monday', 'Tuesday', 'Wednesday', 
                     
'Thursday', 'Friday', 
                     
'Saturday');

for (
$n03 = 0; $n03 <= (count($a03) - 1); $n03++)
{
echo 
$a03[$n03] . '<br>';
}

?> 

 RESULT   

Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday