CodesJava

Easy learning with example program codes

PHP Magic Constants

PHP Magic Constants are same as PHP Constants. The word Magic is attached only cause of unique feature of PHP Magic Constants to change their values on the basis of their use.

PHP Magic Constants are case insensitive and they starts and ends with a double underscore (__).

 

MAGIC CONSTANTUSAGE
__LINE__Used to represent current line number where it is used.
__FILE__Used to represent full path and name of the file.
__CLASS__Used to represent the function name where it is used.
__METHOD__Used to represent the name of the class method where it is used.
__DIR__Used to represent full directory path of the file.
__NAMESPACE__Used to represent the name of the current namespace.
__FUNCTION__Used to represents the function name where it is used.
__TRAIT__Used to represent the trait name where it is used.

Example:

<!DOCTYPE html>
<html>
<body>
 
<?php
 
echo “<p>Example for __LINE__</p>;  
echo __LINE__ . "<br><br>";
 
echo “<p>Example for __FILE__</p>;  
echo __FILE__ . "<br><br>";
 
echo “<p>Example for __DIR__</p>;  
echo __DIR__ . "<br><br>";
 
?>
 
</body>
</html>
Please follow and like us:
Posted in PHP   








Copyright © 2019 CodesJava DMCA.com Protection Status