How can I create a pyramid from using php? - php

I need to create a pyramid using asterisks. I specify a value which becomes the base of the pyramid. The base contains as much asterisks as the value specified and the pyramid must skip its rows by 1..Here I am facing a problem when I specify an even number of base..
The pyramid must looke like the one below.
*
***
*****
*******
*********
**********
I am getting
####*
###***
##*****
###*****
####*****
**********
I want to replace the # by some blank space and I am getting the bug that the number of asterisks in the 4th row has decreased.. How do I fix these two bugs ?
function create_pyramid($limit){
if ($limit > 0){
for ($row =0;$row<=$limit;$row++){
if (($row % 2 == 0) && ($row != $limit)){ continue;}
$rows = "";
for ($col =0;$col<$row;$col++){
$rows= $rows.'*';
}
$pattern = "%'#".((($limit - $row)/2)+$row)."s\n";
printf ($pattern,$rows);
print '<br />';
}
}
else{
print "Invalid data";
}
}
create_pyramid(10);

I prefer mine :
echo '<pre>';
$n = 5;
function print_tree($n, $str, $max) {
for ($i = 0; ($i < (($max - $n) / 2)); $i++) {
echo " ";
}
for ($i = 0; ($i < $n); $i++) {
echo $str;
}
echo "<br/>";
}
for ($flag = 0; ($flag < 2); $flag++) {
for ($a = 1, $b = 1, $c = 1, $d = 4; (($d - 3) <= $n); $a += 2, $b++) {
if ($flag == 1) {
print_tree($a, "*", $max);
}
if ($b == $d) {
if ($flag == 0) {
$max = $a;
}
if (($d - 3) != $n) {
$a -= ((2 * $c) + 2);
}
$b = 0;
$d++;
if (($d % 2) == 0) {
$c++;
}
}
}
}
if ((($foot = $n) % 2) == 0) {
$foot++;
}
for ($i = 0; ($i < $foot); $i++) {
print_tree($foot, "|", $max);
}
outputs :
*
***
*****
*******
*****
*******
*********
***********
*************
***********
*************
***************
*****************
*******************
*********************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
*************************
***************************
*****************************
*******************************
*********************************
***********************************
*************************************
***************************************
|||||
|||||
|||||
|||||
|||||
Or even this one:
<?php
$n = 8;
ob_start();
$stars = ($n - 1) * 2 + 1;
$spaces = 0;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo ' ';
echo str_repeat(' ', $spaces * 2);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}
$stars = ($n - 1) * 2 + 1;
$spaces = 0;
$margin = $stars / 2 + 1;
for ($i = 0; ($i < $n); $i++) {
echo str_repeat(' ', $margin);
echo str_repeat(' ', $spaces);
echo str_repeat('*', $stars);
echo "\n";
$spaces += 1;
$stars -= 2;
}
echo trim(implode("\n", array_reverse(explode("\n", ob_get_clean()))), "\n"), "\n";
it gives:
*
***
*****
*******
*********
***********
*************
***************
* *
*** ***
***** *****
******* *******
********* *********
*********** ***********
************* *************
*************** ***************
funny exercices isn't it... 8-)

You can try
create_pyramid("*", 5);
create_pyramid("#", 10);
create_pyramid("^_^", 10);
function create_pyramid($string, $level) {
echo "<pre>";
$level = $level * 2;
for($i = 1; $i <= $level; $i ++) {
if (!($i % 2) && $i != 1)
continue;
print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH);
print PHP_EOL;
}
}
Output A
*
***
*****
*******
*********
Output B
#
###
#####
#######
#########
###########
#############
###############
#################
###################
Output C
^_^^_^^_^
^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^

Just make it simpler:
function create_pyramid($limit) {
for($row = 1; $row < $limit; $row ++) {
$stars = str_repeat('*', ($row - 1) * 2 + 1);
$space = str_repeat(' ', $limit - $row);
echo $space . $stars . '<br/>';
}
}
echo "<pre>" ;
create_pyramid(10);

<?php
$n=9;
for($i=0; $i<=$n; $i++)
{
for($j=1; $j<=$i; $j++)
echo " ";
for($k=1; $k<=$n-$i; $k++)
echo $k;
for($j=($k-2); $j>0; $j--)
echo $j;
for($k=1; $k<=$i; $k++)
echo " ";
echo "</br>";
}
?>

function create_row($num, $limit) {
$append = '';
if($limit > $num && ($limit - $i) % 2 == 0) {
$append .= '-';
}
$stars = str_repeat('*', $num);
$ap_len = floor(($limit - $num) / 2);
$prepend = str_repeat('-', $ap_len);
$append .= str_repeat('-', $ap_len);
return $prepend . $stars . $append;
}
function create_pyramid($limit){
if ($limit > 0){
$no_last = false;
for($i = 1; $i <= $limit; $i += 2) {
print create_row($i, $limit) . PHP_EOL;
if($i == $limit) {
$no_last = true;
}
}
if(!$no_last) {
print create_row($limit, $limit) . PHP_EOL;
}
}
}
create_pyramid(10);

From what I understand, what you are looking for is an Odd numbered pyramid, i.e. the number of * in each row is as per odd number series, like 1,3,5,7. If you wish to include "if-else" statements then you can go with the above answered loops, but if you only wish to use "for" loops, then you can use the following code:
<?php
$x=1;
for($i=1;$i<7;$i++)
{
for($j=7;$j>$i;$j--)
{
echo ' ';
}
for($k=1;$k<=$x;$k++)
{
echo '*';
}
$x=$x+2;
echo "<br/>";
}
?>

I think that the simplest solution is to create 2 loops with condition:
$n = 5; // specify how many rows you want to
$stars = 0;
for ($i = $n; $i > 0; $i--) {
for ($j = 0; $j < $i + $stars; $j++) {
if ($j < $i - 1) {
echo " ";
} else {
echo "*";
}
}
$stars += 2;
echo "\n";
}
The output will be:
*
***
*****
*******
*********

<?php
$l=5;
for ($i=1; $i <=5; $i++) {
for ($k=1; $k < $l ; $k++) {
echo ' ';
}
for ($j=0; $j<$i; $j++) {
echo '*';
}
$l--;
echo "<br>";
}
?>

function create_piramide ($filas) {
$n = $filas;
echo '<pre>';
for ($i = $filas; $i >= 0; $i--) {
echo str_repeat(' ', $i).str_repeat('o ', $n - $i)."\n";
}
echo '</pre>';
}
create_piramide (10);

function pyramid($height)
{
for ($i = 1; $i <= $height; $i++) {
echo str_repeat(" ", $height - $i);
echo str_repeat('*', $i * 2 - 1) . '<br/>';
}
}
pyramid(5);

Diamond Shape
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
* * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * *
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
<?php
$num=15;
$num2=0;
$half = $num;
for($i=$num2; $i<=$num; $i++)
{
for($j=$half; $j>$i; $j--)
{
echo " ";
}
for($j=$num2; $j<=$i; $j++)
{
echo " * ";
}
echo "<br>";
}
for($i=$num2; $i<=$num; $i++)
{
for($j=$num2; $j<=$i; $j++)
{
echo " ";
}
for($j=$half; $j>$i; $j--)
{
echo " * ";
}
echo "<br> ";
}
?>

$n = 5;
for($i = $n; $i >= 1; $i--){
for($j = 1; $j <= $i; $j++){
if($j < $i){
echo '0';//you can replace 0 with space
}else{
$num = $n - $i;
for($k = 0; $k <= $num; $k++){
echo '*';
}
for($y = 1; $y <= $n - 1; $y++){
if($y <= $num){
echo '*';
}else{
echo '0';//you can replace 0 with space
if($y == $n - 1){
echo '<br/>';
}
}
}
}
}
}

