PHP Constants

Conversely to variables, constants are meant for defining immutable values. Constants can be accessed for any scope within a script; however, they can only contain scalar values. Constant names, like variables, are case-sensitive; they also follow the same naming requirements, with the exception of the leading $. It is considered best practice to define constants using only upper-case names.

Here’s an example of constants at work:

<?php
define('EMAIL', 'abc@xyz.com'); // Valid name
echo EMAIL; // Displays 'abc@xyz.com'
define('USE_XML', true);
if(USE_XML) { } // Evaluates to true
define('1CONSTANT', ’some value’); // Invalid name
?>

Next Topic | Previous Topic

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *