PHP变量范围
文件名myServices.phpPHP变量范围,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标记没有定义作用域
<?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
?>