参考:【个人专用】PHP打印测试,PHP调试技巧,PHP测试技巧
以前习惯用 echo,print_r 等方法来测试php输出,这样多多少少会影响到代码的正常运行,现改成“把测试变量输出到文本”的形式,可实现日志形式的调试,代码如下:
<?
/**
* 01.数组转换成字串
* */
function Array_arrayeval($array, $level = 0) {
$space = '';//每行的"空格"数量,即"制表符"数量
for($i = 0; $i <= $level; $i++) {
$space .= "\t";
}
$evaluate = "Array\n$space(\n";//计值
$comma = $space;//逗号
foreach($array as $key => $val) {
$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
$val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '\''.addcslashes($val, '\'\\').'\'' : $val;
if(is_array($val)) {
$evaluate .= "$comma$key => ".Array_arrayeval($val, $level + 1);
} else {
$evaluate .= "$comma$key => $val";
}
$comma = ",\n$space";
}
$evaluate .= "\n$space)";
return $evaluate;
}
/**
* 02.写入文件
* @param 1.$filename 文件名(完整的物理路径,例如:D:\php\www\root\wjbphp\wjb.txt)
* @param 2.$writetext 要写入的文本
* @param 3.$openmod 读写方式("w":删掉以前的内容,从新写入; "a":追加)
* */
function File_writefile($filename, $writetext, $openmod='w') {
if(@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $writetext);
fclose($fp);
return true;
} else {
log_runlog('error', "File: $filename write error.");
return false;
}
}
/**
* 01.数组转换成字串
* */
function Array_arrayeval($array, $level = 0) {
$space = '';//每行的"空格"数量,即"制表符"数量
for($i = 0; $i <= $level; $i++) {
$space .= "\t";
}
$evaluate = "Array\n$space(\n";//计值
$comma = $space;//逗号
foreach($array as $key => $val) {
$key = is_string($key) ? '\''.addcslashes($key, '\'\\').'\'' : $key;
$val = !is_array($val) && (!preg_match("/^\-?\d+$/", $val) || strlen($val) > 12 || substr($val, 0, 1)=='0') ? '\''.addcslashes($val, '\'\\').'\'' : $val;
if(is_array($val)) {
$evaluate .= "$comma$key => ".Array_arrayeval($val, $level + 1);
} else {
$evaluate .= "$comma$key => $val";
}
$comma = ",\n$space";
}
$evaluate .= "\n$space)";
return $evaluate;
}
/**
* 02.写入文件
* @param 1.$filename 文件名(完整的物理路径,例如:D:\php\www\root\wjbphp\wjb.txt)
* @param 2.$writetext 要写入的文本
* @param 3.$openmod 读写方式("w":删掉以前的内容,从新写入; "a":追加)
* */
function File_writefile($filename, $writetext, $openmod='w') {
if(@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $writetext);
fclose($fp);
return true;
} else {
log_runlog('error', "File: $filename write error.");
return false;
}
}
?>
主要是用到“File_writefile”函数,对上面的 “File_writefile”函数再包装一下:
function cachewrite($writetext, $openmod='a'){
$filename = 'd:\My Documents\1\cache.txt';
$writetext = "\r\n+++++++++++++++++++++++++++++++++++++++++++\r\n".$writetext;
return File_writefile($filename, $writetext, $openmod);
}
$filename = 'd:\My Documents\1\cache.txt';
$writetext = "\r\n+++++++++++++++++++++++++++++++++++++++++++\r\n".$writetext;
return File_writefile($filename, $writetext, $openmod);
}
应用如下:
$arr = array(
"w" => "wen",
"j" => "jian",
"b" => "bao"
);
cachewrite(print_r($arr,true));
"w" => "wen",
"j" => "jian",
"b" => "bao"
);
cachewrite(print_r($arr,true));
输出结果:
+++++++++++++++++++++++++++++++++++++++++++
Array
(
[w] => wen
[j] => jian
[b] => bao
)
Array
(
[w] => wen
[j] => jian
[b] => bao
)
上面的仅仅是小技巧而已,最好还是用专业的调试工具去调试比较好。。。