Witam wszystkich!
Jak wiadomo często zachodzi konieczność sprawdzenia zawartości tablicy, typu zmiennej etc.
Print_r pokazuje w sposób przyjemny dla oka, ale bez typów zmiennych, z kolei var_dump ma wszystko czego trzeba, ale jak na mój gust jest niezbyt czytelny.
Z tego też względu, na własne potrzeby zmajstrowałem prostą funkcję wyświetlającą dane w sposób równie czytelny jak print_r, ale z określeniem typów zmiennych jak var_dump.
Różnicę stanowi podejście do zmiennych typu object i resource - w tej funkcji wyświetlany jest jedynie odpowiedni typ oraz nazwa klasy w przypadku obiektu i typ "zasobu" w przypadku resource (np mysql link)

Przykład:
  1. <?php
  2. $tab1 = array (
  3. '12',
  4. 4,
  5. new FrontController,
  6. $this->System->Smarty,
  7. mysql_connect('localhost', 'user', 'pass'),
  8. 'item1' => 'item1value', 
  9. 'item2' => array(
  10. 'subitem1' => '55', 
  11. 'subitem2' => false, 
  12. 'subitem3' => 4,
  13. 'subarray' => array (
  14. 'subarrayitem1' => 'subarrayitem1value',
  15. 2.4,
  16. 'subarrayitem2'
  17. ),
  18. null
  19. )
  20. );
  21. showArray($tab1, 'Informacja dodatkowa');
  22. ?>

Efekt:

Jeżeli ktoś uzna to za przydatne, poniżej kod (jeśli komuś nudziłoby się na tyle, żeby wtrącić swoje uwagi to będę wdzięczny winksmiley.jpg)
  1. <?php
  2. function showArray($input, $additionalText='', $inputKey='', $level=0, $start=true) {
  3.  
  4. if($start) {
  5. echo '<div style="text-align:left;font-size:9pt;color:#454545;width:auto;margin:10px auto;padding:10px 20px;">
  6.  <p style="text-align:left;font-weight:bold;color:#A60000;text-decoration:underline;margin:5px 0;">
  7. '.$additionalText.'</p>';
  8. }
  9. switch(gettype($input)) {
  10. case 'array':
  11. $type = 'Array('.sizeof($input).')<br />{';
  12. $inputText = '';
  13. break;
  14. case 'string':
  15. $type = 'String('.strlen($input).')';
  16. $inputText = '"'.htmlentities($input).'"';
  17. break;
  18. case 'integer':
  19. $type = 'Integer';
  20. $inputText = $input;
  21. break;
  22. case 'boolean':
  23. $type = 'Boolean';
  24. $inputText = var_export($input, true);
  25. break;
  26. case 'double':
  27. $type = 'Float';
  28. $inputText = $input;
  29. break;
  30. case 'object':
  31. $type = 'Object of';
  32. $inputText = get_class($input);
  33. break;
  34. case 'resource':
  35. $type = 'Resource of';
  36. $inputText = get_resource_type($input);
  37. break;
  38. case 'NULL':
  39. $type = 'NULL';
  40. $inputText = '';
  41. break;
  42. }
  43.  
  44. $string = "<div style=\"margin:0 0 0 %dpx\">%s<span style=\"font-size:7pt;color:#009900;\">%s</span>
  45.  <span style=\"font-weight:bold;font-size:9pt;color:#000000;\">%s</span></div>";
  46. $arrayClosing = "<div style=\"margin:15px 0 0 %dpx\">}</div>";
  47.  
  48. $inputKey = (is_string($inputKey)) ? '["<strong>'.$inputKey.'</strong>"] => ' : '[<strong>'.$inputKey.'</strong>] => ' ;
  49.  
  50. if($start && !is_array($input)) {
  51. printf($string, 0, '', $type, $inputText);
  52. return;
  53. }
  54. if(is_array($input)) {
  55. printf($string, $level*50, ($start) ? '' : $inputKey, $type, $inputText);
  56. foreach($input AS $key=>$value) {
  57. showArray($value, $additionalText, $key, $level+1, false);
  58. }
  59. printf($arrayClosing, $level*50);
  60. } else {
  61. printf($string, $level*50, $inputKey, $type, $inputText);
  62. }
  63.  
  64. if($start) {
  65. echo '</div>';
  66. }
  67. }
  68. ?>