Magic Constants In PHP



   <?php
  
Magic Constants In PHP
``````````````````````````````````
  All the variable must be defined with prefix $ and function with suffix () parenthesis.
  but Constants are defined Generally in UPPERCASE and with not prefix and suffix
  
  How to Make Constants In PHP
``````````````````````
define('CONSTANT_NAME','value');

How to use:
echo CONSTANT_NAME;

All Constants are Global can be Accessed anywhere
Scope of Context: Global
<?php

#WAP to define your own User defined Constants.

define('GRAVITY',9.8);
echo GRAVITY;
function test(){
echo GRAVITY; //Global Scope
}

#calling of test
test();
define('GRAVITY',10); //cannot be Resigned

Magic Constants are predefined Constant in PHP and Capable to provinding
runtime values
These Magic Constants have __ as prefix and __ as suffix

Few of them
1. __FILE__ : give the entire path for a file
Example : file name header.php
echo __FILE__
C:/xampp/htdocs/path/to/folder/layout/header.php
2. __DIR__ : give the entire path for a file parent folder
echo __DIR__
C:/xampp/htdocs/path/to/folder/layout/
3. __NAMESPACE__ : Give the name_space name
4. __CLASS__ : Give the Current Class Name
5. __LINE__ : Give the Line Number where execution is Done
6. __FUNCTION__ : Give the function of being Called
7. __METHOD__ : Give the method name of a class
8. __TRAIT__ : To give the Trait Name(Multiple Inheritance Expection)

 __NAMESPACE__, __CLASS__, __METHOD__ will be discussed on chapter OOPS


 


Magic Constants In PHP