Hi Everyone,
How are you all?
Friends i m new on daniweb, and i just want to know that how can i make diamond with stars (*) using "For" Loop in PHP.

My Teacher Gives me an assignment to make like this...

for ($a=1; $a<=5; $a++)
{
	
	for($b=1; $b<=5; $b++)
	{
		echo "*";
	
	}
	echo "<br>";
}

up code is showing me this result

*****
*****
*****
*****
*****

i want like this one below...!

*****
****
***
**
*


Any one here to help me out of this...?
i m waiting for your response...!
Thanks
Best Regards!
MK

Recommended Answers

All 15 Replies

Hey mate,
Is this what you are after?
http://npratley.net/a.php

The easiest way i got that was like this

<?PHP
$b = 5;
for ($a=1; $a<=5; $a++)
{
        echo str_repeat('*', $b);
        $b--;
        echo "<br>";
}
?>

Hi Everyone,
How are you all?
Friends i m new on daniweb, and i just want to know that how can i make diamond with stars (*) using "For" Loop in PHP.

My Teacher Gives me an assignment to make like this...

for ($a=1; $a<=5; $a++)
{
	
	for($b=1; $b<=5; $b++)
	{
		echo "*";
	
	}
	echo "<br>";
}

up code is showing me this result

*****
*****
*****
*****
*****

i want like this one below...!

*****
****
***
**
*


Any one here to help me out of this...?
i m waiting for your response...!
Thanks
Best Regards!
MK

Replace inner loop with this..

for($b=1; $b<=6-$a; $b++)

That should do the trick..
cheers!!

venkat0904,
While that is an acceptable way to acomplish it,
It is my belief that str_repeat would be the faster of the two, with the less code of the two. Just a thought incase speed is an issue later, aswell.

venkat0904,
While that is an acceptable way to acomplish it,
It is my belief that str_repeat would be the faster of the two, with the less code of the two. Just a thought incase speed is an issue later, aswell.

nicko170, i agree with you but as u can see from the kind of problem assigned, it is more likely that teacher provided this as an exercise to get familiar with logic building with for loops. Its not a php specific problem. My teachers used to assign these kind of problems in C/C++.. rather much tougher ones...
your solution is much appreciable though...

venkat0904,
Understandable, thankyou for your input. You are probably right with that, either was is an acceptable solution, you'rs being more loop orientated.
Cheers,
Nick

venkat0904,
Understandable, thankyou for your input. You are probably right with that, either was is an acceptable solution, you'rs being more loop orientated.
Cheers,
Nick

sure
though I, personally, would have used ur kind of solution if my teacher provide me with such assignments.. :P (provided he/she is not bothered about source code)
Cheers!!

Fair enough :P
Where as my teachers liked cleaner/efficient code.
That was a pain in the arse when one had to work with 5 - 6 level deep arrays.
Cheers.

Fair enough :P
Where as my teachers liked cleaner/efficient code.
That was a pain in the arse when one had to work with 5 - 6 level deep arrays.
Cheers.

lolzzz..
but i believe code efficiency comes into play after u get ur basics straight...
i still remember my teachers giving me problems like "Do .... without using .... "
pretty annoying at first but realized later that it ws for our own good..
Cheers!!

Check Now Friend...!

Got your PM too, but the question was already nicely answered.

Got your PM too, but the question was already nicely answered.

Looks like every1 is concernd abt d problem except d 1 who posted it.. :D

Thank u all of you...!

Thank You So Much...!

Thank u all of you...!

Thank You So Much...!

`$a=6; $b=0; $c=0; $space = 1;
  $space = $a - 1;
  for ($c = 1; $c <= $a; $c++)
  {
    for ($b = 1; $b <= $space; $b++)
    echo "       ";

     for ($b = 1; $b <= 2*$c-1; $b++)
     printf("<font size='6' color='red'> * </font> ");
     printf("<br>");
       $space=$space-1;
  }
  $space = 1;
  for ($c = 1; $c <= $a - 1; $c++)
  {
    for ($b = 1; $b <= $space; $b++){
     echo "      ";
     }
    $space++;
    for ($b = 1 ; $b <= 2*($a-$c)-1; $b++) {
      printf("<font size='6' color='red'> * </font> ");
   }
     echo "<br>";
  }`
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.