$n1=10;
$k=$n1/2;
$n=$n1/2;
for($i=0 ; $i<=$n1 ; $i++) {
if($i <= $n1/2) {
$k=$k+1;
$n=$n-1;
} else {
$k=$k-1;
$n=$n+1;
}
for($j=0 ; $j<=$n1 ; $j++) {
if($j < $k && $j > $n) {
echo "*";
} else {
echo " &nbsp";
}
}
echo "<br/>";
}

<?php
ini_set('display_errors', 1);
/**
* Here is my solution for printing the pyramid using a class
*/
class pyramid
{
function __construct($rows)
{
for ($k=1; $k <= $rows ; $k++) {
$this->space($rows,$k);
$this->left($k);
$this->right($k);
echo "<br>";
}
}
public function left($k)
{
for ($i=1; $i < $k ; $i++) {
echo $i;
}
}
public function right($i)
{
for ($j=$i; $j >= 1 ; $j--) {
echo $j;
}
}
public function space($rows,$k)
{
for ($i=$rows-$k; $i > 0 ; $i--) {
echo " ";
}
}
}
$pyramid = new pyramid(5);
?>
<style type="text/css">
body{
font-family: monospace;
font-size: 24px;
}
</style>

Related

anyone knows how to print this pattern in php

Expected output :
my current code only output the pattern but it can't have more comb:
$n = 5;
$n1 = 4;
for ($i = 0; $i <= $n; $i++) {
for ($j = 0; $j <= $n1; $j++) {
if (($i == 0 || $i == 5) && ($j > 0 and $j < 3)) {
if (($i == 0 && $j == 1) || ($i == $n && $j != 2)) {
printf(" ");
}
printf("$i");
} elseif (($j == 0 || $j == 3) && ($i != 0 and $i != 5)) {
if ($j == 0 && $i != 1 && $i != 4) {
printf("");
} elseif (($j == 3 && $i != 1 && $i != 4)) {
printf("  ");
} elseif ($j == 0) {
printf(" ");
}
printf("$i");
} else {
printf(" ");
}
}
echo "<be>";
`
this is the output the only problem is how do i multiply the comb.
Just for fun I wrote it in JS, but probably pretty straightforward to convert to PHP (String.repeat => str_repeat, [].push => array_push, [].slice => array_slice, etc)
const arr = [];
const output = (x=1, y=1) => {
for (let k = 0; k < 5; k++) {
arr.push([' '.repeat(5-k), k, ' '.repeat(k), k, ' '.repeat(5-k)].join(''))
}
for (let k = 5; k <= 10; k++) {
arr.push([' '.repeat(k-5), 10-k, ' '.repeat(10-k), 10-k, ' '.repeat(k-5)].join(''))
}
arr.forEach((row, index) => {
arr[index] = row + row.substring(1).repeat(x - 1);
});
const orig = [...arr.slice(1)]
for (let i = 1; i < y; i++) {
arr.push(...orig);
}
console.log(arr.join('\n'))
}
output(2,3)
The simplest way is to create an array, where 1 = '*' and 0 = ' '.
This is the algorithm in PHP 7 that I have done to create the pattern.
Note: I have made changes to optimize the code
<?php
$max = 6;
// Arg
$arg1 = 2;
$arg2 = 2;
// Change to pair size when is non
if ($max % 2 != 0) {
++$max;
}
// Matrix with zero values
$t = \array_fill(0, $max * $arg1 - $arg1 + 1, \array_fill(0, $max * $arg2 - $arg2 + 1, 0));
// Lower: start left position
$lower = $max / 2 - 1;
// Upper: start right postion
$upper = $lower + 1;
// Set pattern in matrix
for ($i = 0, $n = $max - 1; $i <= $n; $i++, $lower--, $upper++) {
for ($a1 = 0; $a1 < $arg1; $a1++) {
for ($a2 = 0; $a2 < $arg2; $a2++) {
$y = $i + $max * $a1 - $a1;
$p = $max * $a2 - $a2;
// Set pixel pattern
$t[$y][$lower + $p] = $t[$y][$upper + $p] = 1;
}
}
// Restart lower?
if ($lower == 0) {
$lower = $max;
}
// Restart upper?
if ($upper == $n) {
$upper = -1;
}
}
echo '<pre>', \PHP_EOL;
foreach ($t as $r) {
foreach ($r as $v) {
if ($v) {
echo '*';
} else {
echo ' ';
}
}
echo \PHP_EOL;
}
echo '</pre>';
?>
Output:
** **
* * * *
* * *
* * *
* * * *
** **
* * * *
* * *
* * *
* * * *
** **

How to do nested loop in descending order?

What I want to get with for loop. Something will be like this.
*
**
***
****
*****
****
***
**
*
This thing I want to create right now I have this code.
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
if($i == $end){
for($c=1; $c <= $end; $c++ ){
for($d=i; $d >= 2; $d--){
echo $star;
}
echo "<br>";
}
}
};
?>
And it's working fine with
*
**
***
****
*****
But then I need opposite loop first 4 then 3 then 2 then 1...
Where am I going wrong with this?
Just for fun. Try this:
$stars = 1;
for ($i = 0; $i < 9; $i++) {
for ($s = 1; $s <= $stars; $s++) {
echo "*";
}
echo "\n";
$stars += ($i<4)?1:-1;
}
And, for even more fun, one with just one for loop:
$stars = 0;
$starctr = 0;
for ($i = 0; $i < 25; $i++) {
echo "*";
if ($stars == $starctr) {
echo "\n";
$stars += ($i<14)?1:-1;
$starctr = 0;
} else {
$starctr++;
}
}
Can using nested for loop. Example:
$n = 5;
for($i = 1; $i <= $n; $i++){
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
}
for($i = $n-1; $i >= 1; $i--){
for($j = $i; $j >= 1; $j--){
echo '*';
}
echo '<br />';
}
Another technique using array_fill(), array_map(), array_reverse()
$n = 5; $arr = array();
for($i = 1; $i <= $n; $i++){
$arr[] = array_fill(0, $i, '*');
}
array_map(function($v){echo join('', $v) . '</br>';},$arr);
unset($arr[count($arr) - 1]); //remove last index value
array_map(function($v){echo join('', $v) . '</br>';},array_reverse($arr));
<?php
for($i=0;$i<=6;$i++){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
for($i=5;$i>=1;$i--){
for($k=6;$k>=$i;$k--){
echo " ";
}
for($j=1;$j<=$i;$j++){
echo "* ";
}
echo "<br>";
}
?>
This can be achieved with only 2 for loops.
$offset = 1;
for ($i = 1; $i > 0; $i += $offset)
{
for($j = 1; $j <= $i; $j++){
echo '*';
}
echo '<br />';
if ($i === 5)
$offset = -1;
}
<?php
$i = 1;
$end = 5;
$star = "*";
for($i; $i <= $end; $i++){
for($b=1; $b <= $i; $b++){
echo $star;
}
};
for($c=$send; $c>=2; $c-- ){
for($d=$end; $d >= 2; $d--){
echo $star;
}
echo "<br>";
};
?>
Using a single for loop:
$end = 5;
$out = array();
for ($i=0;$i<$end;++$i)
{
$out[] = str_repeat('*', $i+1);
}
echo implode(PHP_EOL, $out).PHP_EOL;
array_pop($out);
$out = array_reverse($out);//remove last ****** <-- only one line with 5 stars
echo implode(PHP_EOL, $out).PHP_EOL;
Replace PHP_EOL with <br> if you want to, but this is the least loopy way to write this code I can think of
live demo
Here's a recursive attempt:
function ladder($n, $counter=1, &$acc=array())
{
if ($counter == ($n*2)) {
return implode($acc);
}
if ($counter <= $n) {
$acc[$counter] = str_repeat('*', $counter) . "\n";
$counter++;
}
if ($counter > $n) {
$diff = (int) $n-$counter;
$acc[$counter] = str_repeat('*', $n+$diff) . "\n";
$counter++;
}
return ladder($n, $counter, $acc);
}
print_r(ladder(5));

print number vertically and grouping it

I am trying to print number vertically and it must be in group
here is my code
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
for ($j = $i; $j <= 24; $j = $j + $rows) {
$count++;
if($count>$nums){
break;
}
echo "<div class='fleft'>$count</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
out of above
but i want output like for the first column
and next group number will start from where first group number end. in this case next group start from 25
please ask if any doubt
$nums = 105;
$rows = 8;
$colsize = 3;
$col = floor($nums / $rows);
$group = floor($col / $colsize);
$count = 0;
$groupsize = $rows * $colsize;
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
$modulo = 0;
$correction = 0;
$rest = $nums - $count;
if ($rest < $groupsize) {
$empty = $groupsize - $rest;
$correction = floor($empty / $colsize);
$modulo = $empty % $colsize;
}
for ($i = 1; $i <= $rows; $i++) {
$colind = 0;
for ($j = $i; $j <= $groupsize; $j = $j + $rows) {
$count++;
if ($count > $nums) {
break;
}
$val = $j + ($g * $groupsize);
$val -= $colind * $correction;
$modcor = $colind - ($colsize - $modulo);
if ( $modcor > 0 ) {
$val -= $modcor;
}
echo "<div class='fleft'>" . $val . "</div>";
$colind++;
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
This works:
Also, you can change number of digits, columns or size of column
for($group = 0; $group < 3; $group++){
for($row =1 ; $row <= 8; $row++){
for($col = 0; $col <= 2; $col++){
echo ($group*24)+ $row + 8 * $col; echo " ";
}
echo "\n";
}
}
This code will print the number in the requested format. You need to modify according to your need.
may be i am mad , made a simple alter .... try this
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$letCounter=0; //added a counter
for ($g = 0; $g <= $group; $g++) {
echo "<div class='group'>";
for ($i = 1; $i <= $rows; $i++) {
$letCounter=0; //reset counter on each loop
for ($j = $i; $j <= 24; $j = $j + $rows)
{
$count++;
if($count>$nums)
{break;}
//made an alter in the below line , some math :)
echo "<div class='fleft'>".($letCounter++ * $rows +$i)."</div>";
}
echo "<div class='clear'></div>";
}
echo "</div>";
}
Thanks !
This May work
$nums = 105;
$rows = 8;
$col = floor($nums / $rows);
$group = floor($col / 3);
$count = 0;
$flag = true;
for($c=1;$c<=$col;$c++)
{
if($c%$group== 1)
{
echo "Group Start";
$flag = false;
}
for ($i = 1; $i <= $rows; $i++) {
$count++;
echo "<div class='fleft'>$count</div>";
echo "<div class='clear'></div>";
}
echo "Line End";
if($c%$group == 2&& $flag)// Check here for your requirement
echo "Group End </br>";
$flag = true;
}

Print pyramid pattern

1
1 2 1
1 2 3 2 1
1 2 1
1
$newline = "\r\n";
$prnt = '*';
$nos = 3;
for($i = 1; $i <= 2; $i++)
{
for($s = $nos; $s >= 1; $s--)
{
echo " ";echo " ";echo " ";
}
for($j = 1; $j <= $i; $j++)
{
echo $j;echo " ";
}
$m = 2;
for($k = 1; $k <= ($i - 1); $k++)
{
echo $k;echo " ";
}
echo '<br />';
$nos--;
}
$nos = 1;
for($i = 3; $i >= 1; $i--)
{
for ($s = $nos; $s >= 1; $s--) {
echo " ";echo " ";echo " ";
}
for($j = 1; $j <= $i; $j++)
{
echo $j;echo " ";
}
for($k = 1; $k <= ($i - 1); $k++)
{
echo $k;echo " ";
}
$nos++;
echo '<br />';//printf("\n");
}
i got output
1
1 2 1
1 2 3 1 2
1 2 1
1
am not able to print space when i use echo ' '; so i used echo " "; but i dont want to use echo " "; plz resolve this issue.
I am having problem creating above program but somewhere missing a value may be need apply some condition over there . Please see my code.
Printing multiple spaces in straight HTML isn't allowed because of how it must be rendered.
If you want to print exactly - whitespaces included - just wrap your code with a <pre/> tag.
<pre>Spacing will be literal.</pre>
You could also format with the white-space CSS property by setting it to pre.
.spaces {
white-space: pre;
}
<div class="spaces">Spacing will be literal.</div>
This is a fun little algorithm. Here's a recursive solution in PHP. I wrap it in <PRE> tag so that I can use spaces and new lines "\n".
<pre>
<?php
function printPyramid($height) {
// initialize
$size = ($height * 2) - 1;
$half = $size / 2;
$arr = Array();
for($r = 0; $r < $size; $r++) {
$arr[] = array();
for($c = 0; $c < $size; $c++) {
$arr[$r][] = "";
}
}
$arr[$half][$half] = $height;
// recursively build, pass array as reference "&"
pyramidRec($arr, $half, $half, $size);
// print
for($r = 0; $r < $size; $r++) {
for($c = 0; $c < $size; $c++) {
if(empty($arr[$r][$c]))
echo " ";
else if(strlen($arr[$r][$c]) == 1)
echo "{$arr[$r][$c]} ";
else
echo $arr[$r][$c];
}
echo "\n";
}
}
function pyramidRec(&$arr, $r, $c, $size) {
$val = $arr[$r][$c];
$newVal = $val - 1;
if($newVal == 0)
return;
// up
if($r - 1 >= 0 && empty($arr[$r-1][$c])) {
$arr[$r-1][$c] = $newVal;
pyramidRec($arr, $r-1, $c, $size);
}
// down
if($r + 1 < $size && empty($arr[$r+1][$c])) {
$arr[$r+1][$c] = $newVal;
pyramidRec($arr, $r+1, $c, $size);
}
// left
if($c - 1 >= 0 && empty($arr[$r][$c-1])) {
$arr[$r][$c-1] = $newVal;
pyramidRec($arr, $r, $c-1, $size);
}
// right
if($c + 1 < $size && empty($arr[$r][$c+1])) {
$arr[$r][$c+1] = $newVal;
pyramidRec($arr, $r, $c+1, $size);
}
}
printPyramid(5);
?>
</pre>

Pyramid of asterisks in php [duplicate]

This question already has answers here:
How can I create a pyramid from using php?
(14 answers)
Closed 8 years ago.
I am having problem creating a pyramid of asterisk.
Please see my code.
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
Result:
*
**
***
****
*****
My question is how I am going to make that like.
*
* *
* * *
* * * *
* * * * *
<pre><?php
$n = $i = 5;
while ($i--)
echo str_repeat(' ', $i).str_repeat('* ', $n - $i)."\n";
?></pre>
use the same program within <center> </center> tag ! like:
<center>
<?php
for($i=1;$i<=5;$i++){
for($j=1;$j<=$i;$j++){
echo "*";
}
echo "<br />";
}
?>
</center>
Use HTML whitespace character to procude the whitespaces:
So something like this:
<?php
// pyramid height
$height = 5;
for($i=1;$i<=$height;$i++){
for($t = 1;$t <= $height-$i;$t++)
{
echo " ";
}
for($j=1;$j<=$i;$j++)
{
// use here to procude space after each asterix
echo "* ";
}
echo "<br />";
}
?>
try this
$height = 5;
$space = $height;
for($i = 1; $i <= $height; $i++) {
echo str_repeat(' ', --$space);
for($j=1;$j<=$i;$j++){
if($j > 1) {
echo ' ';
}
echo '*';
}
echo '<br />';
}
create_pyramid("*", 5);
function create_pyramid($string, $level) {
echo "<pre>";
$level = $level * 2;
for($i = 1; $i <= $level; $i ++) {
if (!($i % 2) && $i != 1)
continue;
print str_pad(str_repeat($string, $i),($level - 1) * strlen($string), " " , STR_PAD_BOTH);
print PHP_EOL;
}
}
From link posted above by Baba
$n = 5;
$i = 0;
for($i=1; $i<=$n; $i++){
echo "<pre>";
echo str_repeat(" ", $n-$i);
echo str_repeat("# ", $i);
}

Categories