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.
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 (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;
define('WEBSITE',qoify.com);
echo WEBSITE;
//output : qoify.com
const WEBSITE='qoify.com'; echo WEBSITE; //output : qoify.com
define('FRUITS', array(
'APPLE',
'ORANGE',
'BANANA'
));
echo FRUITS[1];
//output : ORANGE
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
| __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 |