Friday 17 October 2014

PHP 5 Constants

Constants are like variables except that once they are defined they cannot be changed or undefined.

PHP Constants

A constant is an identifier (name) for a simple value. The value cannot be changed during the script.
A valid constant name starts with a letter or underscore (no $ sign before the constant name).
Note: Unlike variables, constants are automatically global across the entire script.

Set a PHP Constant

To set a constant, use the define() function - it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.
The example below creates a case-sensitive constant, with the value of "Welcome to Ajay Online Zone":

Example

<?php
define("GREETING", "Welcome to Ajay Online Zone");
echo GREETING;
?>

The example below creates a case-insensitive constant, with the value of "Welcome to Ajay Online Zone":

Example

<?php
define("GREETING", "Welcome to Ajay Online Zone", true);
echo greeting;
?>

No comments:

Post a Comment