PHP basic constants

WBOY
Release: 2016-08-08 09:26:32
Original
756 people have browsed it

Constant

1. Constant composition = constant name + constant value

Constant name: [a- zA-Z0-9_] combination , and numbers cannot be used as the beginning, case-sensitive, uppercase is recommended

Constant value: assignable data type Integer, floating point, Boolean, string, null

2. How to declare constants

define( 'Constant name','Constant value')

const Constant name = constant value (supported after PHP 5.3)

3. Detection of constants bool defined (constant name)

4. Characteristics of constants:

(1) Once defined, it cannot be redeclared

( 2) Once defined, it cannot be reassigned

5. Variable constant constant (constant name)

6. The difference between define and const

(1) "Position" can be declared

define: line of code, control structure, loop structure, function

const: line of code, class

Note:

Line of code: not in the control structure, loop body, function, class, but a pure line of code

The constants declared with define in the function can only be used after the function is called, otherwise an error will be reported as a character String output

(2) Scope

define and const Constants declared in the code line have global validity

const Constants declared in the class , can only be used in classes

(3) const uses an ordinary constant name, define can use an expression as the name

const FOO = 'BAR';

for ($i = 0; $i < 32; ++$i) {

define('BIT_' . $i, 1 << $i);

}

(4) const can only accept static scalars, while define can take any expression

For example :

_Const Bit_5 = 1 & LT; & LT; 5; // Invple Invalid

Define ('Bit_5', 1 & LT; & LT; 5); // Valid Valid

(5) Constants defined by const are case-sensitive, and define can specify whether case-sensitive is through the third parameter (true means case-insensitive)

For example:

define('FOO', 'BAR', true);

echo FOO; // BAR

echo foo;

(6)

Using const makes the code simple and easy to read. const itself is a language structure, and define is a function

(7)

const is much faster than define during compilationIllustration:

Image resource address: http://download.csdn.net/detail/zz249456649/8571357

Just a personal comment

Definition of constants: When the page is running or in a class, declare a constant value

Usage:

Formulas, project configuration files, website root directories, etc.

The above has introduced the basic constants of PHP, including related content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template