今天学到一个招,就是如果是在FIREFOX或者chrome下,可以将PHP中的警告,错误等信息,
分门别类输出到firebug的控制台中,这样查看起来会比较好看,比较直观.
先有一个debug类如下:
class PHPDebug {
function __construct() {
if (!defined("LOG")) define("LOG",1);
if (!defined("INFO")) define("INFO",2);
if (!defined("WARN")) define("WARN",3);
if (!defined("ERROR")) define("ERROR",4);
define("NL","\r\n");
echo '';
/// end of IE
}
function debug($name, $var = null, $type = LOG) {
echo ''.NL;
}
}
使用起来也很方便,例子如下:
// include and instantiate the class
require_once("PHPDebug.php");
$debug = new PHPDebug();
// simple message to console
$debug->debug("A very simple message");
// vaiable to console
$x = 3;
$y = 5;
$z = $x/$y;
$debug->debug("Variable Z: ", $z);
// a warnign
$debug->debug("A simple Warning", null, WARN);
// info
$debug->debug("A simple Info message", null, INFO);
// An error
$debug->debug("A simple error messsage", null, ERROR);
// Array in console
$fruits = array("banana", "apple", "strawberry", "pineaple");
$fruits = array_reverse($fruits);
$debug->debug("Fruits array", $fruits);
// object to console
$book = new stdClass;
$book->title = "Harry Potter and the Prisoner of Azkaban";
$book->author = "J. K. Rowling";
$book->publisher = "Arthur A. Levine Books";
$book->amazon_link = "http://www.amazon.com/dp/0439136369/";
$debug->debug("Object", $book);
这样,就可以将各种警告或错误,分门别类地显示在浏览器的控制台中了