Home > php教程 > php手册 > php中的反斜杠/

php中的反斜杠/

WBOY
Release: 2016-06-21 08:55:43
Original
998 people have browsed it

目录有些人喜欢补上斜杠 比如 $dir = dirname(__FILE__) . '/';
而有些人嫌麻烦和代码不美观,直接用 $dir = dirname(__FILE__);

这样当我们要include文件的时候就有可能出问题。
比如要包含test.php,如果是dirname(__FILE__) . '/test.php',那当然没问题,如果没有斜杠,如dirname(__FILE__) . 'test.php',那问题就来了,要找D:\web\test.php就变成了找D:\webtest.php了。

解决方案:
目录常量或变量后用 ./ 代替 /
比如:
include 'D:/web./test.php';
include 'D:/web/./test.php';
都相当于
include 'D:/web/test.php';

这样就不用考虑目录名后面是否加 / 了,而且当用常量或变量来获取目录的时候,显示将更直观。
如:
define('ROOT_PATH', dirname(__FILE__) . '/');
include ROOT_PATH . './test.php';

include ROOT_PATH . '/test.php';
include ROOT_PATH . 'test.php';
更容易直观的判断 文件为 ROOT_PATH 下的文件




Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template