Hướng dẫn how do you define a constant in php? - làm thế nào để bạn xác định một hằng số trong php?

Mục lục

  • Cú pháp
  • Hằng số được xác định trước
  • Hằng số ma thuật

Một hằng số là một định danh (tên) cho một giá trị đơn giản. Như tên cho thấy, giá trị đó không thể thay đổi trong quá trình thực hiện tập lệnh (ngoại trừ các hằng số ma thuật, không thực sự là hằng số). Hằng số nhạy cảm trường hợp. Theo quy ước, số nhận dạng liên tục luôn luôn được viết hoa.

Ghi chú::

Trước PHP 8.0.0, các hằng số được xác định bằng hàm xác định () có thể không nhạy cảm trường hợp.define() function may be case-insensitive.

Tên của một hằng số tuân theo các quy tắc giống như bất kỳ nhãn nào trong PHP. Một tên hằng số hợp lệ bắt đầu bằng một chữ cái hoặc dấu gạch dưới, theo sau là bất kỳ số lượng chữ cái, số hoặc dấu gạch dưới. Như một biểu hiện chính quy, nó sẽ được thể hiện như vậy: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$

Có thể xác định các hằng số () có tên dành riêng hoặc thậm chí không hợp lệ, có giá trị chỉ có thể được truy xuất với hàm hằng số (). Tuy nhiên, làm như vậy không được khuyến khích.define() constants with reserved or even invalid names, whose value can only be retrieved with the constant() function. However, doing so is not recommended.

Ví dụ #1 Tên hằng số hợp lệ và không hợp lệ

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>

Lưu ý: Đối với mục đích của chúng tôi ở đây, một chữ cái là A-Z, A-Z và các ký tự ASCII từ 128 đến 255 (0x80-0xff).: For our purposes here, a letter is a-z, A-Z, and the ASCII characters from 128 through 255 (0x80-0xff).

Giống như Superglobals, phạm vi của một hằng số là toàn cầu. Các hằng số có thể được truy cập từ bất cứ nơi nào trong một kịch bản mà không liên quan đến phạm vi. Để biết thêm thông tin về phạm vi, hãy đọc phần thủ công về phạm vi biến.

Lưu ý: Kể từ Php 7.1.0, hằng số lớp có thể khai báo khả năng hiển thị của được bảo vệ hoặc riêng tư, làm cho chúng chỉ có sẵn trong phạm vi phân cấp của lớp được xác định.: As of PHP 7.1.0, class constant may declare a visibility of protected or private, making them only available in the hierarchical scope of the class in which it is defined.

WBCarts tại Juno Dot Com ¶

10 năm trước

11/14/2016 - note updated by sobak
-----

CONSTANTS and PHP Class Definitions

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away.

define

('MIN_VALUE', '0.0');   // RIGHT - Works OUTSIDE of a class definition.
define('MAX_VALUE', '1.0');   // RIGHT - Works OUTSIDE of a class definition.

//const MIN_VALUE = 0.0;         RIGHT - Works both INSIDE and OUTSIDE of a class definition.
//const MAX_VALUE = 1.0;         RIGHT - Works both INSIDE and OUTSIDE of a class definition.

class Constants
{
 
//define('MIN_VALUE', '0.0');  WRONG - Works OUTSIDE of a class definition.
  //define('MAX_VALUE', '1.0');  WRONG - Works OUTSIDE of a class definition.
const MIN_VALUE = 0.0;      // RIGHT - Works INSIDE of a class definition.
 
const MAX_VALUE = 1.0;      // RIGHT - Works INSIDE of a class definition. public static function getMinValue()
  {
    return
self::MIN_VALUE;
  }

  public static function

getMaxValue()
  {
    return
self::MAX_VALUE;
  }
}
?>

#Example 1:
You can access these constants DIRECTLY like so:
* type the class name exactly.
* type two (2) colons.
* type the const name exactly.

#Example 2:
Because our class definition provides two (2) static functions, you can also access them like so:
* type the class name exactly.
* type two (2) colons.
* type the function name exactly (with the parentheses).

#Example 1:
$min = Constants::MIN_VALUE;
$max = Constants::MAX_VALUE; #Example 2:
$min = Constants::getMinValue();
$max = Constants::getMaxValue(); ?>

Once class constants are declared AND initialized, they cannot be set to different values -- that is why there are no setMinValue() and setMaxValue() functions in the class definition -- which means they are READ-ONLY and STATIC (shared by all instances of the class).

Warwick dot jm dot barbnes tại gmail dot com ¶

2 năm trước

The documentation says, "You can access constants anywhere in your script without regard to scope", but it's worth keeping in mind that a const declaration must appear in the source file before the place where it's used.

