This what I made.It become
1
121
12321
1234321
But I hope I can get
1234321
12321
121
1
I have tried so many times, but I can't make this. Anyone can help??
Do I make some mistake?
<?php
$p=4;
for($x=1;$x<=$p;$x++)
{
for($q=$p;$q>$x;$q--)
{
echo "<center>";
}
for($k=1;$k<$x;$k++)
{
echo $k;
}
if($x>=1)
{
for($v=$x; $v>=1; $v--)
{
echo $v;
}
}
echo "<br>";
}
?>
I would appreciate it, if you can tell me my mistake. Thank You
You approach is correct, just minor update, Reverse the first for
limits also, remove echo "<center>";
from inside the loop and place it outside
<?php
$p=4;
echo "<center>";
for($x=$p;$x>0;$x--)
{
/* for($q=$p;$q>$x;$q--)
{ //Not at all needed
echo "<center>";
} */
for($k=1;$k<$x;$k++)
{
echo $k;
}
if($x>=1)
{
for($v=$x; $v>=1; $v--)
{
echo $v;
}
}
echo "<br>";
}
echo "</center>";
Try this:
echo '<center>';
$p=4;
for($x=4;$x>0;$x--)
{
for($k=1;$k<$x;$k++)
{
echo $k;
}
if($x>=1)
{
for($v=$x; $v>=1; $v--)
{
echo $v;
}
}
echo "<br>";
}
echo '</center>';
Or an easier way:
$max = 4;
echo "<center>";
while ($max >= 1) {
$string = '';
for ($current = 1; $current <= $max; $current++) {
$string .= $current;
}
echo $string . strrev(substr($string, 0, -1));
echo '<br>';
$max--;
}
echo "</center>";
This is the function you need:
function printCenterNb($nb, $html = true) {
if ($html)
echo '<center>';
for($i = $nb; $i > 0; $i--) {
for($i2 = 1; $i2 < $i; $i2++)
echo $i2;
for($i2 = $i; $i2>=1; $i2--)
echo $i2;
if ($html)
echo "<br>";
}
if ($html)
echo '</center>';
}
printCenterNb(5);