Php defines constants define () and common variables

Source: Internet
Author: User
Tags php tutorial

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. This means that for example, true and $ true are different.

Definition and usage
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 ).
 
Example
Example 1
Defines a case-sensitive constant:

<? Php Tutorial
Define ("greeting", "hello world! ");
Echo constant ("greeting ");
?> Output:

Hello world! Example 2
Defines a case-insensitive constant:

<? Php
Define ("greeting", "hello world! ", True );
Echo constant ("greeting ");
?> Output:

Hello world!

2. Common variable $ a = "hello ";

3. Variable 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. When writing $ a [1], the parser needs to know that $ a [1] is used as a variable, you still want $ 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.

 

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.