Definition of PHP constants

Source: Internet
Author: User
Tags format define definition constant constant definition echo message mixed

Constants can be understood as values that are invariant. After a constant value is defined, it cannot be changed anywhere else in the script. A constant is made up of English letters, underscores, and numbers, but numbers cannot appear as first letters.
Use the Defaine () function in PHP to define constants, which have the syntax format:
Define (string constant_name, mixed value, case_sensitive = True)
The function has 3 parameters:
Constant_name: Required parameter, constant name, that is, marker
Value: Required arguments, values of constants
Case_sensitive: Optional parameter, specifying whether case sensitive, set to TRUE indicates insensitive

There are two ways to get constant values:
1. Use the constant name to get the value directly;
2. Use the constant () function.
The constant () function and the direct use of the constant name output is the same, but the function can dynamically output different constants, in the use of flexible and convenient.
The syntax format is:
Mixed constant (string constant_name)
The parameter constant_name is the name to get the constant, or a variable that stores the constant name.
If successful, the value of the constant is returned, and the failure indicates that the error message constant is not defined.
To determine whether a constant has been defined using the defined () function. The syntax format for a function is:
BOOL Defained (String constants_name)
Constant_name to get the name of the constant, the existence returns TRUE, otherwise it returns false;
You can use predefined constants in PHP to get information in PHP. such as "_file_", "_line_", "Php_os" and so on.

Cases:
<?php
Define ("message", "PHP constant definition, constant name difference case");
echo message. "    <br/> "; Output constant Message
echo message. "    <br/> "; Output "message", indicating that there is no such constant

Define ("MESSAGE2", "PHP constant definition, constant name not case-sensitive", true);
echo MESSAGE2. "   <br/> "; Output Constants MESSAGE2
echo Message2. "   <br/> "; Output constants

$constant _name = "Message2";
ECHO constant ($constant _name). "   <br/> "; Output Constants MESSAGE2
echo defined ("message"). "         <br/> "; If the definition returns True,echo output displays 1
?>



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.