Created
September 27, 2016 13:01
-
-
Save eugenelee0420/601cebb3f3e4bc462e757f57f946a238 to your computer and use it in GitHub Desktop.
PHP number diamond with for loops
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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