Skip to content

Instantly share code, notes, and snippets.

@alyjee
Created June 25, 2018 10:45
Show Gist options
  • Save alyjee/0f5b3be0f082e08523256bff1d9e00e4 to your computer and use it in GitHub Desktop.
Save alyjee/0f5b3be0f082e08523256bff1d9e00e4 to your computer and use it in GitHub Desktop.
How to display a complete diamond in PHP
<?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