Monday, 10 August 2015

PHP Constants :- 
  • A constant is a name or an identifier for a simple value.
  • A constant value cannot change during the execution of the script.
  • By default, a constant is case-sensitive.
  • Constant identifiers are always uppercase.
  • A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
  • If we have defined a constant, it can never be changed or undefined.
  • To define a constant you have to use define() function.
  • To retrieve the value of a constant, we have to simply specifying its name.


constant() function :-
  • By its name, we can understand this function will return the value of the constant.
  • This is useful when we want to retrieve value of a constant, but we do not know its name.
  • It is stored in a variable or returned by a function.
  • Only scalar data like boolean, integer, float and string can be contained in constants.


Differences between variables and constants :- 
  • 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.
  • There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.

No comments:

Post a Comment