Friday, March 13, 2015

what are PHP Constants ?

A constant acts as an identifier (name) for a simple value. Valid constant name starts with a  letter or underscore, followed by any numbers or  letters or underscores. Constant name cannot start with $ sign.


Uses of PHP constant:

Constants are accessible from anywhere without regard to scope.
Constants acts like global and has a global scope, and are unchangeable they are defined once and can be called or used many times. Therefore, it is convenient using constants as it reduces the time for programming.
Syntax:

define(name, value, case-insensitive)

Example:

PHP Script (constant.php)
<?php
define("const1", "<h3>PHP Tutorial</h3>");
/*Case insensitive constant name*/
define("CONST2", "PHP - Hypertext Preprocessor", true);
echo "<h2>Example of Constant</h2>";
echo const1;
echo const2;
?>



No comments:

Post a Comment