Home  >  Q&A  >  body text

http://run.php.cn/The running result of online PHP program is incorrect

	<?php
	$x = 5;
	$y = 10;

	function myTest() {
		$GLOBALS[ 'y' ] = $GLOBALS[ 'x' ] + $GLOBALS[ 'y' ];
	}

	myTest();
	echo $y; // 应该输出 15 ,但在run.php.cn输出为10
	?>

风中咏歌风中咏歌1644 days ago1033

reply all(4)I'll reply

  • 风中咏歌

    风中咏歌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

    c.runoob.com网.jpg

    c.runoob.com网运行结果.jpg

    it1352.com网.jpg

    it1352.com网运行结果.jpg

    tool.lu网.jpg

    tool.lu网运行结果.jpg

    ##

    reply
    0
  • 风中咏歌

    风中咏歌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.

    reply
    0
  • 风中咏歌

    风中咏歌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)

    reply
    0
  • 老洪

    老洪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?

    reply
    0
  • Cancelreply