Home / Core PHP / PHP 5 Constants

PHP 5 Constants

Constants are like variables except that once they are defined they cannot be changed or undefined.

PHP Constants
A constant is an identifier (name) for a simple value. The value cannot be changed during the script.

A valid constant name starts with a letter or underscore (no $ sign before the constant name).

Note: Unlike variables, constants are automatically global across the entire script.

Set a PHP Constant
To set a constant, use the define() function – it takes three parameters: The first parameter defines the name of the constant, the second parameter defines the value of the constant, and the optional third parameter specifies whether the constant name should be case-insensitive. Default is false.

The example below creates a case-sensitive constant, with the value of “Welcome to vshakya.in!”:

Example

Select Code
<?php
define("GREETING", "Welcome to vshakya.in!");
echo GREETING;
?>


The example below creates a case-insensitive constant, with the value of “Welcome to vshakya.in!”:

Example

Select Code
<?php
define("GREETING", "Welcome to vshakya.in!", true);
echo greeting;
?>

About v.shakya

I am V.Shakya, Freelance PHP Programmer. I like to share my ideas, views and knowledge to all of you who come across my website. I am young, enthusiastic, highly motivated and self disciplined person. I completed my studies in Master of Computer Application and currently giving my technical expertise to one of the Big IT company. I have more than Ten years of experience in vast field of Programming , Designing and Development of websites and various software's.

Check Also

PHP 5 Arrays

An array stores multiple values in one single variable: Example <?php $cars=array("Volvo","BMW","Toyota"); echo "I like …

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.