FOR
For is one the most versatile and most widely used looping mechanism across most programming languages.
FOR (initialize counter; evaluate counter; increase counter) {
// code to execute for each iteration
}
Example;
$result = ''
FOR ($x = 0; $x < 10; $x++) {
$result .= html:p('$x is ' . $x)
}
$result
play_arrow RUN open_in_new OPEN IN TAB
Here is how the above code works:
- Initialize counter statement of
$x = 0;
initializes the$x
and sets the starts the loop with value of$x
set to 0. - Evaluate counter statement of
$x < 10;
will check the validation of the expression and as long as the$x
value is less than10
will continue the loop. - Increase counter statement of
$x++;
will increase$x
by1
after each iteration.
Copyright ©2013-2022 SunSed®. All rights reserved.