Learning notes for constant type based on PHP beginners

Source: Internet
Author: User
Tags php define

This article will summarize some usage of PHP constant types. This is a friend's learning note. I will share it with you.

Once a constant in PHP is defined, it cannot be changed or undefined. constants have predefined constants (internal constants) and default PHP constants, you can also customize constants.

The following are some predefined constants: (For details, refer to the PHP manual)

TRUE: the constant is true (TRUE) and true (TRUE ).

FALSE: the constant is a pseudo value (false), which is FALSE (not true ).

PHP_VERSION, which is the version number of the current PHP program, for example, "5.2.17 ".

PHP_ OS, which is the name of the operating system currently executing the PHP program parser, such as "Linux ".

DIRECTORY_SEPARATOR, which indicates the path separator, "" On Windowns, and "/" on Linux.

The following are some default PHP constants:

E_ERROR. This constant indicates the most recent error.

E_WARNING, which refers to the nearest warning.

E_PARSE. This constant is a potential problem with the parsing syntax.

E_NOTICE. This regular expression is unusual but not necessarily an error. For example, you can access a non-existent variable.

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.

PHP has several constants called "Magic constants" whose values change with their position in the code. For example, the value of "_ LINE _" depends on the rows in the script. These special constants are case-insensitive (but generally large-write), as shown below:

The row number of the constant in the "_ LINE _" file.

The complete path and FILE name of the "_ FILE _" FILE. If it is used in a file to be included, the file name to be included is returned. _ FILE _ also contains an absolute path (if it is a symbolic connection, it is the parsed absolute path ).

The directory where the "_ DIR _" file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0)

"_ FUNCTION _" FUNCTION name. The name (case sensitive) of the FUNCTION defined by this constant is returned from PHP 5 ).

The name of the "_ CLASS _" CLASS. Starting from PHP 5, this constant returns the name (case sensitive) when the CLASS is defined ).

The METHOD name of the "_ METHOD _" class, which returns the name (case sensitive) when the METHOD is defined ).

"_ NAMESPACE _" is the name of the current NAMESPACE (case sensitive). This constant is defined during compilation.

 

Of course, when writing a program, the above default constants are not enough. The define () function allows us to define the required constants. See the following example:

1234 <? Php define ('constant', 'Hello! '); Echo CONSTANT;?>
 

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.

Constants are case-sensitive by default, and constant identifiers are always capitalized. This value cannot be changed during script execution.

Differences Between Defining constants and variables:

No dollar sign before a constant ($)
Constants can only be defined using the define () function, but cannot be defined using the value assignment statement.
Constants can be defined and accessed anywhere, regardless of the variable range rules.
Once defined, a constant cannot be redefined or undefined.
The constant value can only be a scalar.

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.