Skip to content

Instantly share code, notes, and snippets.

@LGitHub-sprout
Last active March 10, 2021 21:19
Show Gist options
  • Save LGitHub-sprout/1aee5308c3cd2caf42cf004935a4462f to your computer and use it in GitHub Desktop.
Save LGitHub-sprout/1aee5308c3cd2caf42cf004935a4462f to your computer and use it in GitHub Desktop.
while, do .. while, for Loops
<?php
/**
* O'Reilly p.87
* while vs. for loop:
* use while when condition DOES NOT depend on a simple, regular change to a var.
* use while: when checking for SPECIAL INPUT OR ERROR and end loop when it occurs.
*/
$count2 = 0;
while ( $count2++ <= 12) {
echo "<p>$count2 times 12 is " . $count2 * 12 . "</p>";
}
// do .. while loop
$count3 = 0;
do {
echo "$count3 times 12 is " . $count3 * 12 . "<br />";
}
while ( ++$count3 <= 12 );
/**
* o'reilly p. 86
* for loop:
* use for single values THAT CHANGE ON REG BASIS, such as a list of user choices to process.
*
* more complex operations
* (remember to distinguish btwn commas and semi-colons)
* for ( $i = 0, $j = 0; $i + $j <= 10; $i++, $j++ )
*
* for ( initialization; terminating condition; modification )
*/
for ( $count = 0; $count <= 12; ++$count ) {
echo $count times 12 is " . $count * 12 . "<br />";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment