Why does global not work in functions?
许云龙
许云龙 2019-02-02 11:14:57
0
4
1608

<!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>

After execution, the result is 6, shouldn’t it be 11? I checked that $x has no value

许云龙
许云龙

reply all(3)
明日边缘

You only used the value of the global variable in the method, and the value of the global variable did not change.

失去过去

Use the $GLOBAL super global function to print the data and you will know how much the data is

过客
//是可以正常显示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


Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!