Examples of differences and usages of define and defined functions in PHP

Source: Internet
Author: User
Define

define is used to define a constant, which is also global in scope. You can access it anywhere in the script without the scope of the control. Once a constant is defined, it can no longer be changed or undefined.

There are a lot of similarities between constants and variables, so it's easy to confuse; Let's look at the differences between constants and variables:

    1. A constant value cannot be changed after it has been specified

    2. When you set a constant, you do not need to precede the "$" symbol

    3. Constants can be accessed by domains of all scopes

    4. The value of a constant can only be "strings [string]" and "numbers [number]"

The syntax format for define is as follows:

Define (name,value,case_insensitive)
parameter
name
value
case_insensitive

Specify a constant (case-sensitive) with the sample code as follows:

<?phpdefine ("greeting", "How is you today?"); ECHO constant ("greeting");? >

Output the following result:

How is you today?

Specify a constant (case-insensitive) with the sample code as follows:

<?phpdefine ("greeting", "How is you today?", TRUE); Echo constant ("greeting"); >

Output the following result:

How is you today?

Defined

The defined is used to detect that a constant is undefined and returns true if the constant exists, otherwise false.

The syntax format for defined is as follows:

Defined (name)
Parameters Description
Name Necessary parameters. Specifies the name of a constant object

The sample code is as follows:

<?phpdefine ("greeting", "How is you today?"); echo defined ("greeting");? >

Output the following results:1

The defined tag of the thinkphp template engine is used to determine whether a constant has been defined.

Defined tag usage is as follows:

<present name= "Constants" > What to Output </present>

Use the defined tag in the template/tpl/default/index/display.html as follows:

<defined name= "site_name" > website name:{*site_name}</defined>

If the judgment is not defined, you can use:

<notdefined name= "Site_Name" >{*site_name} does not exist live undefined </notdefined>

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.