Home > Backend Development > PHP Tutorial > The difference between php define constant definition and variable_PHP tutorial

The difference between php define constant definition and variable_PHP tutorial

WBOY
Release: 2016-07-13 10:54:24
Original
1751 people have browsed it

Constants must be defined before use, otherwise an error will occur during program execution. Use define() function in php tutorial to define constants.

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

For example: define("php360","Perfect php");


Here’s an example:

define() function defines a constant.

Constants are similar to variables, except that:

After setting, the value of the constant cannot be changed
Constant names do not require a leading dollar sign ($)
Scope does not affect access to constants
Constant values ​​can only be strings or numbers
Grammar
define(name,value,case_insensitive) parameter description
name required. Specifies the name of the constant.
value required. Specifies the value of the constant.
case_insensitive required. Specifies whether constant names are case-sensitive.

If set to true, it will not be case sensitive. Default is false (case sensitive).
*/


define("greeting","hello world!");
echo constant("greeting");


/*
Running this code will output the result hello world! to the browser.

2. Rules for constant naming: start with a letter or underscore, and can be followed by any letter, number, or underscore.

3. The difference between constants and variables:

(1) There is no $ sign in front of the constant, but the variable must start with the $ sign.

(2) Constants can only be defined using the define() function and cannot be defined through assignment statements.

(3) Constants can be defined and accessed anywhere regardless of the rules of variable scope.

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

(5) The value of a constant can only be a scalar, that is, three types: integer, floating point, and string*/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632320.htmlTechArticleConstants must be defined before use, otherwise errors will occur during program execution. Use define() function in php tutorial to define constants. 1. Syntax format: define (constant name, constant value); For example:...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template