开发者

PHP Variables Scope

file name myServices.php

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

in another file lets say file.php

    include "myServices.php"

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

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

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

?>


The previous variables are reassigned new values.


If it is just like you have it listed, then the $name and $gender variable's values are replaced with "ELENA" and "FEMALE"

Why don't you try, echo $name;, echo $gender;


As Codeacula said, those variables will be overwritten. The opening and closing PHP tags do not define scope. Variables are in what's called the global scope unless they are inside of a function or class. Global methods are as the name implies available, and can be overwritten inside functions and classes

When a variable is inside of a function then that variable is only available inside of that function unless it is prepended with the keyword global.

A quick search on google will give you more information on variable scope.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