有什么需要调整的吗
来源:1-9 编程练习
慕后端0484621
2017-09-13 14:16:08
<?php
header('content-type:text/html;charset=utf-8');
function test1(){
$imooc1='imooc';
echo $imooc1;
//$imooc=imooc;
}
test1();
echo '<hr/>';
$imooc2='imooc';
function test2(){
global $imooc2;
echo $imooc2;
}
test2();
echo '<hr/>';
$imooc3='imooc';
function test3(){
echo $GLOBALS['imooc3'];
}
test3();
?> 2回答
慕丝3243066
2017-09-13
三种方法都可以正确的输出'imooc',方法一变量只在函数内部有效,方法二使用了全局变量,方法三使用了超全局变量。如果解决您的问题请采纳,祝学习愉快!
imooc_澈
2017-09-13
您好,您通过global关键字以及预定义变量$_GLOBALS再函数中全局变量的方式是正确的,不过本题不需要写那么多,根据题意,写下面就可以了
<?php
header('content-type:text/html;charset=utf-8');
$imooc='imooc';
//方法一:使用global关键字
function test1(){
global $imooc;
echo $imooc;
}
test1();
echo '<hr/>';
//方法二:使用$GLOBALS['imooc']
function test2(){
echo $GLOBALS['imooc'];
}
test2();
?>
相似问题