Thursday, 7 March 2013

Define Constants in Php | PHP Tutorials for Beginners

What is Constant:-
A constant is an identifier for a any value. Constant is case-sensitiv and constant identifiers are always uppercase. A constant name always starts with a letter or underscore. A constant value cannot change during the execution of the script. If we have defined a constant, it can never be changed. 

How to define Constant:-
To define a constant we have to use define() function and to retrieve the value of a constant, we have to use its name. Unlike  variables constants do not have $ sign.

define("PAGE_TITLE", "PHP Tutorials for Beginners");
define("DESC", "Php constant tutorial example");

echo PAGE_TITLE; // Output:- PHP Tutorials for Beginners
echo DESC; // Output:- Php constant tutorial example

Differences between constants and variables are:-
There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.
Constants cannot be defined by simple assignment, they may only be defined using the define() function.
Constants may be defined and accessed anywhere without regard to variable scoping rules.
Once the Constants have been set, may not be redefined or undefined.

Hope this php tutorial is useful for you. Keep following for more examples.