Php Flow » Php

Defining a Constant in PHP

Constant is important variable in any technology. When you are creating any application based on any Technology, You need to define some constant variable which will not change in throughout application.
A constant Stands for “A constant is an identifier (name) for a simple value”.

Main difference between constant and Variable

The main difference between a constant and a variable is variable may be changed but constant cannot be change in application.

Main feature of Constant

1- Constant cannot be change.
2- Define in single time and used multiple time based on requirement.
3- By default case sensitive.
4- Constant name will be in capital letter.


Here is a basic example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
define("URL", "http://phpflow.com");
define("MESSAGE", "Hello Parvez");

echo URL
//output will http://phpflow.com
echo MESSAGE
// output Hello Parvez
echo URL." is developed by ".MESSAGE // outputs http://phpflow.com is developed by Parvez
?>

[/php]

So at the time of using constant must be remember that constants are case sensitive, so below wouldn’t work

[code type="php"]
<?php
echo URL;
?>

To solve case sensitive constant problem in php,PHP define() function has third parameter to control case problem.
To make it case-insensitive, all you need to do is set that third parameter to true.

1
2
3
4
5
6
7
8
9
10
11
<?php
define("URL", "http://phpflow.com", true);

echo SITE // outputs http://phpflow.com
echo Site // outputs http://phpflow.com
?>
Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook.

Random Posts

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>