How to use constants in PHP ?

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 define("NEXTLINE","\n"); 
echo "Hello ".NEXTLINE."world"; 
?>

Output:

Hello
world

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:

535 total views, 1 views today

Leave a Reply

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