📜  PHP常量和变量有什么区别?

📅  最后修改于: 2022-05-13 01:56:41.481000             🧑  作者: Mango

PHP常量和变量有什么区别?

PHP常量 PHP常量是保持不变的标识符。通常,它在脚本执行期间不会更改。它们区分大小写。默认情况下,常量标识符总是大写的。通常,常量名称以下划线或字母开头,后跟多个字母和数字。他们不需要用 $ 符号写一个常量。 constant()函数用于返回常量的值。

例子:

PHP


PHP
";
   echo $x;
   echo "
";    echo $y; ?>


输出:

Welcome to geeksforgeeks.com!

PHP变量: PHP变量是为保存数据的内存地址指定的名称。声明PHP变量的基本方法是使用 $ 符号,后跟变量名。变量帮助PHP代码在程序中间存储信息。如果我们在分配变量之前使用它,那么它已经存储了一个默认值。我们可以用来构造变量的一些数据类型是整数、双精度和布尔值。

例子:

PHP

";
   echo $x;
   echo "
";    echo $y; ?>

输出:

Hello from geeksforgeeks!
5
10.5

PHP常量和PHP变量之间的区别

PHP ConstantsPHP Variables
In PHP constants there is no need to use $ sign.In PHP Variables the $ sign is been used.

The data type of PHP constant cannot be changed during the

execution of the script.

The data type of the PHP variable can be changed during the

execution of the script.

A PHP constant once defined cannot be redefined.A PHP variable can be undefined as well as can be redefined.

We can not define a constant using any simple assignment operator

rather it can only be defined using define().

We can define a variable using a simple assignment operation(=).
Usually, constants are written in numbers.On the other hand, variables are written in letters and symbols.
PHP constants are automatically global across the entire script.PHP variables are not automatically global in the entire script.
PHP constant is comparatively slower than PHP variableA PHP variable is comparatively faster than the PHP constant