Php defines constants define () and common variables

Source: Internet
Author: User
Tags php define
1. define the constant define (quot; constantquot;, quot; helloworld. quot;); constants can only contain scalar data (boolean, integer, floa... 1. define the constant define ("constant", "hello world. ");

Constants can only contain scalar data (boolean, integer, float, and string ).

When calling a constant, you only need to obtain the constant value with a simple name, instead of adding the "$" symbol, such as echo constant;

Note:Constants and (global) variables are in different namespaces, which means that for example, true and $ true are different.

Definition and usage

The define () function defines a constant, which is similar to a variable. The difference is:

After the value is set, the constant value cannot be changed. the constant name does not need the dollar sign ($) at the beginning. the scope does not affect constant access. the constant value can only be a string or number.

Syntax

Define (name, value, case_insensitive) parameter description

Name is required. it specifies the name of a constant.

Value is required, specifying the value of a constant.

Case_insensitive is required to specify 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 ).

Example 1: define a case-sensitive constant:

 

Example 2: define a case-insensitive constant:

 

2. common variable $ a = "hello ";

3. variable (using two dollar signs ($ ))

$ A = "world ";

Both variables are defined:

$ A's content is "hello" and $ hello's content is "world ".

Therefore, it can be expressed:

Echo "$ a $ {$ a}"; or echo "$ a $ hello"; both outputs: hello world

To use variables in an array, you must solve an ambiguous problem. this is when you write $ a [1, the parser needs to know whether to use $ a [1] as a variable or $ a as a variable and retrieve the value of [1] in the variable, the syntax for solving this problem is to use $ {$ a [1]} for the first case and $ {$ a} [1] for the second case.

Permanent address:

Reprint at will ~ Please bring the tutorial URL ^

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.