Mathieu Hays

Here is a trick I came up with recently. I find it very convenient when you want to keep your visual output clean and be able to have a more organised view of your data. It basically just support numbers, string, arrays and simple stdClass objects which I think is enough for most use cases.

<?php
function console_log()
{
  $log = array();

  foreach (func_get_args() as $arg) {
    if (is_object($arg)) {
      $_arg = get_object_vars($_arg);
    }

    $log[] = json_encode($_arg);
  }
  
  echo "<script type='text/javascript'>console.log(".join(",", $log).")</script>";
}

Let me know what you think in the comments 😉