当前位置:  首页>> 技术小册>> PHP合辑1-基础入门

在程序中,变量用于存储一些值或数据,这些值或数据可以在程序中后续使用。变量也可以看作是容器,用于存储字符值、数值、内存地址和字符串。PHP有自己的变量声明和存储方式。
在处理PHP中的变量时,需要遵循一些规则和注意事项:

  • PHP中声明的任何变量必须以美元符号($)开头,后跟变量名。
  • 变量可以具有长的描述性名称(如$factorial、$even_nos)或短的名称(如$n、$f或$x)。
  • 变量名只能包含字母数字字符和下划线(即’a-z’、’A-Z’、’0-9’和’_’)在其名称中。甚至不能以数字开头。
  • 常量用作不能更改的简单值的变量。它也是区分大小写的。
  • 变量的赋值使用赋值运算符“等于(=)”。变量名位于等号的左侧,表达式或值位于赋值运算符“=”的右侧。
  • 必须记住,PHP中的变量名必须以字母或下划线开头,不能以数字开头。
  • PHP是一种弱类型语言,我们不需要声明变量的数据类型,PHP会根据值自动分析数据类型。在转换过程中也是如此。变量在使用前无需声明,它会根据需要自动将类型从一种类型转换为另一种类型。
  • PHP变量区分大小写,即$sum和$SUM被视为不同的变量。

PHP用于声明或构造变量的数据类型有:

  • 整数
  • 浮点数
  • 空值
  • 字符串
  • 布尔值
  • 数组
  • 对象
  • 资源

示例

  1. <?php
  2. // These are all valid declarations
  3. $val = 5;
  4. $val2 = 2;
  5. $x_Y = "gfg";
  6. $_X = "GeeksforGeeks";
  7. // This is an invalid declaration as it
  8. // begins with a number
  9. $10_ val = 56;
  10. // This is also invalid as it contains
  11. // special character other than _
  12. $f.d = "num";
  13. ?>

变量作用域

变量的作用域是指在程序中可以访问变量的范围,即变量的作用域是程序中可见或可以访问的部分。
根据作用域,PHP有三种变量作用域:

局部变量:在函数内部声明的变量称为该函数的局部变量,并且仅在该特定函数中具有作用域。简单来说,它无法在该函数之外被访问。在函数外部声明与函数内部同名的变量是完全不同的变量。我们将在后面的文章中详细了解函数。现在,可以将函数视为一组语句。

  1. <?php
  2. $num = 60;
  3. function local_var()
  4. {
  5. // This $num is local to this function
  6. // the variable $num outside this function
  7. // is a completely different variable
  8. $num = 50;
  9. echo "local num = $num \n";
  10. }
  11. local_var();
  12. // $num outside function local_var() is a
  13. // completely different Variable than that of
  14. // inside local_var()
  15. echo "Variable num outside local_var() is $num \n";
  16. ?>

输出:

  1. local num = 50
  2. Variable num outside local_var() is 60

全局变量:在函数外部声明的变量称为全局变量。这些变量可以在函数外部直接访问。要在函数内部访问这些变量,需要在变量之前使用“global”关键字来引用全局变量。

  1. <?php
  2. $num = 20;
  3. // function to demonstrate use of global variable
  4. function global_var()
  5. {
  6. // we have to use global keyword before
  7. // the variable $num to access within
  8. // the function
  9. global $num;
  10. echo "Variable num inside function : $num \n";
  11. }
  12. global_var();
  13. echo "Variable num outside function : $num \n";
  14. ?>

输出:

  1. Variable num inside function : 20
  2. Variable num outside function : 20

静态变量:PHP的特点是在执行完毕并释放内存后删除变量。但有时我们需要在函数执行完成后仍然存储变量。为了实现这一点,我们使用static关键字,这时的变量被称为静态变量。PHP根据变量的值关联一个数据类型。

示例:

  1. <?php
  2. // function to demonstrate static variables
  3. function static_var()
  4. {
  5. // static variable
  6. static $num = 5;
  7. $sum = 2;
  8. $sum++;
  9. $num++;
  10. echo $num, "\n";
  11. echo $sum, "\n";
  12. }
  13. // first function call
  14. static_var();
  15. // second function call
  16. static_var();
  17. ?>

输出:

  1. 6
  2. 3
  3. 7
  4. 3

您可能已经注意到,即使在第一次函数调用后,$num仍然会定期递增,但$sum不会。这是因为$sum不是静态变量,在第一次函数调用执行后,其内存会被释放。

可变变量:

PHP允许我们使用动态变量名,称为可变变量。
可变变量实际上就是变量,其名称是由另一个变量的值动态创建的。

示例:

  1. <?php
  2. $a = 'hello'; //hello is value of variable $a
  3. $$a = 'World'; //$($a) is equals to $(hello)
  4. echo $hello; //$hello is World i.e. $hello is new variable with value 'World'
  5. ?>

输出:

  1. World