Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Constants

A PHP Constant is a value defined in the script which remains constant and static throughout the execution of the script (or in other words 'runtime'), that is to say they are static values and cannot be changed.

Proper way to write a constant identifier is by using the upper-case alphabets and it should begin directly with an underscore or an alphabet. In order to define a constant you must use define() function but when you are to retrieve the constant value all you have to do is just write the constant name as it is.

Constant are global variables so no matter wherever you are in the script you will still be able to retrieve a constant defined earlier in the script.

Topics to be covered

We will cover the following topics in this lesson

  • PHP Constant Example
  • Are Constant Different from Variables?
  • Rules of a Valid Constant name
  • Magic Constants in PHP
  • Summary

PHP Constant Example

­Below is an example of how you can use constant() function inside your code.

<?php

define("_PATH_", "/home/Fred/");
define("FILENAME", "script.sh");
echo _PATH_,FILENAME;

?>

 

In the example above you can see that programmer has created a constant by the name of _PATH_ and assigned a desired path to the constant and defined filename to constant name FILENAME and when you execute this script, the browser will return the following output:

/home/Fred/script.sh

Are Constant Different from Variables?

Yes, constant are not as same as variables, there are few key differences between variables and constants listed below:

  • A Dollar Sign ($) is not required before a constant, in fact if you will put a dollar sign before a constant it won't recognize it at constant rather it will be considered a variable because a variable MUST being with a $ sign
  • Constants are not defined the same way as the variables are assigned with the values, rather if we want to define a constant we will use `define( function to properly define a constant
  • Constants do not have any scoping rules so a Constant is accessed from anywhere within the PHP code unlike Variables which are divided by groups of scoping rules
  • Constants are static, so once you set them (or define them) there is no way you can undefined or change a constant during script runtime

Rules of a Valid Constant name

A Constant must begin with an alphabet or an underscore, it can have an underscore in the beginning of the name or at the end of the name but both sides of a constant name must not have underscore. It can have a number at the end but should not at the beginning. To elaborate more we shall demonstrate you with the small list of what are examples of valid variables vs invalid variables that are not accepted by PHP

// Below is example Valid constant names
define("FIRST",     "first constant");
define("SECOND2",    "second constant");
define("THIRD_3", "third constant");
define("_FOURTH", "fourth constant");

// And these are Invalid constant names
define("1ST",    "first incorrect constant");
define("__SECOND__", "second incorrect constant");

Magic Constants in PHP

Other than the user defined constants there are predefined constants as well provided by PHP language. There are several of them but only five of them are considered to be magical constants, that is to say they can be changed depending on where they are used.

For instance, the value of __LINE__ depends on the number of line on which you are using it on your script. These magic constants are case-sensitive and list of these constants are as follow

__LINE__

This magic constant will return the current number of line at which this constant is being used

<?php

/* some code here */
echo __LINE__

?>

Output

4

__FILE__

This will return the full path alongside filename of the script file itself in which this constant is used. However if you use this constant as inside an include file then it will return the path and filename of that file.

<?php

echo __FILE__

?>

Output something similar to the following

/var/www/html/foo.php

(assuming we called this constant in filename called foo.php under the path given above)

__FUNCTION__

it will return the name of function name as it was declared (case-sensitivity applied). It's proper implementation is from PHP 5, prior version has some changed behavior compared to PHP 5 and above and prior to 4.3.0 it doesn't exist at all

<?php

function someFunc() {
	echo __FUNCTION__ ;
}

echo someFunc();

?>

Output

someFunc

__CLASS__

This magical constant will return class method name and it will be returned as it was declared (again case-sensitive)

<?php

class someClass
{
    function someFunc() {
    	echo __CLASS__;
    }
}

$obj = new someClass();
$obj->somefunc();

?>

 

Output

someClass

__METHOD__

With this magic, we will get the class' name alongside the function used within that class returned as it was declared (this one is also case-sensitive)

<?php

class someClass
{
    function someFunc() {
    	echo __METHOD__;
    }
}

$obj = new someClass();
$obj->somefunc();

?>

Output

someClass::someFunc

Summary

Constants are variables whose value once defined, cannot be changed. The value is assgined at the time that the contant is defined. The define function is used to define constants. Apart from user defined constants, PHP also comes with built-in constants that we can use to perform common tasks such as get the file path for the file, determine that method name, class name etc.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP - Variable Types

PHP - Strings

Leave A Reply