Warum funktioniert global nicht in Funktionen?
许云龙
许云龙 2019-02-02 11:14:57
0
4
1699

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>php.cn</title>

< /head>

<body>

<?php

$x=5;

$y=6;

function test(){

global $x,$y;

$y= $x+$y;

}

test();

echo $y;

?>

</body>

</html>

Nach der Ausführung ist das Ergebnis 6, was sollte nicht 11 sein? Ich habe $x überprüft und es gibt keinen Wert

许云龙
许云龙

Antworte allen(3)
明日边缘

你只是方法里使用了全局变量的值,全局变量的值并没有改变的

失去过去

使用 $GLOBAL 超全局函数 打印下数据就知道数据在是多少了 

过客
//是可以正常显示11呀,不行你复制本地测试一下看看
$x=5;
$y=6;
function test(){
global $x,$y;
$y=$x+$y;   //11= 5 + 6
}
test();
echo $x;	//5
echo "<br>";
echo $y;	//11


Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage