Hướng dẫn what is define constants in php? - định nghĩa 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ệ

<?php// 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.

<?php

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).

<?php #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):
<?php
foo
();
const
X = 1;
function
foo() {
    echo
"Value of X: " . X;
}
?>
Result: "Value of X: X"

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php// 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

<?php 0

Hằng số được xác định là gì?

Hằng số là một cái tên có ý nghĩa thay thế một số hoặc chuỗi không thay đổi. Các hằng số lưu trữ các giá trị, như tên gọi, vẫn không đổi trong suốt quá trình thực hiện một ứng dụng. Bạn có thể sử dụng các hằng số được xác định bởi các điều khiển hoặc các thành phần bạn làm việc hoặc bạn có thể tạo riêng của mình.a meaningful name that takes the place of a number or string that does not change. Constants store values that, as the name implies, remain constant throughout the execution of an application. You can use constants that are defined by the controls or components you work with, or you can create your own.

DEFINE () trong PHP là gì?

Hàm xác định () xác định một hằng số.Các hằng số giống như các biến, ngoại trừ các khác biệt sau: giá trị của hằng không thể thay đổi sau khi nó được đặt.Tên liên tục không cần một dấu hiệu đô la hàng đầu ($)defines a constant. Constants are much like variables, except for the following differences: A constant's value cannot be changed after it is set. Constant names do not need a leading dollar sign ($)

Biến và không đổi trong PHP là gì?

Biến PHP thực thi tập lệnh.Một hằng số PHP một khi được xác định không thể được xác định lại.Một biến PHP có thể được xác định cũng như có thể được xác định lại.Chúng tôi không thể xác định hằng số bằng cách sử dụng bất kỳ toán tử gán đơn giản nào.Thay vào đó, nó chỉ có thể được xác định bằng cách sử dụng xác định ().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().

Ví dụ liên tục là gì?

Hằng số: Một hằng số có thể được định nghĩa là một giá trị cố định, được sử dụng trong các biểu thức và phương trình đại số.Một hằng số không thay đổi theo thời gian và có giá trị cố định.Ví dụ, kích thước của giày hoặc vải hoặc bất kỳ trang phục nào sẽ không thay đổi tại bất kỳ điểm nào.the size of a shoe or cloth or any apparel will not change at any point.