Go deep into the usage of php define () and defined () Functions

Source: Internet
Author: User

The define () function defines a constant.
The define () function defines a constant.
Constants are much like variables, except t for the following differences:
Constants [constant] and variables [variable] have many similarities, so they are easy to be confused. Next, let's list the differences between constants [constant] and variables [variable:

• A constant's value cannot be changed after it is set
A constant value cannot be changed after it is specified;
• Constant names do not need a leading dollar sign ($)
When you set a constant, you do not need to add the "$" symbol before it;
• Constants can be accessed regardless of scope
Constants can be accessed by all fields;
• Constant values can only be strings and numbers
The constant value can only be "string [string]" and "number [number]";

Syntax
Syntax

Copy codeThe Code is as follows: define (name, value, case_insensitive)


Parameter
Parameters
Description
Description
Name Required. Specifies the name of the constant
Required parameter. Constant name
Value Required. Specifies the value of the constant
Required parameter. Constant Value
Case_insensitive Optional. Specifies whether the constant name shocould be case-insensitive. If set to TRUE, the constant will be case-insensitive. Default is FALSE (case-sensitive)
Optional. Specifies whether the constant name is case-insensitive [case-insensitive]. If it is set to True, It is case-insensitive. If it is set to False, it is case-sensitive. The default value is False.


Example 1
Case 1
Define a case-sensitive constant:
Specify a constant (case sensitive ):

Copy codeThe Code is as follows: <? Phpdefine ("GREETING", "Hello you! How are you today? "); Echo constant (" GREETING ");?>

The output of the code above will be:
The above code will output the following results:Copy codeThe Code is as follows: Hello you! How are you today?

Example 2
Case 2
Define a case-insensitive constant:
Specify a constant (Case Insensitive ):Copy codeThe Code is as follows: <? Phpdefine ("GREETING", "Hello you! How are you today? ", TRUE); echo constant (" greeting ");?>

The output of the code above will be:
The above code will output the following results:Copy codeThe Code is as follows: Hello you! How are you today?

The defined () function checks whether a constant exists.
The role of the defined () function is to check whether a constant exists.

Returns TRUE if the constant exists, or FALSE otherwise.
If the constant exists, True is returned. If the constant does not exist, False is returned.

Syntax
Syntax

Copy codeThe Code is as follows: defined (name)


Parameter
Parameters
Description
Description
Name Required. Specifies the name of the constant to check
Required parameter. Constant Object Name

Example
Case

Copy codeThe Code is as follows: <? Phpdefine ("GREETING", "Hello you! How are you today? "); Echo defined (" GREETING ");?>

The output of the code above will be:
The above code will output the following results:Copy codeThe Code is as follows: 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.