Hướng dẫn php constant not working - hằng số php không hoạt động

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ệ

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.



#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].



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]:

Result: "Value of X: X"

0

1

2

Ewspencer tại Industrex Dot Com ¶

19 năm trước

3

4

5

6

7

2

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

6 năm trước

9

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

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

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

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

Bài Viết Liên Quan

Chủ Đề