• Blogs (9)
    • 📱 236 - 992 - 3846

      📧 jxjwilliam@gmail.com

    • Version: ‍🚀 1.1.0
  • php well-formated output

    Blogs20102010-11-20


    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 ”
      “;
    5. print_r($var);
    6. echo ”“;
    7. }
    8. else {
    9. echo $var . ”
      n”;
    10. }
    11. }

    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:

    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 ”
      “;
    13. if($info != FALSE) echo ”$info:
      “;
    14. do_dump($var, ’$‘.$vname);
    15. echo ”“;
    16. }
    17. function do_dump(&$var, $var_name=NULL, $indent=NULL, $reference=NULL)
    18. {
    19. $do_dump_indent = ”|    “;
    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 $type = &$real_name
      “;
    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 = ”“;
    35. elseif($type == “Integer”) $type_color = ”“;
    36. elseif($type == “Double”){ $type_color = ””; $type = “Float”; }
    37. elseif($type == “Boolean”) $type_color = ”“;
    38. elseif($type == “NULL”) $type_color = ”“;
    39. if(is_array($avar))
    40. {
    41. $count = count($avar);
    42. echo “$indent” . ($var_name ? “$var_name => ”:"") . ”$type ($count)
      $indent(
      “;
    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)
      “;
    50. }
    51. elseif(is_object($avar))
    52. {
    53. echo “$indent$var_name $type
      $indent(
      “;
    54. foreach($avar as $name=>$value) do_dump($value, “$name”, $indent.$do_dump_indent, $reference);
    55. echo “$indent)
      “;
    56. }
    57. elseif(is_int($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color$avar
      “;
    58. elseif(is_string($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color”$avar”
      “;
    59. elseif(is_float($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color$avar
      “;
    60. elseif(is_bool($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) $type_color”.($avar == 1 ? “TRUE”:“FALSE”).”
      “;
    61. elseif(is_null($avar)) echo “$indent$var_name = $type(“.strlen($avar).“) {$type_color}NULL
      “;
    62. else echo “$indent$var_name = $type(“.strlen($avar).“) $avar
      “;
    63. $var = $var[$keyvar];
    64. }
    65. }