PHP变量范围

时间:2010-08-18 02:39:55

标签: php variables scope

文件名 myServices.php

<?php
   $gender = 'MALE';
?>

在另一个文件中,请说 file.php

    

    include "myServices.php"

    $name = 'SAM';
    $age  = '23';
?>

<!--after some more HTML code-->
<?php

    $gender = 'FEMALE';        
    $name = 'ELENA';
    //Question:
    //In the above statements are there new variables created or the 
    //previous variables are reassigned new values

?>

3 个答案:

答案 0 :(得分:11)

以前的变量是重新分配的新值。

答案 1 :(得分:1)

如果它就像您列出的那样,则$name$gender变量的值将替换为“ELENA”和“FEMALE”

为什么不试试,echo $name;echo $gender;

答案 2 :(得分:1)

正如Codeacula所说,这些变量将被覆盖。打开和关闭PHP标记不定义范围。变量位于所谓的全局范围内,除非它们位于函数或类中。全局方法顾名思义,可以在函数和类中重写

当变量在函数内部时,该变量仅在该函数内部可用,除非它前面加上关键字global

谷歌上的快速搜索将为您提供有关变量范围的更多信息。

相关问题