Constants php

Syntax

You can define a constant by using the define()-function or by using the const keyword outside a class definition as of PHP 5.3.0. Once a constant is defined, it can never be changed or undefined.

Note: Constants and (global) variables are in a different namespace. This implies that for example TRUE and $TRUE are generally different.

Example #1 Defining Constants

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


Example #2 Defining Constants using the const keyword

<?php
// Works as of PHP 5.3.0
const CONSTANT 'Hello World';

echo CONSTANT;
?>

Leave a comment