第一步:

在 php.ini 中,将 display_errors 设置为 On;

第二步:

在 框架的 开始处,添加如下代码:

<?php
if (isset($_GET['debug'])) {
     // ini_set('display_errors', true);
     //【注意】本来不想到 php.ini 中修改 display_errors 值,但是 在 PHP 代码中设置 display_errors 值不起作用,悲剧!
    error_reporting(E_ALL);
    set_time_limit(0);
} else {
    error_reporting(E_ERROR);
}

 



 

以前习惯用 echo,print_r 等方法来测试PHP输出,这样多多少少会影响到代码的正常运行,现改成“把测试变量输出到文本”的形式,可实现日志形式的调试,代码如下:

<?php
/**
* 写文件
*
* @param string $filename 文件名
* @param string $text 要写入的文本字符串
* @param string $openmod 文本写入模式('w':覆盖重写,'a':文本追加)
* @return bool
*/
function file_write($filename = '', $text = '', $openmod = 'w') {
if (@$fp = fopen($filename, $openmod)) {
flock($fp, 2);
fwrite($fp, $text);
fclose($fp);
return true;
} else {
return false;
}
}

/**
* 写对象(包括 数字、字符串、数组)
* 【注意:临时调试用而已!】
*
* @param string $text 要写入的文本字符串
* @param string $type 文本写入类型('w':覆盖重写,'a':文本追加)
* @param bool $isVarExport 是否变量导出
* @return bool
*/
function write2($text = '', $type = 'a', $isVarExport = false) {
$filename = __DIR__ . '/write2.txt';

$text = (is_array($text) && $isVarExport) ? var_export($text, true) : print_r($text, true);
$text = "++++++++++++++++++++++++++++++++++++++++++\r\n"
. date('Y-m-d H:i:s') . "\r\n"
. $text . "\r\n";

return file_write($filename, $text, $type);
}


今后,直接用write2('xxx');函数即可。

 

另外,也可用“file_put_contents”去写文本

$data222 = $arr;

$text222 = print_r($data222, true);
$filename222 = 'd:\My Documents\1\write.txt';
file_put_contents($filename222, $text222);