<?php
$x=10;
$y=20;
function test(){
global $x,$y; //使用global关键字
$y=$x+$y;
}
test();
echo $y;//输出30
这段代码怎么输入还是 20 呢?
When outputting, the result of the annotation is always the same - PHP Chinese website Q&A - When outputting, the result of the annotation is always the same - PHP Chinese website Q&A
Take a look around and learn.
考虑到php版本的兼容性问题,不建议这么写,推荐的写法如下:
<?php $GLOBALS['x']=10; $GLOBALS['y']=20; function test(){ $GLOBALS['y']=$GLOBALS['x']+$GLOBALS['y']; } test(); echo $GLOBALS['y'];//输出30
When outputting, the result of the annotation is always the same - PHP Chinese website Q&A - When outputting, the result of the annotation is always the same - PHP Chinese website Q&A
Take a look around and learn.
考虑到php版本的兼容性问题,不建议这么写,推荐的写法如下: