Composite allocation operator

Asked

Viewed 72 times

2

I can’t understand why he won’t perform the expression I want.

Follows the code:

$i = 1; $ranking = 1; $r = 0;
for($i = 1; $i <=5; $i++){
    $p = isset($_GET["n$i"])?$_GET["n$i"]:0;
    echo "$ranking ° Numero: $p";
    $r += $p;
    $ranking++;
}

echo "A Multiplicação dos numeros é igual a : $r"

The main line of the code is $r += $p;, it adds the variables and displays in the variable $r, so far so good, but change to $r *= $p; or to $r /= $p; the result will always be 0 (ZERO), because?

  • PS: is missing a ; (semicolon) in the last line of the code also.

  • Yes, I must have done it in copy+Paste, thank you ^^

2 answers

3


How much is 0 multiplied by any value? It’s 0, right? So it’s explained. Pure mathematics.

How much is 0 divided by any amount? Also 0.

Programming follows the mathematical rules.

If you put 1 in $r Something may come out in multiplication, but it’s probably not what you want. In the division would probably be better another value, but it doesn’t seem that changing the operator makes sense in this code. Even it can probably be simplified, so it was presented.

If I had a bigger context I could improve more, but it’s better this way:

$r = 0;
for ($ranking = 1; $ranking <=5; $ranking++) {
    $p = isset($_GET["n$ranking"])?$_GET["n$ranking"]:0;
    echo "$ranking ° Numero: $p";
    $r += $p;
}

There were unnecessary variables there.

If you want to multiply:

$r = 1;
for ($ranking = 1; $ranking <=5; $ranking++) {
    $p = isset($_GET["n$ranking"])?$_GET["n$ranking"]:0;
    echo "$ranking ° Numero: $p";
    $r *= $p;
}

I put in the Github for future reference.

I didn’t even mention that this code has serious security issues because it picks up a piece of data that comes from the outside and trusts that it will be just right. But that’s another matter.

  • The question is, if the variable $p takes 4 times the number 5, that is.. 5 5 5 5 with the operator $r += $p; will the result be 20 correct? however, if I change to $r *= $p; the result will be 0. .

  • 1

    I don’t know what you’re talking about.

  • I just edited the comment, I sent it in half, if possible read above again please. Thanks

  • Thank you very much, I knew something was wrong, and that I was not filling up, always good to have more than one programmer to check the code. I changed the lines: $i = 1; $ranking = 1; $r = 1; echo "Number multiplication equals : ". ($r-1);

  • I’m still learning PHP, as I said above, thank you very much for the tips. could show me the simplified way of this code?

  • @Nicolasguilherme I edited and put the simplified form

  • 1

    Thanks, I just saw, I posted my version also before you post yours, the ranking variable was even unnecessary, is that I was doing other tests and ended up leaving her there. My mistake was not realizing that I initialized the $r variable with 0(ZERO), after that great touch, everything made sense. With regard to code security, I have never studied about security, so as soon as I finish my studies on the syntax of that language, I will begin to study about code security. Thanks for the touch, and for the examples, will help those who come to have the same doubt.

Show 2 more comments

1

For those who have Doubts, the code was like this.

$i = 1; $r = 1;
while($i <=5) {
    $p = isset($_GET["n$i"])?$_GET["n$i"]:0;
    echo "$i ° Numero: $p <br>";
    $r *= $p;
    $i++;
}


echo "<br>A Multiplicação dos numeros é igual a : ".($r-1);
  • Did the bigown code not work? The only thing that seems to have changed was the $r-1 at the end.

  • Hi, I hadn’t seen his code yet, this is the same thing as mine, but with the repetition structure for, and the ranking variable I removed from the code.

  • I understand, his edition came later, but his reply was not explained so I found the one with the mustache better, nothing against yours, only I think that for future visitors the other answer will be more useful. ;D

  • Yes, he who helped me to clear the doubt, he deserves ^^.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.