Wednesday, March 18, 2009

Constants in php

  • Constant can be defined by using the define() function or by using the const keyword outside the class definition as of php 5.3.0.
  • Constant is different from variables in php. To access variable you must use $ prepend but to access constant you should not prepend with a $. With function constant() constant value can also be retrieved.
  • Constants can't be redefined or undefined once they have been set.
  • Unlike variables constants value is not locale. You can access from anywhere within the script.
  • A simple example is below.

<?php
define("myCONSTANT", "This is a constant.");
echo myCONSTANT;
?>

which will print output,
This is a constant.


Related Documents
Working with cookie in php(set, retrieve, delete)
Accessing data from html forms in php
Track error messages on php
Array declaration, assignment and printing in php
Variables in PHP
Magic constants in php

No comments:

Post a Comment