php初学者笔记
时间:2009-08-05 来源:sulpha
一、不要使用相对路径include
As a rule of thumb, never include files using relative paths.
二、include的变量变为NULL的问题
现象:
有个配置文件config.php,内容如下:
<?php
define('URL', 'http://192.168.1.15/');
$DBNAME = 'databasename';
?>
config.php文件被别的文件a.php引用:
<?php
function getuserapp() {
include_once('./config.php");
}
?>
a.php文件被b.php文件引用:
在b.php里面,只能访问config.php里面的URL常量,不能访问变量$DBNAME
原因:
http://us.php.net/manual/en/function.include.php
http://cn.php.net/manual/en/language.variables.scope.php
好的习惯:
If you wish to abstract away include calls inside functions, or programmatically juggle files to include using functions, just remember:
1. Declare any variables as global if you want those variables "included" in the global scope (ie. if they are used outside the file).
2. Functions are naturally global, so files that only contain functions (libs, sets of api's what have you) can be included anywhere.
As a rule of thumb, never include files using relative paths.
二、include的变量变为NULL的问题
现象:
有个配置文件config.php,内容如下:
<?php
define('URL', 'http://192.168.1.15/');
$DBNAME = 'databasename';
?>
config.php文件被别的文件a.php引用:
<?php
function getuserapp() {
include_once('./config.php");
}
?>
a.php文件被b.php文件引用:
在b.php里面,只能访问config.php里面的URL常量,不能访问变量$DBNAME
原因:
http://us.php.net/manual/en/function.include.php
http://cn.php.net/manual/en/language.variables.scope.php
好的习惯:
If you wish to abstract away include calls inside functions, or programmatically juggle files to include using functions, just remember:
1. Declare any variables as global if you want those variables "included" in the global scope (ie. if they are used outside the file).
2. Functions are naturally global, so files that only contain functions (libs, sets of api's what have you) can be included anywhere.
相关阅读 更多 +