您好,欢迎访问一九零五行业门户网

php相对路径和绝对路径_PHP教程

一个好的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');
http://www.bkjia.com/phpjc/621603.htmlwww.bkjia.comtruehttp://www.bkjia.com/phpjc/621603.htmltecharticle一个好的php代码,无论放到windows还是linux,不同版本的php上,都能正确的输出结果,才是一个好代码。 说起来容易的事,做起来并不是很轻...
其它类似信息

推荐信息