Skip to content

Instantly share code, notes, and snippets.

@eugenelee0420
Created September 27, 2016 13:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eugenelee0420/601cebb3f3e4bc462e757f57f946a238 to your computer and use it in GitHub Desktop.
Save eugenelee0420/601cebb3f3e4bc462e757f57f946a238 to your computer and use it in GitHub Desktop.
PHP number diamond with for loops
<?php
echo "<center>";
$x=30;
// Incremental loop for each horizontal line
for($root=0;$root<=$x;$root++) {
// Incremental loop (1 till current $root)
for($inc=1;$inc<$root;$inc++) {
echo $inc;
echo " ";
}
// Decremental loop (current $root down to 1)
for($dec=$root;$dec>=1;$dec--) {
echo $dec;
echo " ";
}
echo "<br>";
}
// Decremental loop for each horizontal line
for($root=$x-1;$root>=1;$root--) {
// Incremental loop (1 till current $root)
for($inc=1;$inc<$root;$inc++) {
echo $inc;
echo " ";
}
// Decremental loop (current $root down to 1)
for($dec=$root;$dec>=1;$dec--) {
echo $dec;
echo " ";
}
echo "<br>";
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment