Php define constant definition and variable difference

Source: Internet
Author: User
Tags constant definition php define php tutorial

A constant must be defined before use; otherwise, an error occurs during program execution. In the php Tutorial, use the define () function to define constants.

1. Syntax format: define ("constant name", "constant value ");

Example: define ("php360", "perfect php ");


Here is an example:
 

The define () function defines a constant.

Constants are similar to variables, except that:

After setting, the constant value cannot be changed.
The constant name does not need to start with the dollar sign ($)
The scope does not affect constant access.
A constant value can only be a string or number.
Syntax
Define (name, value, case_insensitive) parameter description
Name is required. Specifies the name of a constant.
Value is required. Specifies the value of a constant.
Case_insensitive is required. Specifies whether the constant name is case sensitive.

If it is set to true, it is not case sensitive. The default value is false (case sensitive ).
*/


Define ("greeting", "hello world! ");
Echo constant ("greeting ");


/*
Run this code and the hello world will be output in the browser! .

2. Naming rules for constants: start with a letter or underline, followed by any letter, number, or underline.

3. Differences between constants and variables:

(1) there is no $ symbol before the constant, and the variable must start with $.

(2) constants can only be defined using the define () function, but cannot be defined using the value assignment statement.

(3) constants can be defined and accessed anywhere, regardless of the variable range rules.

(4) once a constant is defined, it cannot be redefined or undefined, and the value cannot be changed, and the value of the variable can be changed at any time.

(5) the constant value can only be a scalar, that is, integer, floating point, and string */

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.