今天看到一个魔术变量,是以前没见过的,__DIR__,我查了查,发现原来是php5.3新增的,顺便举几个例子,解释一下php的魔术变量
1:__FILE__
文件的完整路径和文件名。如果用在被包含文件中,则返回被包含的文件名。自 PHP 4.0.2 起,__FILE__ 总是包含一个绝对路径(如果是符号连接,则是解析后的绝对路径),而在此之前的版本有时会包含一个相对路径。
这个变量,我用的是最多的,估计也是大家用的最多的。
web服务器都会指定一个documentroot的,但是不同的服务器,设置的documentroot有可能是不同的,在这种情况下,把一个网站从一个服务器搬家到另一个服务器,这样就有可能因为路径的不同,造成网站跑不起来。
<?php /** 在你的公用的配置文件中,来设置你的根目录,这样就不用担心经常搬家了。 */ define('ROOT_PATH', dirname(__FILE__) . DIRECTORY_SEPARATOR); echo ROOT_PATH; echo "<br>"; echo __FILE__; echo "<br>"; echo dirname(__FILE__); echo "<br>"; echo dirname(dirname(__FILE__)); ?>
输出:
C:\Web\Apache\htdocs\Magic\06\
C:\Web\Apache\htdocs\Magic\06\test.php
C:\Web\Apache\htdocs\Magic\06
C:\Web\Apache\htdocs\Magic
2:__LINE__
文件中的当前行号。这个变量在调试错误的时候,还是比较有作用的,其他的时候,没什么用处,纯属个人观点。
<?php echo __LINE__; //显示,__LINE__所在的行号 ?>
输出:
2
3:__CLASS__
类的名称,PHP5返回的结果是区分大小写的
<?phpclass base_class{ function say_a() { echo "'a' - said the " . __CLASS__ . "<br/>"; } function say_b() { echo "'b' - said the " . get_class($this) . "<br/>"; }}class derived_class extends base_class{ function say_a() { parent::say_a(); echo "'a' - said the " . __CLASS__ . "<br/>"; } function say_b() { parent::say_b(); echo "'b' - said the " . get_class($this) . "<br/>"; }}$obj_b = new derived_class();$obj_b->say_a();echo "<br/>";$obj_b->say_b();?>
结果为:
'a' - said the base_class
'a' - said the derived_class
'b' - said the derived_class
'b' - said the derived_class
有的时候,我们可以用get_class来代替__CLASS__
4:__FUNCTION__和__METHOD__
__FUNCTION__:函数名称,php5中返回的结果是区分大小写的
__METHOD__:方法中的函数名称,php5中返回的结果是区分大小写的
二个都是取得方法的名称,有什么不同呢?
<?phpclass test{ function a() { echo __FUNCTION__; echo "<br>"; echo __METHOD__; }}function good(){ echo __FUNCTION__; echo "<br>"; echo __METHOD__;}$test = new test();$test->a();echo "<br>";good();?>
返回结果:
a
test::a
good
good
相对于孤立的函数来说,二个都可以取出函数名,没什么区别,如果是class中的方法时,__FUNCTION__只能取出class的方法名,而__METHOD__不光能取出方法名,还能取出class名
5:__DIR__
文件所在的目录。如果用在被包括文件中,则返回被包括的文件所在的目录。它等价于 dirname(__FILE__)。除非是根目录,否则目录中名不包括末尾的斜杠。(PHP 5.3.0中新增)
如果在5.3以前的版本中想用__DIR__的话,可以这样
<?phpif (!defined('__DIR__')){ $iPos = strrpos(__FILE__, "/"); define("__DIR__", substr(__FILE__, 0, $iPos) . "/");}?>
6:__NAMESPACE__
当前命名空间的名称(大小写敏感)。这个常量是在编译时定义的(PHP 5.3.0 新增)
7:__STATIC__
当你调用class的静态方法时,返回class名称,区分大小写。如果在继承中调用的话,不管在继承中有没有定义,都能返回继承的class名。
<?php//php5.3 class Model{ public static function find() { echo __STATIC__; }}class Product extends Model{}class User extends Model{}Product::find(); // "Product" User::find(); // "User" ?>
转:http://blog.51yip.com/php/1165.html

PHP is a server-side scripting language used for dynamic web development and server-side applications. 1.PHP is an interpreted language that does not require compilation and is suitable for rapid development. 2. PHP code is embedded in HTML, making it easy to develop web pages. 3. PHP processes server-side logic, generates HTML output, and supports user interaction and data processing. 4. PHP can interact with the database, process form submission, and execute server-side tasks.

PHP has shaped the network over the past few decades and will continue to play an important role in web development. 1) PHP originated in 1994 and has become the first choice for developers due to its ease of use and seamless integration with MySQL. 2) Its core functions include generating dynamic content and integrating with the database, allowing the website to be updated in real time and displayed in personalized manner. 3) The wide application and ecosystem of PHP have driven its long-term impact, but it also faces version updates and security challenges. 4) Performance improvements in recent years, such as the release of PHP7, enable it to compete with modern languages. 5) In the future, PHP needs to deal with new challenges such as containerization and microservices, but its flexibility and active community make it adaptable.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP is not dead. 1) The PHP community actively solves performance and security issues, and PHP7.x improves performance. 2) PHP is suitable for modern web development and is widely used in large websites. 3) PHP is easy to learn and the server performs well, but the type system is not as strict as static languages. 4) PHP is still important in the fields of content management and e-commerce, and the ecosystem continues to evolve. 5) Optimize performance through OPcache and APC, and use OOP and design patterns to improve code quality.

PHP and Python have their own advantages and disadvantages, and the choice depends on the project requirements. 1) PHP is suitable for web development, easy to learn, rich community resources, but the syntax is not modern enough, and performance and security need to be paid attention to. 2) Python is suitable for data science and machine learning, with concise syntax and easy to learn, but there are bottlenecks in execution speed and memory management.

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Chinese version
Chinese version, very easy to use