# Multiplying values within a for

Posted on

#### Question :

To using the for to generate a loop of 4 reps! Inside this loop I make a very simple numerical multiplication.

``````<?php

for(\$loop = 1; \$loop <= 4; \$loop += 1)

{
echo (\$loop * 10)." ";
}
?>

``````

And I do not know how, but I would like the result to be

``````1 10 50 100
``````

How to do this!?

Only use a multiplier for each result, since it is predefined:

``````<?php

\$m = array(0, 1, 5, 16.66666666666667, 25);

for (\$loop = 1; \$loop <= 4; \$loop += 1) {
echo (\$loop * \$m[\$loop]) . " ";
}

?>
``````

Based on your code it would look like this!

``````\$str = [50,0];
for(\$i=1; \$i<=4; \$i++) {

if(\$i==1){
\$output = \$i ." ";
} else {
\$next = array_sum(\$str);
array_shift(\$str);
array_push(\$str,\$next);
if(\$i==2){
\$output .= (\$next/5) ." ";
} else {
\$output .= \$next." ";
}
}

}

echo \$output;
``````

output

``````1 10 50 100
``````

I could not imagine a better application that is below because the multipliers are 1,10,5 and 2 … But it worked out

Take a look:

``````<?php

\$valor = 1;

for(\$loop = 1; \$loop <= 4; \$loop += 1)  {

if(\$loop == 1){

\$valor = \$valor * 1;

}

if(\$loop == 2){

}

if(\$loop == 3){

}

if(\$loop == 4){

}

?>
``````

It has another option … It works too.

``````    <?php

\$valorInicial = 1;

\$valor = \$valorInicial;

for(\$loop = 1; \$loop <= 4; \$loop += 1)  {