Heim  >  Fragen und Antworten  >  Hauptteil

Das Ausführungsergebnis des http://run.php.cn/online PHP-Programms ist falsch

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

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

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

风中咏歌风中咏歌1644 Tage vor1034

Antworte allen(4)Ich werde antworten

  • 风中咏歌

    风中咏歌2020-03-28 16:56:05

    下面的在线PHP编辑器的PHP都是PHP7以上的版本,运行结果都是15

    c.runoob.com网.jpg

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

    it1352.com网.jpg

    it1352.com网运行结果.jpg

    tool.lu网.jpg

    tool.lu网运行结果.jpg

    Antwort
    0
  • 风中咏歌

    风中咏歌2020-03-27 22:06:38

    对于这个原因,我还是持怀疑态度,这样一来,global就要看php的版本就会有不同的运算结果,这样兼容性就有点差了。

    Antwort
    0
  • 风中咏歌

    风中咏歌2020-03-27 22:01:03

    找到原因了,见本网站链接 https://www.php.cn/php/php-variables.html 注:上述代码在PHP5版本下输出值是15;但是PHP中文网在线实例环境是PHP7版本的,所以输出值应为10。因为在最新的php7版本中,PHPglobal变量处理机制进行了修改,此版本下global关键字只能引用简单变量。详情区别介绍可查看此文: 关于PHP5和PHP7版本下global变量机制有哪些变化?(代码实测)

    Antwort
    0
  • 老洪

    老洪2020-03-27 21:46:22

    $GLOBAL['x'] 表示的是,对数组$GLOBAL取出键名为'x'的值。问题你这段代码,这个数组哪里来的?

    Antwort
    0
  • StornierenAntwort