Extra whitespace characters in HTML documents have no effect on the rendered output. If you look at the output page source, you will see that you are getting the correct result. Here are your options:
1) Send a Content-Type: text/plain
header:
header('Content-Type: text/plain');
Make sure to change your <br>
tags to newline characters ("
"
, "
"
, or PHP_EOL
)
2) Wrap what you have there with a <pre>
tag
echo '<pre>';
for($i=1; $i<=5; $i++){
for($j=1; $j<=5; $j++){
if($j>= (6-$i)){
echo "*";
} else {
echo " ";
}
}
echo "<br>";
}
echo '</pre>';
3) Use non-breaking spaces:
for($i=1; $i<=5; $i++){
for($j=1; $j<=5; $j++){
if($j>= (6-$i)){
echo "*";
} else {
echo " ";
}
}
echo "<br>";
}
On another note, here's a one-liner that will do the same thing as what you're trying to do without the additional for
loop and if/else
statements:
for ($i=1; $i<=5; $i++) {
echo str_pad(str_repeat('*', $i), 5, ' ', STR_PAD_LEFT) . PHP_EOL;
}
Try it on 3v4l.org.