Welcome to PHPBash. In this PHP Tutorial, we will learn about Basics of PHP Constants. In our Previous Tutorial, we learn about different concepts related to PHP variables. A constant is a name or an identifier with a fixed or permanent value. In Programming, Both constants and variables are used for storing data or values and act as container. Constants are more like variables with one difference that, Constants can not be modified/Changed during execution of program.
PHP Constants
In this, PHP tutorial covers all the topics of Basics of PHP Constants like
1. PHP Constants
2. Using the PHP define() method.
3. Using the PHP const keyword.
4. PHP 7 Constant Arrays
5. PHP Global Constants
6. PHP constant() Function
7. PHP Class constant array
Now let's start this tutorial with understanding PHP Constants and their use.
PHP Constants
A constant is an identifier whose value cannot be changed or modified during the Execution of PHP program or Script. While writing program, sometimes there is need to keep certain value fixed and shouldn't be changed. So here, we use the concept of Constant. Once constant is created, It can be accessed or referred by simply its name. A Constant name should start with letter or underscore.
IMPORTANT:
1.The Dollar Sign ($) is not required before the PHP constant name.
2.PHP Constants are by default global across the entire PHP script or PHP program.
3.PHP Constants can be accessed and print using PHP echo statement.
4.PHP Constants name should be in CAPITAL LETTERS. Its general convention for writing constant names.
5.PHP Constants is case-sensitive by default.
6.PHP Constants name declaration follows same rules Like PHP Variables. Check more here..
Let's look at some Valid and Invalid Constant names:
<?php
// Valid constant names
define("MIN","1");
define("COUNTRY","AMERICA");
define("COLOR", "RED");
// Invalid constant names
define("2TEST","45");
?>
In PHP, there are two ways to define a constant:
1. Using the PHP define() method.
2. Using the PHP const keyword.
1. Using the PHP define() method.
To create a constant, the define() function can be used.
Syntax:
define(name, value, case-insensitive);
In above syntax, There are following 3 parameters:
1. name: It is the name of the constant
2. value: It is the value of the constant
3. case-insensitive: It controls, whether the constant name should be case-insensitive or not.
IMPORTANT: By Default, Constant Names are Case Sensitive. so while printing their values, you need to be careful.
The Following Example will Create a constant with a case-sensitive name:
<?php
define("EXAMPLE", "PHP Constant Example");
echo EXAMPLE;
?>
PHP Constant example using define() |
If we try above example with following code, it will produce error because EXAMPLE is case sensitive.
<?php
define("EXAMPLE", "PHP Constant Example");
echo example;
?>
The Following Example will Create a constant with a case-insensitive name:
<?php
define("EXAMPLE", "PHP Constant Example",true);
echo example;
?>
Now in above example, it will print Constant value as we have defined case-insensitive parameter true.
2. Using the PHP const keyword.
PHP constants can be defined or created with const keyword as well.
IMPORTANT:
1.PHP define() vs PHP const: The PHP const keyword allow to define only scalar constants, i.e. only integers, floats, booleans and strings, where as PHP define() can be used to define array and resource constants as well.
2.In case of PHP const keyword use, the constant name is always case sensitive.
The Following Example will Create a constant with a case-sensitive name using const keyword:
<?php
const EXAMPLE = "PHP Constant Example";
echo EXAMPLE;
?>
PHP 7 Constant Arrays
PHP7 allow to create an Array constant by using the PHP define() function.
Example:
<?php
define("WEEK_DAYS",["SUN","MON","TUE","WEN","THU","FRI","SAT",]);
echo WEEK_DAYS[1]; //it will print MON
echo WEEK_DAYS[2]; //it will print TUE
?>
PHP Global Constants
As described earlier, PHP Constants are by default global and can be used in whole PHP script.
Example:
<?php
define("CITY", "New York");
function city_Print()
{
echo CITY;
}
city_Print();
?>
PHP constant() Function
The PHP constant() return the value of a constant If constant is defined, else NULL. It required constant name as parameter.
Syntax:
<?php
echo constant(CONSTANT_NAME);
?>
Example:
<?php
//DEFINE a constant
define("MIN_VALUE","1");
//PRINT a constant
echo constant("MIN_VALUE");
?>
PHP Class Constant array
In PHP, the constants are categorized in two types like PHP constants and PHP class Constant or array. The constants can be defined using PHP define() or PHP const keyword as described above. In case of PHP class constants, They must be created inside the specific class structure or interface using the const keyword.
<?php
// PHP Class Constant array- PHP Const Keyword
class Dog {
const DESCRIPTION = [
'name' => 'Puppy',
'color' => 'Black',
'weight' => '3 kg'
];
}
// PHP Interface Constant array- PHP Const Keyword
interface IDog {
const DESCRIPTION = [
'name' => 'Puppy',
'color' => 'Black',
'weight' => '3 kg'
];
}
// PHP Interface Constant array- PHP define()
define('DESCRIPTION', [
'name' => 'Puppy',
'color' => 'Black',
'weight' => '3 kg'
]);
echo "Name:".Dog::DESCRIPTION['name']; // Prints Puppy
echo "<br/>Color:".IDog::DESCRIPTION['color']; // Prints Black
echo "<br/>Weight:".DESCRIPTION['weight']; // Prints 3 kg
?>
I would acknowledge you sharing your individual experiences about "PHP constants". If you have any doubt/query related to PHP or Web Technologies, Please let me know in Comment Section.
I will certainly take care that, I should give reply for every single comment on my blog posts. So, let’s fire the discussion up! See you in the comments section below..
Sharing is caring ❤️
Thank you for Visiting PHPBash. Keep Visiting for more Interesting concepts of Php language and web technologies from basics to advanced. As well we will also share some ready-to-use, useful code snippets for beginners to kick start their web development project.
Comments
Post a Comment
Thank you so much.