Arithmetic operation within string concatenation without parenthesis causes strange result

后端 未结 3 620
星月不相逢
星月不相逢 2021-01-13 17:34

Consider the following line of code:


The output of that is 3, which is the expec

3条回答
  •  渐次进展
    2021-01-13 17:40

    Your string '10 - 7 = ' is being concatenated with $x. Then that is being interpreted as an int which results in 10 and then 7 is subtracted, resulting in 3.

    For more explanation, try this:

    echo (int) ('10 - 7 = ' . 10); // Prints "10"
    

    More information on string to number conversion can be found at http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

    If the string starts with valid numeric data, this will be the value used

提交回复
热议问题