Constants of PHP Basics

Source: Internet
Author: User
Tags php basics
Constants

1, the composition of the constant = constant Name + constant value

Constant name: A combination of [a-za-z0-9_], and the number can not be started, case-sensitive, recommended capitalization

Constant value: Assignable data type integral type, float, Boolean, string, NULL

2. How to declare a constant

Define (' Constant name ', ' constant value ')

const constant NAME = constant Value (supported after PHP 5.3)

3, the constant detection bool defined (constant name)

4, the characteristics of the constant:

(1) Once defined, cannot be re-declared

(2) Once defined, the value cannot be re-assigned

5. Variable constant constant (constant name)

6, define and const difference

(1) can declare "location"

Define: Line of code, control structure, loop structure, function

Const: Line of code, class

Note:

Line of code: not control structure, loop body, function, class, Pure line of code

Constants declared with define in a function can only be used after a function call, otherwise an error will be made when the string output

(2) Scope

Constants declared by define and const in code lines, with global validity

Const constants declared in a class can only be used in a class

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

Const FOO = ' BAR ';

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

Define (' Bit_ '. $i, 1 << $i);

}

(4) const can only accept static scalar, and define may take any expression

For example:

Const BIT_5 = 1 << 5; Invalid invalid

Define (' Bit_5 ', 1 << 5); The effective valid

(5) const-defined constants are case-sensitive, while define can specify case sensitivity by using the third argument (true for case insensitivity)

For example:

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

Echo FOO; BAR

echo foo; BAR


(6) using const makes the code easy to read, and const itself is a language structure, and define is a function

(7) const is much faster at compile time than define

Graphic:


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

Personally, talk to yourself.

Definition of a constant: in a page run or class, declare a value that is always constant

use: formulas, project profiles, site roots, and so on


The above describes the basic PHP constants, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.