Hướng dẫn what are the constants in php? - các hằng số trong php là gì?

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

Hằng số PHP đưa ra một ví dụ là gì?

Các hằng số tương tự như biến ngoại trừ một khi chúng được xác định, chúng không bao giờ có thể được xác định hoặc thay đổi. Họ vẫn không đổi trên toàn bộ chương trình. Các hằng số PHP tuân theo các quy tắc biến PHP tương tự. Ví dụ, nó có thể được bắt đầu bằng một chữ cái hoặc chỉ gạch dưới.similar to the variable except once they defined, they can never be undefined or changed. They remain constant across the entire program. PHP constants follow the same PHP variable rules. For example, it can be started with a letter or underscore only.

Có bao nhiêu loại hằng số trong PHP?

PHP xác định ("MinSize", 50); Echo Minsize; echo hằng số ("MinSize"); // Điều tương tự như dòng trước?> Chỉ có thể chứa dữ liệu vô hướng (Boolean, Integer, Float và String) có thể được chứa trong các hằng số.boolean, integer, float and string) can be contained in constants.

Hằng số mảng PHP là gì?

Có hai loại hằng số trong PHP, hằng số và hằng số lớp.Các hằng số có thể được xác định khá nhiều ở bất cứ đâu bằng cách sử dụng cấu trúc xác định, trong khi các hằng số lớp được xác định trong lớp riêng lẻ hoặc giao diện bằng từ khóa Const.the constants and the class constants. The constants can be defined pretty much anywhere using the define construct, while the class constants are defined within the individual class or interface using the const keyword.

Mục đích của hàm hằng số () trong PHP là gì?

hằng số () rất hữu ích nếu bạn cần truy xuất giá trị của một hằng số, nhưng không biết tên của nó.I E.Nó được lưu trữ trong một biến hoặc được trả về bởi một hàm.Chức năng này cũng hoạt động với hằng số lớp.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. This function works also with class constants.