php constants

Constants variables are used to store information which is like variables. The main difference between constants and variables is that constant value cannot be changed in the running program, where in variables value can change.

Constants Syntax :

mixed constant ( string $name )

Return the value of the constant indicated by name.

if you need to take the value of a constant , will not know its name. example it is stored in a variable or returned by a function.

The boolean , integer , float and string can be contained in constants.which is possible to define constants as a resource.

Constants Example :


<?php
define("CONSTANT", "Hello Tutorialsscripts.");
echo CONSTANT; // outputs "Tutorialsscripts."
echo Constant; // outputs "Constant" and issues a notice.
?>

The above example first line has difined Constant. second line produce out put 'Tutorialsscripts'.Third line will output produce notice.

Here some differences between constants and variables:

  • Constants will only be defined using the define() function, not by simple assignment;
  • Constants do not have a dollar sign $ before them;
  • Constants may only evaluate to scalar values
  • Constants will not be redefined or undefined once they have been set; and
  • Constants will be defined and accessed anywhere without regard to variable scoping rules;




Content