Hope this article find you well.
Today we will learn about the PHP constants.
So read the article till the end and i will make sure you know everything about PHP constants.
Before i proceed, i would request you to do SUBSCRIBE to ShoutMe360.com so that you can get regular updates.
So lets start…..
What is PHP Constant?
A constant is just like a variable which can hold value.
Then what is difference between a Variable and a Constant?
There is one and only difference is while we can change the value of variable, on the other hand, once the constant is defined, it can not be changed.
define(name, value, case-insensitive)
name: Specifies the name of the constant.
value: Specifies the value of the constant.
case-insensitive: Specifies whether the constant name should be case-insensitive. Default is false.
Valid and invalid constant names
// Valid constant names
define(“ONE”, “first thing”);
define(“TWO2”, “second thing”);
define(“THREE_3”, “third thing”);
// Invalid constant names
define(“2TWO”, “second thing”);
define(“__THREE__”, “third value”);
Quick Overview about Constants-
– Once defined a constant, it can not be changed or undefined.
– Constants are Global means they can be used across the entire script.
– A constant is case-sensitive.
– A constant identifiers are always uppercase.
– A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
– To define a constant define() function is to be used and to get the value of a constant, simply specifying its name.
– No need to have constant with a $.
– Function constant() also can be used to read a constant’s value if you need to access the constant’s name dynamically.
As indicated by the name, this function will return the value of the constant.
This function is very useful when you need to access the value of constant but you do not know its name.
echo constant(“SHOUTME”); // same thing as the previous line
Differences between constants and variables are
Variable names starts with a dollar sign($) while no need to write a dollar sign($) before constant.
Constants cannot be defined by simple assignment like variable, need to use define() function.
Constants may be defined and accessed anywhere across the script. No scope rules implies on constants.
Once the Constants are defined, may not be redefined or undefined.
PHP Magic constants
PHP provides a large number of predefined constants to any script which it runs.
There are five magical constants that change depending on where they are used. These special constants are case-insensitive and are as follows −
The current line number of the file.
The full path and filename of the file. If used inside an include, the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances.
The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lower-cased.
The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).
Hope this explained you enough about the PHP Constants.
This is Lalit signing off for today.
If you like this post please give a thumbs up and keep it sharing.
Hey don’t forget to SUBSCRIBE please so that you can get regular updates.