PHP entry-based constant type learning Notes _php Tutorial

Source: Internet
Author: User
Tags php define variable scope
This article to give you a summary of the PHP constant type of some usage, this is a friend's study notes, I would like to share with you classmates.

Once a constant in PHP is defined, it can no longer be changed or undefined, constants have predefined constants (intrinsic constants), constants defined by PHP, and of course, constants can be customized.

Here are a few of the predefined constants: (See the PHP Manual for a full pre-defined constant)

True, the Constant is Truth (true), True (set).

False, the constant is pseudo (false) and false (not valid).

Php_version, the constant is the version number of the current PHP program, such as "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 represents the path delimiter, is "" on windowns, and "/" on Linux.

Here are a few of the PHP default constants:

E_error, this constant refers to the most recent error.

E_warning, this constant refers to the nearest warning.

E_parse, this constant is a potential problem for parsing syntax.

E_notice, this constant is unusual but not necessarily the wrong place. For example, 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 extensions, and are only present when the extensions are loaded, either dynamically after loading, or they are included at compile time.

PHP has several constants called "Magic constants" whose values change as they change position in the code. For example, the value of "__line__" depends on the row it is in the script to determine. These special constants are case-insensitive (but generally uppercase), as follows

The line number in the "__line__" file where the constant is located.

The full path and file name of the "__file__" file. If used in the included file, returns the file name that is included. The __file__ also contains an absolute path (if it is a symbolic connection, the resolved absolute path).

The directory where the "__dir__" file resides. If used in the included file, returns the directory where the included files are located. It is equivalent to DirName (__file__). Unless it is a root directory, the name in the directory does not include the trailing slash. (New in PHP 5.3.0)

"__function__" function name, since PHP 5 This constant returns the name of the function when it is defined (case-sensitive).

The name of the "__class__" class, since PHP 5 This constant returns the name of the class when it is defined (case-sensitive).

The method name of the "__method__" class that returns the name (case-sensitive) when the method is defined.

"__namespace__" The name of the current namespace (case sensitive), which is defined at compile time.

Of course, the above default constants are not sufficient when writing programs. The function of define () allows us to define the constants we need ourselves. See the following example:

1234

The constant name and any other PHP tags follow the same naming conventions. A valid constant name begins with a letter or underscore followed by any letter, number, or underscore.

Changshime is considered case-sensitive, and by convention constant identifiers are always uppercase, and the value cannot be changed during script execution.

To define the difference between a constant and a defined variable:

No dollar sign ($) before constant
Constants can only be defined with the Define () function, not through assignment statements
Constants can be defined and accessed anywhere without regard to the rules of variable scope
Constants cannot be redefined or undefined once defined
The value of a constant can only be scalar

http://www.bkjia.com/PHPjc/628641.html www.bkjia.com true http://www.bkjia.com/PHPjc/628641.html techarticle This article to give you a summary of the PHP constant type of some usage, this is a friend's study notes, I would like to share with you classmates. Once a constant in PHP is defined, ...

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