Click to See Complete Forum and Search --> : A test


T.muqiao
12-13-2006, 05:43 PM
need print:1 4 7 10 13 16 19 22 25,

for($i=1;$i<=9;$i=_____){//fill the blank
echo $i;
}

Piranha
12-13-2006, 05:56 PM
What do you want to happen? I can see that you want to add 3 and echo the result. But do you want it done 9 times or do you want it while the value is lower than some value? Depending on how you want it you can do any of these ways:

for ($i = 1; $i <= 9; $i = $i + 3) {
echo $i;
}

for ($i = 0; $i < 9; $i++) {
echo $i * 3 + 1;
}

T.muqiao
12-13-2006, 06:36 PM
I think it's not wrong,
but have answer to this.

suntra
12-13-2006, 06:55 PM
<?
$intMinimum = 1;
$intMaximum = 25;
$intSkip = 3;
for($intI = $intMinimum; $intI <= $intMaximum; $intI = $intI + $intSkip) {
echo $intI . '<br />' . "\n";
}
?>


Or even better...


<?
$intMinimum = 1;
$intMaximum = 25;
$intSkip = 3;
$intMinimum = min($intMinimum, $intMaximum);
$intMaximum = max($intMinimum, $intMaximum);
$intSkip = abs($intSkip);
for($intI = $intMinimum; $intI <= $intMaximum; $intI = $intI + $intSkip) {
echo $intI . '<br />' . "\n";
}
?>

Weedpacket
12-13-2006, 08:27 PM
Or
foreach(range(1,25,3) as $i)
{
...
}

T.muqiao
12-13-2006, 10:05 PM
a answer to this


<?php
for($i=1;$i<=9;$i=10,printf(" 4 7 10 13 16 19 22 25") ){
echo $i;
}
?>

suntra
12-13-2006, 10:31 PM
Well you almost only output a string !! So PHP doesn't calculate anything (or almost !!) what's the use ?!

Piranha
12-14-2006, 07:08 AM
I think it's not wrong,
but have answer to this.

It is not possible to give an answer to your first post since it is totally messed up with the value of $i. But I gave two answers that would work, and you have gotten 3 other answers as well. All those 5 answers would work without any problem IF you mean what have been answered. If not then you have to ask in a way that we can understand.

By the way, a better way to follow your way would be

<?php
echo "1 4 7 10 13 16 19 22 25";
?>

Or to just do this in the HTML code:

1 4 7 10 13 16 19 22 25