PHP Beginner's ...LOGIN

PHP Beginner's Guide to Magic Variables

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. __LINE__

The current line number in the file

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码 
	echo '这是第 “ '  . __LINE__ . ' ” 行';    //查看第几行
?>

2. __FILE__


The full path and file name of the file. If used within an included file, returns the name of the included file.

Since PHP 4.0.2, __FILE__ always contains an absolute path (or the resolved absolute path if it is a symbolic link), while versions before that sometimes contained a relative path

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码 
	echo '该文件位于 " '  . __FILE__ . ' " ';	//查看路径
?>	


3. __DIR__


The directory where the file is located. If used within an included file, returns the directory where the included file is located.

It is equivalent to dirname(__FILE__). Directory names do not include the trailing slash unless they are the root directory. (New in PHP 5.3.0)

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码 
	echo '该文件位于 " '  . __DIR__ . ' " ';
?>


##4.

__FUNCTION__

<?php
    function test() {
    	echo  '函数名为:' . __FUNCTION__ ;
    }
    test();
?>

5.

__CLASS__


The name of the class (newly added in PHP 4.3.0). Since PHP 5 this constant returns the name of the class when it was defined (case sensitive).

In PHP 4 this value is always lowercase. The class name includes the scope in which it is declared (e.g. Foo\Bar). Note that since PHP 5.4 __CLASS__ also works for traits. When used in a trait method, __CLASS__ is the name of the class that calls the trait method

<?php
    	class test {
		function _print() {
			echo '类名为:'  . __CLASS__ . "<br>";
			echo  '函数名为:' . __FUNCTION__ ;
		}
	}
	$t = new test();
	$t->_print();
?>


6.

__METHOD__


Returns the name of the method when it was defined (case sensitive)

	<?php
	    function test() {
		echo  '函数名为:' . __METHOD__ ;
	    }
	    test();
	?>


Next Section

<?php header("Content-type: text/html; charset=utf-8");//设置编码 echo '这是第 “ ' . __LINE__ . ' ” 行'; //查看第几行 ?>
submitReset Code
ChapterCourseware