Create Pyramid Using asterisks in PHP

Use 2 ‘for loops’ with conditions and a ‘while loop’ like the one below. You must specify the pyramid’s height using a variable like $height = 5; first.

$height = 5;
for( $i=$height; $i>=1; $i-- ){
	for( $j=1; $j<=$height; $j++ ){
		if( $j >= $i ){
			echo '*';
			if( $j == $height ){
				$diff = $j - $i;
			}
		}else{
			echo '&nbsp;&nbsp;';
		}
	}
	$k = 0;
	while( $k < $diff ){
		echo '*';
		$k++;
	}
	echo '<br/>';
}

OUTPUT:

Pyramid using asterisks PHP

Leave a Reply

Your email address will not be published. Required fields are marked *