This doesn't work (using PHP 5.4):
foo();
const
X = 1;
function
foo() {
    echo
"Value of X: " . X;
}
?>
Result: "Value of X: X"

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
0

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
1

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
2

Ewspencer tại Industrex Dot Com ¶

19 năm trước

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
3

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
4

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
5

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
6

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
7

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
2

Gried tại Nospam Dot Nsys Dot của ¶

6 năm trước

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
9

11/14/2016 - note updated by sobak
-----
0

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
2

Hafenator2000 tại Yahoo Dot Com ¶

17 năm trước

11/14/2016 - note updated by sobak
-----
2

Andreas R. ¶

15 năm trước

11/14/2016 - note updated by sobak
-----
3

Raheel Khan ¶

7 năm trước

11/14/2016 - note updated by sobak
-----
4

11/14/2016 - note updated by sobak
-----
5

11/14/2016 - note updated by sobak
-----
6

11/14/2016 - note updated by sobak
-----
7

Sumon Mahmud (Abu Taleb)

2 năm trước

11/14/2016 - note updated by sobak
-----
8

11/14/2016 - note updated by sobak
-----
9

CONSTANTS and PHP Class Definitions 0

CONSTANTS and PHP Class Definitions 1

CONSTANTS and PHP Class Definitions 2

CONSTANTS and PHP Class Definitions 3

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
2

Ewspencer tại Industrex Dot Com ¶

17 năm trước

CONSTANTS and PHP Class Definitions 5

CONSTANTS and PHP Class Definitions 6

CONSTANTS and PHP Class Definitions 7

Andreas R. ¶

15 năm trước

11/14/2016 - note updated by sobak
-----
3

CONSTANTS and PHP Class Definitions 9

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 0

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 1

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 2

// Valid constant names
define("FOO",     "something");
define("FOO2",    "something else");
define("FOO_BAR""something more");// Invalid constant names
define("2FOO",    "something");// This is valid, but should be avoided:
// PHP may one day provide a magical constant
// that will break your script
define("__FOO__""something"); ?>
2

Raheel Khan ¶

7 năm trước

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 4

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 5

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 6

Sumon Mahmud (Abu Taleb)

bão táp ¶

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 7

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 8

Using "define('MY_VAR', 'default value')" INSIDE a class definition does not work as expected. You have to use the PHP keyword 'const' and initialize it with a scalar value -- boolean, int, float, string (or array in PHP 5.6+) -- right away. 9

0

Làm thế nào hằng số có thể được xác định?

Các hằng số có thể được xác định bằng cách sử dụng từ khóa Const hoặc bằng cách sử dụng hàm xác định ()-. Mặc dù DEFINE () cho phép một hằng số được xác định thành một biểu thức tùy ý, từ khóa Const có các hạn chế như được nêu trong đoạn tiếp theo. Khi một hằng số được xác định, nó không bao giờ có thể được thay đổi hoặc không xác định.using the const keyword, or by using the define()-function. While define() allows a constant to be defined to an arbitrary expression, the const keyword has restrictions as outlined in the next paragraph. Once a constant is defined, it can never be changed or undefined.

Làm thế nào chúng ta có thể tạo hằng số trong PHP?

Hằng số PHP có thể được xác định theo 2 cách: sử dụng hàm xác định ().Sử dụng từ khóa const ...
Tên: Nó chỉ định tên không đổi ..
Giá trị: Nó chỉ định giá trị không đổi ..
Không phân biệt trường hợp: Chỉ định xem một hằng số có nhạy cảm trường hợp hay không.Giá trị mặc định là sai.Nó có nghĩa là nó nhạy cảm theo mặc định ..

Chúng ta có thể xác định mảng không đổi trong PHP không?

Có, bạn có thể định nghĩa một mảng là hằng số.Từ PHP 5.6 trở đi, có thể xác định hằng số là biểu thức vô hướng và cũng có thể xác định hằng số mảng.Có thể định nghĩa các hằng số là tài nguyên, nhưng nên tránh, vì nó có thể gây ra kết quả bất ngờ.. From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant. It is possible to define constants as a resource, but it should be avoided, as it can cause unexpected results.

Làm thế nào bạn có thể xác định một ví dụ cho không đổi?

Trong toán học, một hằng số là một số cụ thể hoặc một biểu tượng được gán một giá trị cố định.Nói cách khác, hằng số là một giá trị hoặc số không bao giờ thay đổi trong biểu thức.Giá trị của nó liên tục giống nhau.Ví dụ về hằng số là 2, 5, 0, -3, -7, 2/7, 7/9, v.v.a specific number or a symbol that is assigned a fixed value. In other words, a constant is a value or number that never changes in expression. Its value is constantly the same. Examples of constant are 2, 5, 0, -3, -7, 2/7, 7/9 etc.