php well-formated output
Blogs20102010-11-20
I wrote this for debug php purpose.
In LAMP, there are 2 types of output:
- standard output (stdout, file descriptor: 1)
We output the information data to standard output, it is the default mode when we use echo, print. - standard error (stderr, file descriptor: 2)
When errors occurs, the error messages write to it. In command line environment, it is the same as standard output (stdout=stderr), but it differs in other cases.
in Apache web server, the standard output redirects to $log/access.log, while standard errors to $log/error.log.
For standard output, the data can be buffered or not:
- By using ob_start(), fwrite() functions to buffer the data. This delays the operation, but improves the input/output performance.
- If not useing buffering, fwrite() functions directly operate I/O, without delay (although frequent I/O operation could decrease the performance).
In production environment, for security reason, we suppress the error message on failure by prepending a @ to the function name.
e.g, @mysql\pconnect($host, $username, $password);_
In developing environment, the output is very important for testing and debug purpose which we can’t disable or ignore.
There are 3 type of output:
(a) To system logfile, e.g, for Apache’s logfiles: error.log and access.log.
It is very useful for test and debug purpose, e.g, we use:
$ tail -f error.log
$ tail -f access.log
to monitor errors and standard output in real time;
or:
$ tail -n 20 error.log
to monitor error.log last 20 lines.
(b) self-defined log to hold output: We can create a file and ouput useful data into our file to monitor the programs.
$file = fopen(“my.log”, “w”);
fwrite($file, $message);
fclose($file);
(c) to screen (browser):
We have 3 common functions for output to screen purpose:
- echo, or print - simply output scalar variable
- print_r - Prints human-readable information about a variable
- var_dump - Dumps information about a variable
The built-in echo is suitable for scalar variables, while print_r() is for array(). var_dump() is a little super, could replace print_r() when necessary.
The following function is a little smart to decide output format according to $variable itself: scalar or array().
If scalar, using echo. If array, using print_r() to output.
- function output($var)
- {
- if (is_array($var)) {
- echo ”
“;
- print_r($var);
- echo ”“;
- }
- else {
- echo $var . ”
n”; - }
- }
If your output is a complex sql in the browser, using the following:
echo ””. $sql . ”
“;
This makes long statements seperated by serveral linkes, so more clear and easy-reading.
The following is a better solution for output in browser. It does some css works to make the output well-formatted and esay-reading.
I used it in my various applications to generate formatted output when warnings or errors occur.
The entry point is: dump($message\to_output, $info);_
- every time the dump($message_to_output) is called.
- $info is the title of the message. It is optional.
- do_dump() is internal called, don’t need to call it directly.
The following are the functions:
- function dump(&$var, $info=FALSE)
- {
- $scope = false;
- $prefix = ‘unique’;
- $suffix = ‘value’;
- if($scope) $vals = $scope;
- else $vals = $GLOBALS;
- $old = $var;
- $var = $new = $prefix.rand().$suffix; $vname = FALSE;
- foreach($vals as $key => $val) if($val === $new) $vname = $key;
- $var = $old;
- echo ”
“;
- if($info != FALSE) echo ”$info:
“; - do_dump($var, ’$‘.$vname);
- echo ”“;
- }
- function do_dump(&$var, $var_name=NULL, $indent=NULL, $reference=NULL)
- {
- $do_dump_indent = ”| “;
- $reference = $reference.$var_name;
- $keyvar = ‘the_do_dump_recursion_protection_scheme’; $keyname = ‘referenced_object_name’;
- if (is_array($var) && isset($var[$keyvar]))
- {
- $real_var = &$var[$keyvar];
- $real_name = &$var[$keyname];
- $type = ucfirst(gettype($real_var));
- echo “$indent$var_name $type = &$real_name
“; - }
- else
- {
- $var = array($keyvar => $var, $keyname => $reference);
- $avar = &$var[$keyvar];
- $type = ucfirst(gettype($avar));
- if($type == “String”) $type_color = ”“;
- elseif($type == “Integer”) $type_color = ”“;
- elseif($type == “Double”){ $type_color = ””; $type = “Float”; }
- elseif($type == “Boolean”) $type_color = ”“;
- elseif($type == “NULL”) $type_color = ”“;
- if(is_array($avar))
- {
- $count = count($avar);
- echo “$indent” . ($var_name ? “$var_name => ”:"") . ”$type ($count)
$indent(
“; - $keys = array_keys($avar);
- foreach($keys as $name)
- {
- $value = &$avar[$name];
- do_dump($value, ”[‘$name’]”, $indent.$do_dump_indent, $reference);
- }
- echo “$indent)
“; - }
- elseif(is_object($avar))
- {
- echo “$indent$var_name $type
$indent(
“; - foreach($avar as $name=>$value) do_dump($value, “$name”, $indent.$do_dump_indent, $reference);
- echo “$indent)
“; - }
- elseif(is_int($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color$avar
“; - elseif(is_string($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color”$avar”
“; - elseif(is_float($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color$avar
“; - elseif(is_bool($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color”.($avar == 1 ? “TRUE”:“FALSE”).”
“; - elseif(is_null($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) {$type_color}NULL
“; - else echo “$indent$var_name = $type(“.strlen($avar).“) $avar
“; - $var = $var[$keyvar];
- }
- }
