i have code :
<?php
$number =9;
$number2 = $number / 2;
for ($b=0; $b<=$number2; $b++){
for ($i=$number; $i>=1; $i--){
echo $i;
}
echo "<br/>";
for ($a=1; $a<=$number; $a++)
{
echo $a;
}
echo "<br/>";
}
?>
I want the result like this .
987654321
123456789
987654321
123456789
987654321
123456789
987654321
123456789
987654321
why if i put odd number like 9 , why always the result 10 loop ?
You have two loops in a loop, therefore you'll always get an even number of lines.
You don't actually need that structure, see:
$number=9;
$k=0;
$sum=1;
for($i=1; $i<=$number; $i++) {
for($j=1; $j<=$number; $j++) {
$k+=$sum;
echo $k;
}
echo '<br>';
//Sum once again so in the next iteration $k is 0 or 10
$k+=$sum;
//Invert the sign of $sum so in the next iteration it substracts, and then adds, and so on
$sum*=-1;
}
Also note that going from 0
to $number
inclusive (lower or equal comparation) you'll get one iteration more (from 0 to 9 there are 10 whole numbers).
Solve with this code , thx Gabriel.
$number=9;
$k=0;
$sum=1;
for($i=1; $i<=$number; $i++) {
for($j=1; $j<=$number; $j++) {
$k+=$sum;
echo $k;
}
echo '<br>';
//Sum once again so in the next iteration $k is 0 or 10
$k+=$sum;
//Invert the sign of $sum so in the next iteration it substracts, and then adds, and so on
$sum*=-1;
}