变量是不同类型信息的存储。声明变量时不能以数字开头,它以“$”符号开头,后跟变量名称(例如 c o d e )。它只能包含下划线和字母数字字符 。 ′ code)。它只能包含下划线和字母数字字符。 ' code)。它只能包含下划线和字母数字字符。′code’ 和 ‘$CODE’ 是两个不同的变量,因为它区分大小写。
<!DOCTYPE html><html><body><?php
functioncode(){
$a=1;// 本地范围echo"<p>Variable a inside function is: $a</p>";}code();// Using variable 'a' outside the function will generate ERRORecho"<p>Variable a outside function is: $a</p>";?></body></html>
输出 :
全局:
<!DOCTYPE html><html><body><?php$a=1;// GLOBAL SCOPEfunctioncode(){
// Using variable 'a' inside this function will generate ERRORecho"<p>Variable a inside function is: $a</p>";}code();echo"<p>Variable a outside function is: $a</p>";?></body></html>
输出 :
全局关键字:
要从函数内访问全局变量,请使用 Global 关键字。为此,您必须在函数内的变量之前使用 Global 关键字。
n a m e = a r r a y ( “ j o h n ” , “ j o h n n y ” , ” h a r r y ” ) ; / / h e r e ‘ name = array(“john”, “johnny”,”harry”); //here ‘ name=array(“john”,“johnny”,”harry”);//here‘name’ is an array