Php 简明教程

PHP - Constants

PHP 中的常量是简单值的一个名称或标识符。常量值在 PHP 脚本执行期间不能更改。

A constant in PHP is a name or an identifier for a simple value. A constant value cannot change during the execution of the PHP script.

  1. By default, a PHP constant is case-sensitive.

  2. By convention, constant identifiers are always uppercase.

  3. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscore.

  4. There is no need to write a dollar sign ($) before a constant, however one has to use a dollar sign before a variable.

Examples of Valid and Invalid Constant Names in PHP

以下是 PHP 中有效和无效常量名称的一些示例 -

Here are some examples of valid and invalid constant names in PHP −

// Valid constant names
define("ONE",     "first thing");
define("TWO2",    "second thing");
define("THREE_3", "third thing");
define("__THREE__", "third value");

// Invalid constant names
define("2TWO",    "second thing");

Difference between Constants and Variables in PHP

  1. Constants cannot be defined by simple assignment; they can only be defined using the define() function.

  2. Constants may be defined and accessed anywhere without regard to variable scoping rules.

  3. Once the Constants have been set, they may not be redefined or undefined.

Defining a Named Constant

PHP 函数库中的 define() 函数用于在运行时定义一个命名常量。

The define() function in PHP library is used to define a named constant at runtime.

define(string $const_name, mixed $value, bool $case = false): bool

Parameters

  1. const_name − The name of the constant.

  2. value − The value of the constant. It can be a scalar value (int, float, string, bool, or null) or array values are also accepted.

  3. case − If set to true, the constant will be defined case-insensitive. The default behavior is case-sensitive, i.e., CONSTANT and Constant represent different values.

define() 函数在成功时返回 "true",失败时返回 "false"。

The define() function returns "true" on success and "false" on failure.

Example 1

以下示例演示了 define() 函数的工作原理:

The following example demonstrates how the define() function works −

<?php
   define("CONSTANT", "Hello world.");

   echo CONSTANT;
   // echo Constant;
?>

第一条 echo 语句输出 CONSTANT 的值。您将获得以下 output

The first echo statement outputs the value of CONSTANT. You will get the following output

Hello world.

但是,当您取消对第二条 echo 语句的注释时,它将显示以下错误:

But, when you uncomment the second echo statement, it will display the following error −

Fatal error: Uncaught Error: Undefined constant "Constant" in hello.php: on line 5

如果将 case 参数设置为 False,PHP 不区分大小写常量。

If you set the case parameter to False, PHP doesn’t differentiate upper and lowercase constants.

Example 2

您还可以将数组用作常量的值。查看以下示例:

You can also use an array as the value of a constant. Take a look at the following example −

<?php
   define(
      $name="LANGS",
      $value=array('PHP', 'Java', 'Python')
   );
   var_dump(LANGS);
?>

它将生成以下 output

It will produce the following output

array(3) {
  [0]=>
  string(3) "PHP"
  [1]=>
  string(4) "Java"
  [2]=>
  string(6) "Python"
}

Using the constant() Function

echo 语句输出已定义常量的值。您还可以使用 constant() 函数。它返回 name 指示的常量的值。

The echo statement outputs the value of the defined constant. You can also use the constant() function. It returns the value of the constant indicated by name.

constant(string $name): mixed

如果您需要检索常量的值,但不知道其名称,constant() 函数非常有用。即,它存储在变量中或由函数返回。

The constant() function is useful if you need to retrieve the value of a constant, but do not know its name. I.e. it is stored in a variable or returned by a function.

<?php
   define("MINSIZE", 50);

   echo MINSIZE;
   echo PHP_EOL;
   echo constant("MINSIZE");	// same thing as the previous line
?>

它将生成以下 output

It will produce the following output

50
50

Using the defined() Function

PHP 函数库提供了一个 defined() 函数,用于检查是否给定一个已命名的常量存在。查看以下示例:

The PHP library provides a defined() function that checks whether a given named constant exists. Take a look at the following example −

<?php
   define('MAX', 100);

   if (defined('MAX')) {
      echo MAX;
   }
?>

它将生成以下 output

It will produce the following output

100

PHP 还有一个名为 "get_defined_constants()" 的函数,它返回一个包含所有已定义常量及其值在内的关联数组。

PHP also has a function called "get_defined_constants()" that returns an associative array of all the defined constants and their values.