DEV Community

Discussion on: Daily Challenge #2 - String Diamond

Collapse
 
martyhimmel profile image
Martin Himmel

PHP

function make_diamond(int $width) {
    if ($width < 0 || $width % 2 == 0) {
        return null;
    }
    for ($i = 1; $i < $width; $i += 2) {
        echo str_repeat(' ', $width - $i / 2) . str_repeat('*', $i) . PHP_EOL;
    }
    for ($j = $width; $j > 0; $j -= 2) {
        echo str_repeat(' ', $width - $j / 2) . str_repeat('*', $j) . PHP_EOL;
    }
}