Constants are used to define immutable variables. Suppose if you have aa scenario such that where the value of variable shouldn’t change throughout application life cycle, you should define that variable as constant.
How to define a constant variable?
Constants are defined using ‘define’ function.
Example
define("__PI__", 3.14);
__PI__ is the name of the constant and it has value 3.14.
Constant naming conventions
a. Constant name must starts with a letter or underscore, followed by any number of letters, numbers or underscores with one exception.
b. By convention, constant variable name is always uppercase.
constant_demo.php
#!/usr/bin/php
<?php
define("__PI__", 3.14);
echo __PI__;
?>
Output
$./constant_demo.php
3.14
Note
$ prefix is not required for constant names.
Previous Next Home
No comments:
Post a Comment