<?php $x = 5; $y = 10; function myTest() { $GLOBALS[ 'y' ] = $GLOBALS[ 'x' ] + $GLOBALS[ 'y' ]; } myTest(); echo $y; // 应该输出 15 ,但在run.php.cn输出为10 ?>
风中咏歌2020-03-28 16:56:05
The PHP of the following online PHP editors are all versions of PHP7 or above, and the running results are all 15
##
风中咏歌2020-03-27 22:06:38
For this reason, I am still skeptical. In this way, global will have different calculation results depending on the PHP version, so the compatibility will be a bit poor.
风中咏歌2020-03-27 22:01:03
Found the reason, see this website link https://www.php.cn/php/php-variables.html Note: The output value of the above code in the PHP5 version is 15; however, the online instance environment of the PHP Chinese website is the PHP7 version, so the output value should be 10. Because in the latest php7 version, the PHP global variable processing mechanism has been modified, the global keyword in this version can only refer to simple variables. For a detailed introduction to the differences, please see this article: What are the changes in the global variable mechanism under PHP5 and PHP7? (Code actual test)
老洪2020-03-27 21:46:22
$GLOBAL['x'] means to retrieve the value with key name 'x' from array $GLOBAL. Question about your code, where does this array come from?