gongzhonghao.png
扫码关注我们
最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php 中global关键字的使用

来源:http://erdangjiade.com/topic/3097.html 你好,世界。 2017-09-25 22:42浏览(7)

量分为全局变量和局部变量。学过C语言的童鞋都知道,全局变量的作用域是整个整个文件。在即使在函数内部也有效,但在php中,如果在函数中使用全局变量,php会认为这个变量没有定义。如果我们需要在函数内部使用这个全局变量,这时我们就需要在函数内部,这个全局变量前加关键字global。下面是自己写的一个小demo。用来证明我上面说的

<?php

 

    $str = "string";

    function test()
    {  

       if (isset($str)) 
       {
          echo "the string is defined";
       }
       else 
       {
          echo "the string is undefined";
       }
    }
    test();

?>

这是在浏览器中的运行结果:

8eebfeb0gb6a48ec23070&690.jpg

<?php
 
    $str = "string";
    function test()
    {  
       global $str;//上面的test函数中没有这句话
       if (isset($str)) 
       {
          echo "the string is defined";
       }
       else 
       {
          echo "the string is undefined";
       }
    }
    test();
?>

这是在浏览器中的运行结果:

8eebfeb0gb6a4a32d9abf&690.jpg

以上就是php 中global关键字的使用 的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2