PHP development basic tutorial magic constants

Overview

PHP provides a large number of predefined constants to any script it runs.

However, many constants are defined by different extension libraries and will only appear when these extension libraries are loaded, or after dynamic loading, or have been included during compilation.

1. There are eight magic constants whose values ​​change as their positions in the code change

For example, the value of __LINE__ It depends on which line it is in the script. These special constants are not case-sensitive

See the following table for details:

17.png

You can’t learn by just looking at it. Let’s see the output results from examples. Bar
Example: The code is as follows

<?php
//__LINE__  文件中当前行号__________________________
echo '这是第 “ '  . __LINE__ . ' ” 行';
echo "<hr/>";
//__FILE__  文件的完整路径和文件名__________________
echo '该文件位于 “ '  . __FILE__ . ' ” ';
echo "<hr/>";
//__DIR__  文件所在的目录___________________________
echo '该文件位于 “ '  . __DIR__ . ' ” ';
echo "<hr/>";
//__LINE__  文件中当前行号__________________________
echo '这是第 “ '  . __LINE__ . ' ” 行';
echo "<hr/>";
//__FUNCTION__  函数名称____________________________
function test() {
    echo  '函数名为:' . __FUNCTION__ ;
}
test();
echo "<hr/>";
//__CLASS__  类的名称_______________________________
class c {
    function _print() {
        echo '类名为:'  . __CLASS__ . "<br>";
        echo  '函数名为:' . __FUNCTION__ ;
    }
}
$t = new c();
$t->_print();
echo "<hr/>";
?>

Note: Please understand the following magic constants first, and there will be details in subsequent chapters

Note: Pay attention to the php version problem. Inappropriate versions will report errors


Continuing Learning
||
<?php //__LINE__ 文件中当前行号__________________________ echo '这是第 “ ' . __LINE__ . ' ” 行'; echo "<hr/>"; //__FILE__ 文件的完整路径和文件名__________________ echo '该文件位于 “ ' . __FILE__ . ' ” '; echo "<hr/>"; //__DIR__ 文件所在的目录___________________________ echo '该文件位于 “ ' . __DIR__ . ' ” '; echo "<hr/>"; //__LINE__ 文件中当前行号__________________________ echo '这是第 “ ' . __LINE__ . ' ” 行'; echo "<hr/>"; //__FUNCTION__ 函数名称____________________________ function test() { echo '函数名为:' . __FUNCTION__ ; } test(); echo "<hr/>"; //__CLASS__ 类的名称_______________________________ class c { function _print() { echo '类名为:' . __CLASS__ . "<br>"; echo '函数名为:' . __FUNCTION__ ; } } $t = new c(); $t->_print(); echo "<hr/>"; ?>
submitReset Code