Created
June 25, 2018 10:45
-
-
Save alyjee/0f5b3be0f082e08523256bff1d9e00e4 to your computer and use it in GitHub Desktop.
How to display a complete diamond in PHP
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 | |
$n = 9; | |
$index = 1; | |
$trend = 'upward'; | |
for ($i=1; $i <= $n; $i++) { | |
$stars = $n - ($n - $index); | |
if($index >= $n){ | |
$trend = 'downward'; | |
} | |
if($trend == 'downward'){ | |
$index = $index - 2; | |
} else { | |
$index = $index + 2; | |
} | |
$spaces = ($n - $stars) / 2; | |
for ($j=1; $j <= $spaces ; $j++) { | |
echo '-'; | |
} | |
for ($j=1; $j <= $stars ; $j++) { | |
echo '*'; | |
} | |
for ($j=1; $j <= $spaces ; $j++) { | |
echo '-'; | |
} | |
echo '<br />'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment