How to use constants in PHP ?

How to use constants in PHP ?

Constants in php should not be declared/defined with $. Constants in any programming language will be used to store the predefined values.

Eg:
PI -> 3.14
NEXTLINE -> \n etc..,

Sample PHP program with constants:
[php gutter=”0″]
<?php define("NEXTLINE","\n");
echo "Hello ".NEXTLINE."world";
?>
[/php]

Output:

[plain gutter=”false”]
Hello
world
[/plain]

Note:

Here \n declared as a constant and used in the program without $.
Name of a constant can be a small letter also, but as per naming conventions it is recommended to use only capital letters to name the constants.

PHP Recommended Books:

Leave a Reply