Jump to content

(newbie)How can I use a for loop and echo this statement to create this pattern in php?


chickennugt
 Share

Recommended Posts

I'm practicing and this is the pattern I'm trying to create to echo in php:

*

* *

* * *

* * * *

* * * * * 

 

I don't now how to start, i thought it would be easy but im stuck. what do i put for the html and use a while loop? 

 

<html>
<head>
</head>
<body>
"*"
<br>
"**"
<br>
"***"
<br>
"****"
<br>
"*****"
<?php
 
?>
 
</body>
</html>
Link to comment
Share on other sites

As an alternative to what's shown above, here's another option:

 

 

<?php
 
$start = 1;
$end   = 5;
 
// Using str_repeat()
for ($i = $start; $i <= $end; $i++)
{
echo str_repeat('*', $i) . "\n";
}
 
// Going Both Ways
$dir = 1;
 
for ($i = $start; $i > 0 && $i <= $end; $i += $dir)
{
echo str_repeat('*', $i) . "\n";
 
if ($i == $end)
$dir = -1;
}
 
?>

The First Option Output:
*
**
***
****
*****

The Second:
*
**
***
****
*****
****
***
**
*
Edited by Xaotique
Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.