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 CONSTANT USAGE
__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>
Posted in PHP   
Sign Up/ Sign In
Ask a Question


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki