Home > php教程 > php手册 > php相对路径和绝对路径

php相对路径和绝对路径

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 10:19:20
Original
1325 people have browsed it

一个好的php代码,无论放到windows还是linux,不同版本的php上,都能正确的输出结果,才是一个好代码。
说起来容易的事,做起来并不是很轻松,很多时候写代码都是功能导向,当前环境,要赶时间立马见效果,基本就是怎么方便怎么来了。
但是为了写出一个好的代码和后期减少调试时间,写每一个代码都要斟酌考虑是否能够适应你所能想到的困难,每次解决一个,日积月累下来,你的代码就会伸缩自如了。
相对路径是对于当前代码文件所在文件夹来说。
绝对路径是相对于根文件夹来说。
当代码需要依赖别的文件时,就需要统一代码的包含路径。
代码执行时出现找不到文件,多数是由于没有定义好路径。
我推荐大家写绝对路径来写程序,相对路径一旦移动后就容易出现找不到要包含的文件。
用到的php函数和常量
dirname
__FILE__
DIRECTORY_SEPARATOR
推荐写一个初始化文件 initialize.php

// Define the core paths
// Define them as absolute paths to make sure that require_once works as expected
 
// DIRECTORY_SEPARATOR is a PHP pre-defined constant
// (\ for Windows, / for Unix)
defined('DS') ? null : define('DS', DIRECTORY_SEPARATOR);
 
defined('SITE_ROOT') ? null :  define('SITE_ROOT', dirname(__FILE__));
 
defined('LIB_PATH') ? null : define('LIB_PATH', SITE_ROOT.DS.'includes');
 
// load config file first
require_once(LIB_PATH.DS.'config.php');
 
// load basic functions next so that everything after can use them
require_once(LIB_PATH.DS.'functions.php');
 
// load core objects
require_once(LIB_PATH.DS.'session.php');
require_once(LIB_PATH.DS.'database.php');
 
// load database-related classes
require_once(LIB_PATH.DS.'user.php');
 
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template