William Jiang

JavaScript,PHP,Node,Perl,LAMP Web Developer – http://williamjxj.com; https://github.com/williamjxj?tab=repositories

php well-formated output

I wrote this for debug php purpose.
In LAMP, there are 2 types of output:

  1. standard output (stdout, file descriptor: 1)
    We output the information data to standard output, it is the default mode when we use echo, print.
  2. 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:

  1. echo, or print – simply output scalar variable
  2. print_r – Prints human-readable information about a variable
  3. 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.

  1. function output($var)
  2. {
  3. if (is_array($var)) {
  4. echo "<pre>";
  5. print_r($var);
  6. echo "</pre>";
  7. }
  8. else {
  9. echo $var . "<br>\n";
  10. }
  11. }

If your output is a complex sql in the browser, using the following:
echo “<pre>”. $sql . “</pre>”;
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:

  1. function dump(&$var, $info=FALSE)
  2. {
  3. $scope = false;
  4. $prefix = 'unique';
  5. $suffix = 'value';
  6. if($scope) $vals = $scope;
  7. else $vals = $GLOBALS;
  8. $old = $var;
  9. $var = $new = $prefix.rand().$suffix; $vname = FALSE;
  10. foreach($vals as $key => $val) if($val === $new) $vname = $key;
  11. $var = $old;
  12. echo "<pre style='margin: 20px; display: block; background: white; color: black; font-family: Verdana; border: 1px solid #cccccc; padding: 5px; font-size: 10px; line-height: 13px;'>";
  13. if($info != FALSE) echo "<b style='color: red;'>$info:</b><br>";
  14. do_dump($var, '$'.$vname);
  15. echo "</pre>";
  16. }
  17. function do_dump(&$var, $var_name=NULL, $indent=NULL, $reference=NULL)
  18. {
  19. $do_dump_indent = "<span style='color:#eeeeee;'>|</span>    ";
  20. $reference = $reference.$var_name;
  21. $keyvar = 'the_do_dump_recursion_protection_scheme'; $keyname = 'referenced_object_name';
  22. if (is_array($var) && isset($var[$keyvar]))
  23. {
  24. $real_var = &$var[$keyvar];
  25. $real_name = &$var[$keyname];
  26. $type = ucfirst(gettype($real_var));
  27. echo "$indent$var_name <span style='color:#a2a2a2'>$type</span> = <span style='color:#e87800;'>&$real_name</span><br>";
  28. }
  29. else
  30. {
  31. $var = array($keyvar => $var, $keyname => $reference);
  32. $avar = &$var[$keyvar];
  33. $type = ucfirst(gettype($avar));
  34. if($type == "String") $type_color = "<span style='color:green'>";
  35. elseif($type == "Integer") $type_color = "<span style='color:red'>";
  36. elseif($type == "Double"){ $type_color = "<span style='color:#0099c5'>"; $type = "Float"; }
  37. elseif($type == "Boolean") $type_color = "<span style='color:#92008d'>";
  38. elseif($type == "NULL") $type_color = "<span style='color:black'>";
  39. if(is_array($avar))
  40. {
  41. $count = count($avar);
  42. echo "$indent" . ($var_name ? "$var_name => ":"") . "<span style='color:#a2a2a2'>$type ($count)</span><br>$indent(<br>";
  43. $keys = array_keys($avar);
  44. foreach($keys as $name)
  45. {
  46. $value = &$avar[$name];
  47. do_dump($value, "['$name']", $indent.$do_dump_indent, $reference);
  48. }
  49. echo "$indent)<br>";
  50. }
  51. elseif(is_object($avar))
  52. {
  53. echo "$indent$var_name <span style='color:#a2a2a2'>$type</span><br>$indent(<br>";
  54. foreach($avar as $name=>$value) do_dump($value, "$name", $indent.$do_dump_indent, $reference);
  55. echo "$indent)<br>";
  56. }
  57. elseif(is_int($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
  58. elseif(is_string($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color\"$avar\"</span><br>";
  59. elseif(is_float($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color$avar</span><br>";
  60. elseif(is_bool($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $type_color".($avar == 1 ? "TRUE":"FALSE")."</span><br>";
  61. elseif(is_null($avar)) echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> {$type_color}NULL</span><br>";
  62. else echo "$indent$var_name = <span style='color:#a2a2a2'>$type(".strlen($avar).")</span> $avar<br>";
  63. $var = $var[$keyvar];
  64. }
  65. }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: