PHP Define Constants

Source: Internet
Author: User
Tags php define
A constant is an identifier (first name) of a simple value. As the name implies, the value cannot be changed during script execution (except for the so-called magic constants, they are not constants). Changshime is considered to be case sensitive. By convention constant identifiers are always capitalized.


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. This is expressed in regular expressions: [a-za-z_\x7f-\xff][a-za-z0-9_\x7f-\xff]*


Valid and illegal constant names


A valid constant name
Define ("W3sky", "something");
Define ("W3sky2", "something Else");
Define ("W3sky_bar", "Something More")

Illegal constant name
Define ("2w3sky", "something");

This was valid, but should was avoided:
PHP provide a magical constant
That'll break your script
Define ("__w3sky__", "something");
?>


Note: Here, the letters are a-z,a-z, as well as ASCII characters from 127 to 255 (0X7F-0XFF).


As with Superglobals, the range of constants is global. You can access constants anywhere in the script without the scope of the control. For more information on the role, please read the variable range in the manual.


Grammar

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

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

You can simply get the value of the constant by specifying its name, and do not precede the constant with the $ symbol. If the constant name is dynamic, you can also use the function

Constant () to read the value of the constant. A list of all defined constants can be obtained with get_defined_constants ().

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

If an undefined constant is used, PHP assumes that it wants the name of the constant itself, as if it were called with a string (CONSTANT corresponds to "CONSTANT"). An error of E_notice level is issued at this time. See why $w 3sky[bar] is wrong in the manual (unless you define bar as a constant in advance with define ()). If you only want to check if a constant is defined, use the defined () function.

Constants and variables are different:
* Constant preceded by no dollar sign ($);
* 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.

Defining constants

Define ("CONSTANT", "Hello World");
Echo CONSTANT; Outputs "Hello world."
Echo Constant; Outputs "Constant" and issues a notice.
?>

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