What is Constant in PHP
Constant is a name for a fixed value. Constants are such as variables and it's accept that once they are defined they cannot be changed.
There are two ways to define a constant variable in PHP.
1. Using the define() method.
2. Using the const keyword.
Create constants in PHP
define(name, value, case-insensitive)
Create a constant with a case-sensitive name:
<?php // Defining constant define("SITE_URL", "https://howtowebcode.com/"); // Using constant echo 'Thank you for visiting - ' . SITE_URL; ?>
Create a constant with a case-insensitive name:
<?php // Defining constant define("SITE_URL", "https://howtowebcode.com/", true); // Using constant echo 'Thank you for visiting - ' . site_url; ?>
Using the const Keyword
We can also create constants in PHP using the const keyword. But we can only use the const keyword to define the constants. only integers, booleans and strings, floats, while define() can be used to define array and resource constants as well although they are not used oftenly.
<?php const SITE_URL = "https://howtowebcode.com/"; echo SITE_URL; ?>