Skip to content

Instantly share code, notes, and snippets.

@dailybird
Last active April 1, 2017 14:31
Show Gist options
  • Save dailybird/ccbde0fd1ca188a1d44e731b0e4a4d00 to your computer and use it in GitHub Desktop.
Save dailybird/ccbde0fd1ca188a1d44e731b0e4a4d00 to your computer and use it in GitHub Desktop.
Print a hollow diamond.
<?php
/**
* 打印空心菱形
* @param [int] $level [层数]
* @return [void]
*/
function diamondPrinter($level){
for ($index = 1; $index <= $level; $index++) {
currentLevel($index, $level);
}
for ($index = $level - 1; $index >= 1; $index--) {
currentLevel($index, $level);
}
}
/**
* 打印菱形的单层
* @param [int] $current [当前层数]
* @param [int] $level [总层数]
* @return [void]
*/
function currentLevel($current, $level){
for ($space = $current; $space < $level; $space++) {
echo "<span style='color:#fff;'>*</span>";
}
echo "<span>*</span>";
for ($minSpace = 1; $minSpace <= ($current - 1) * 2 - 1; $minSpace++) {
echo "<span style='color:#fff;'>*</span>";
}
if($current != 1) echo "<span>*</span>";
for ($space = $current; $space < $level; $space++) {
echo "<span style='color:#fff;'>*</span>";
}
echo "<br>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment