dongxi9326 2017-04-20 21:32
浏览 26
已采纳

return语句中的算术会导致意外行为

I'm currently learning PHP from a HTML, CSS, and JS background and I came across some unexpected behavior that interested me. Consequently, I experimented with the following code.

Experiment 1:

It seems that when written the return statement is written like this, everything before the arithmetic is removed/not rendered.

Code:

<?php
    function add($num, $num2) {
      return $num."+".$num2." = ".$num + $num2."<br>";
    }

    echo add(10, 7) . add(20, 1);
?>

Outputs:

17<br>
21<br>


Experiment 2:

However, when I change the first variable/parameter from $num to $num2, it seems that every between the first variable and the + operator is removed.

Code:

<?php
    function add($num, $num2) {
      return $num2."+".$num2." = ".$num + $num2."<br>";
    }

    echo add(10, 7) . add(20, 1);
?>

Outputs:

14<br>
2<br>


Experiment 3:

After trying it in JS, I realized that putting brackets around the arithmetic equation would output the expected result.

Code:

<?php
    function add($num, $num2) {
      return $num."+".$num2." = ".($num + $num2)."<br>";
    }

    echo add(10, 7) . add(20, 1);
?>

Outputs:

10+7 = 17<br>
20+1 = 21<br>

(Also making a $sum variable would fix the problem)


Question:

My question is what causes the unexpected behavior by not putting the brackets around the equation?

  • 写回答

2条回答 默认 最新

  • duanla8800 2017-04-20 21:59
    关注

    The behavior you are seeing is the result of "type juggling".

    Because PHP is not strictly-typed, a string can be interpreted as an integer when needed (or assumed to be needed by the interpreter), or vice versa. So the data type is converted, and if you're not careful can cause issues. In many other languages you would get an error if you treated a string like an integer or an integer like a string. JavaScript, for example, has String.parseInt() to explicitly change the type.

    What the interpreter is doing is roughly the following, step-by-step:

    1. $num - establish an integer with value 10
    2. 10."+" - concatenating an integer with a string

      • Convert the current output to a string (10 becomes "10"), and append a plus sign
      • Output is now "10+"
    3. "10+".$num2 - concatenating a string with an integer

      • Convert the integer to a string and append it
      • Output is now "10+7"
    4. "10+7"." = " - concatenating 2 strings
      • Output is now "10+7 = "
    5. "10+7 = ".$num - concatenating a string with an integer
      • Convert the integer to a string and append it
      • Output is now "10+7 = 10
    6. "10+7 = 10" + $num2 - arithmetic calculation between a string and an integer
      • Convert the string to an integer and add it to the next integer.
      • In this case, when PHP converts a string to an integer, it starts at the beginning of the string and returns all numerals until it hits the first non-number, so "10+7 = 10" pulls out the 10, then hits the + and stops looking for numbers.
      • Output is now 17;
    7. 17."<br>" - concatenation of an integer and a string
      • Convert the integer to a string and append the <br>
      • Output is now 17<br>.

    For reference:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 把Excel导入MATLAB显示错误怎么解决?
  • ¥15 Java中消息和缓存如何使用
  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx