0
PHP - How to split numbers from a Loop?
Hi! Can someone tell me how I can split numbers from a "FOR" loop. So that I can use the individual numbers in another math operation. Eg: for ($i=1; $i<=5; $i++) Result: 1 2 3 4 5 Now I want to add (or subtract or multiple or divide) a number (eg: 2) to each of the individual values; Like eg: 1 + 2 = 3 2 + 2 = 4 3 + 2 = 5 4 + 2 = 6 5 + 2 =7 How can I split the numbers from the Loop? I would prefer to do this with and without a function to understand how this works. Thanx in advance for answering.
3 Answers
0
Thank you Jay.
0
Hi! Is there a way to also do this:
$var=2;
for ($i=1; $i<=5; $i++)
echo $var." x ".$i." = ".$i."<br />";
Result
2 x 1 = 2
2 x 2 = 4 (but I want to use the 2 from before and add that to the 2 of the variable. So 2 + 2)
2 x 3 = 6 (4+2=6)
2 x 4 = 8 (6+2=8)
2 x 5 = 10 (8+2=10)
0
Hi Jay, it doesn't work. It gives me an undefined variable error.
Here is how I used your suggestion. Is this right?
$var=$var+2;
for ($i=1; $i<=5; $i++)
echo $var." x ".$i." = ".$i."<br />";