Declaration and use of PHP constants

Source: Internet
Author: User

Constants can be understood as the amount of constant values. Constants are defined and cannot be changed anywhere else in the script, a constant by the English alphabet. Underscores, and numbers, but numbers cannot be the first letter of a constant.

Use the # define () function in PHP to define constants, the syntax for the function is as follows:

Define (string constant_name,mixed value,case_sensitive=true)


There are two ways to get the value of a constant. One method is to get the value directly using the name of the constant, and the other is to take advantage of the constant () function, and the other is to use the constant () function and output it directly using the constant name. However, the function can dynamically output the values of different constants, which is more flexible and convenient.

The first gets its value directly with the name of the constant

<?

PHP define ("test", "I am jack!"); echo test;?>


The results of the execution are as follows:


Another way to get a constant value code using the constant () function is as follows:

<?php define ("test", "I am jack!"); $JACKSTR = "Test"; ECHO constant ($JACKSTR);?>

The results of the execution are as follows:


Next, let's talk about uppercase and lowercase sensitive issues. Just now the declaration of the constant is set in uppercase and lowercase sensitive, we use the code to verify the see is not, in addition to enhance my memory


The code is as follows:

<?php define ("test", "I am jack!", true); echo Test;echo "\ n"; echo Test;echo "\ n"; echo Test;echo "\ n";?>

Let's look at the results of the output again.



You can find words that are not sensitive to uppercase and lowercase. Even if there is a difference between uppercase and lowercase, the output will still be the same.


Defining constants in advance is no different from using our custom constants. Common pre-defined constants such as the following:














Declaration and use of PHP constants

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.