PHP Constants

A constant is like variables whoose value cannot be changed. A constant value cannot change during the execution of the script. a constant is case-sensitive By default.

PHP Constants

A constant identifier are always uppercase By convention. A valid constant name starts with a letter or underscore(without $ sign)

In PHP, there are two ways to define a constant:
- define() method.
- const keyword.

Create a PHP Constant By define() method

define (name,value, case-insensitive );

Parameters: name: Name of the constant value: Value of the constant case-insensitive: If set to TRUE, the constant will be defined case-insensitive. default is case-sensitive;

Example

define('WEBSITE',qoify.com);
echo WEBSITE;
//output : qoify.com

Defining Constants By const keyword

const WEBSITE='qoify.com';
echo WEBSITE;
//output : qoify.com

PHP Constant Arrays

PHP constant array works as of PHP 7 version , create a constant Array by the define() function.
define('FRUITS', array(
    'APPLE',
    'ORANGE',
    'BANANA'
));
echo FRUITS[1];
//output : ORANGE

PHP Constant is Global

The scope of a constant is global, we can access anywhere in script.
define('FRIEND_NAME','Vipin Katiyar');
function name()
{
	echo 'My friend name is '.FRIEND_NAME;
}
name();
//output: My friend name is Vipin Katiyar
Difference between define and const

PHP Magic constants

__LINE__ Get the current line number of the file.
__FILE__ Full path and filename of the file
__DIR__ Directory of the current file
__FUNCTION__ Function name
__CLASS__ Class name
__TRAIT__ Get the current trait name
__METHOD__ Get the method name of a class
__NAMESPACE__ The name of the current namespace
ClassName::class Get the fully qualified class name resolution