Home  >  Article  >  Backend Development  >  Playing with php constants_PHP tutorial

Playing with php constants_PHP tutorial

WBOY
WBOYOriginal
2016-07-14 10:08:121175browse

Naming rules for constants

Naming: Same naming rules as variables
Method: define() function
Format: define('constant name','specific value');
Example: define('PI',3.14);
Comparison of constants and variables
Same:
1: The naming rules are the same (but generally in uppercase letters)
Different:
1: Don’t use $ when referencing constants, use names directly.
2: Constants can only be defined using define, not assignment statements
3: Once a constant is defined, it cannot be redefined or undefined
4: The value of a constant can only be a scalar (can only be integer, floating point, Boolean, character, NULL)
[php]
//Define constants
define('pi',3.14);
//Calculate the area of ​​the park
$r=3;
echo pi*$r*$r;
//Result: 28.26
//After a constant is defined, it cannot be redefined
define('pi',3.23);
//Result: Notice: Constant pi already defined in C:wampwww
//Constant cannot be reassigned
pi=100;
echo pi;
//Result: Parse error: syntax error, unexpected '=' in C:wampwww
//There will be grammatical errors
$PI=10;
function text(){
echo $PI;//The function has scope. This $PI variable is inside the function and is not the same as the $PI outside.
echo pi;//Constant is defined once and can be used anywhere
}
//Result: Notice: Undefined variable: PI in C:wampwww
text();
?>
How to determine whether a constant is defined?
define defines constants
defined judgment constant
[php]
if(defined('PI')){
echo 'PI constant has been defined.
';
}else{
echo 'PI constant is undefined, let me define it
';
define('PI',33322);
}
echo PI;
?>
[php]
Result: PI constant is undefined, let me define it
33322
http://www.bkjia.com/PHPjc/477800.html

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/477800.htmlTechArticleNaming rules for constants: The same naming rules as variables Method: define() Function format: define(constant name , specific value); Example: define(PI,3.14); The comparison between constants and variables is the same: 1: Naming...
Statement:
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
Previous article:DedeHttpDown PHP remote download web page class, enhanced version modified on 2013-1-17_PHP tutorialNext article:DedeHttpDown PHP remote download web page class, enhanced version modified on 2013-1-17_PHP tutorial

Related articles

See more