Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP变量范围_Php_Variables_Scope - Fatal编程技术网

PHP变量范围

PHP变量范围,php,variables,scope,Php,Variables,Scope,文件名myServices.php <?php $gender = 'MALE'; ?> 在另一个文件中,让我们说file.php 包括“myServices.php” $name='SAM'; $age='23'; ?> 先前的变量将重新分配新值。如果它与您列出的一样,则$name和$gender变量的值将替换为“ELENA”和“FEMALE” 为什么不试试,echo$name,echo$gender正如代码所说,这些变量将被覆盖。开始和结束的PHP标记没有定义作用域

文件名myServices.php

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

在另一个文件中,让我们说file.php

包括“myServices.php”
$name='SAM';
$age='23';
?>

先前的变量将重新分配新值。

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


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

正如代码所说,这些变量将被覆盖。开始和结束的PHP标记没有定义作用域。变量位于所谓的全局范围内,除非它们位于函数或类内部。顾名思义,全局方法是可用的,可以在函数和类内部重写

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


在google上快速搜索将为您提供有关可变范围的更多信息。

您可能想从代码块中删除这个问题,我很难找到它。
    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

?>