[PHP series tutorial] (5) -- PHP Constants

Source: Internet
Author: User
Document directory
  • I. Syntax
  • Ii. predefined Constants

[PHP series tutorial] (4) describes the PHP variable: http://blog.csdn.net/rocket5725/archive/2010/01/08/5159704.aspx

This section describes constants in PHP, including their syntax and predefined constants.

A constant is the identifier (name) of a simple value ). As its name implies, this value cannot be changed during script execution (except for so-called magic constants, they are actually not constants ). Constants are case sensitive by default. By convention, constant identifiers are always capitalized.

Constant names and any other PHP labels follow the same naming rules. A valid constant name starts with a letter or underline followed by any letter, number, or underline. The regular expression is like this: [A-Za-Z _/x7f-/xFF] [a-zA-Z0-9 _/x7f-/xFF] *

Note: here the letter is a-Z, A-Z, and ASCII characters from 127 to 255 (0x7f-0xff.

Like superglobals, the constant range is global. You can access constants anywhere in the script without specifying the scope.

I. Syntax

You can use the define () function to define constants. Once a constant is defined, it cannot be changed or undefined.

Constants can only contain scalar data (Boolean, integer, float, and string ).

You can simply get the value of a constant by specifying its name. Do not add the $ symbol before the constant. If the constant name is dynamic, you can use the constant () function to read the constant value. Use get_defined_constants () to obtain a list of all defined constants.

Note: constants and (global) variables are in different namespaces. This means that for example, true and $ true are different.

If an undefined constant is used, PHP assumes that you want the name of the constant, just as you call it with a string (constant corresponds to "constant "). At this time, an e_notice-level error will be issued. See why $ Foo [bar] is incorrect in the Manual (unless you define Bar as a constant using define () in advance ). If you only want to check whether a constant is defined, use the defined () function.

Constants and variables are different:

1. There is no dollar sign before the constant ($ );

2. constants can only be defined using the define () function, but cannot be defined using the value assignment statement;

3. constants can be defined and accessed anywhere regardless of the variable range rules;

4. Once defined, a constant cannot be redefined or canceled;

5. The constant value can only be a scalar.

Code 1: Define a constant

<? Php <br/> define ("constant", "Hello world. "); <br/> echo constant; // outputs" Hello world. "<br/> echo constant; // outputs" constant "and issues a notice. <br/>?>

Ii. predefined Constants

PHP provides a large number of predefined constants to any script it runs. However, many constants are defined by different extension libraries. They only appear when these extension libraries are loaded, dynamically loaded, or included during compilation.

There are four magic constants that change based on their locations. For example, the value of _ line _ depends on the rows in the script. These special constants are case-insensitive, as follows:

Name Description
_ Line __ The current row number in the file.
_ File __ The complete file path and file name.
_ Function __ Function Name (New in PHP 4.3.0 ).
_ Class __ Class Name (New in PHP 4.3.0 ).
_ Method __ Class Method Name (this is newly added to PhP 5.0.0 ).

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.