testing custom var_dump - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of testing custom var_dump.php

  1. <?php
  2.  
  3. class FooBar
  4. {
  5.     function foo_function()
  6.     {
  7.         return "Hello World!";
  8.     }
  9. }
  10.  
  11. $var_class = new FooBar;
  12.  
  13. $example_array = [
  14.     'null'         => null,
  15.     'null_text'    => 'null',
  16.     'integer'      => 10,
  17.     'integer_text' => '10',
  18.     'float'        => 20.35,
  19.     'float_text'   => '20.35',
  20.     'string'       => 'Hello World',
  21.     'date_1'       => '2021-01-17',
  22.     'date_2'       => '2021-Jan-17',
  23.     'hour_1'       => '6:31:00 AM',
  24.     'hour_2'       => '17:31:00',
  25.     'datetime_1'   => '2021-01-17 17:31:00',
  26.     'datetime_2'   => '2021-Jan-17 6:31:00 AM',
  27.     'datetime_3'   => '2021-01-17 6:31:00 AM',
  28.     'datetime_4'   => '2021-Jan-17 17:31:00',
  29.     'currency_1'   => '1.45$',
  30.     'currency_2'   => '$ 1.45',
  31.     'array'        => [
  32.         'boolean_true'       => true,
  33.         'boolean_false'      => false,
  34.         'boolean_true_text'  => 'true',
  35.         'boolean_false_text' => 'false',
  36.     ],
  37.     'objects_list' => [
  38.         'object'   => (object) [],
  39.         'class'    => $var_class,
  40.     ],
  41. ];
  42.  
  43. $example_array['array']['nested'] = ['other' => (object) ['apple', 'banana', 'coconut']];
  44.  
  45.  
  46. $pretty = function ($v = '', $c = "    ", $in = -1, $k = null) use (&$pretty) {
  47.     $r = '';
  48.     if (in_array(gettype($v), array('object', 'array'))) {
  49.         $r .= ($in != -1 ? str_repeat($c, $in) : '') . (is_null($k) ? '' : "$k: ") . '<br>';
  50.         foreach ($v as $sk => $vl) {
  51.             $r .= $pretty($vl, $c, $in + 1, $sk) . '<br>';
  52.         }
  53.     } else {
  54.         $r .= ($in != -1 ? str_repeat($c, $in) : '') . (is_null($k) ? '' : "$k: ") . (is_null($v) ? '<NULL>' : "<strong>$v</strong>");
  55.     }
  56.     return $r;
  57. };
  58.  
  59. echo $pretty($example_array);
File Description
  • testing custom var_dump
  • PHP Code
  • 22 Feb-2021
  • 1.67 Kb
You can Share it: