11

I need to jump into a server side code. It is used cakephp there. I would like to see a variable, I think it is a model, but I am not sure, let be a variable in or case.

CakeLog::write('debug', 'myArray'.var_export($myArray) );

it will have the output

myArray: Array

I would like to see similar output as var_dump can produce to the output.

Is that possible? if yes, than how?

Any help apreciated.

  • I simply did var_dum($myarray); in my controller and I got the resulted array printed in my view. may it help. – Meer Oct 11 '14 at 03:39

3 Answers3

19

Just use print_r, it accepts a second argument not to output the result.

CakeLog::write('debug', 'myArray'.print_r($myArray, true) );

And if you don't want new lines, tabs or double spaces in your log files:

$log = print_r($myArray, true);
$log = str_replace(array("\n","\t"), " ", $log);
$log = preg_replace('/\s+/', ' ',$log);
CakeLog::write('debug', 'myArray' . $log);
Niklas Modess
  • 2,521
  • 1
  • 20
  • 34
2

Try:

CakeLog::write('debug', 'myArray'.print_r($myArray, true));

The true parameter makes print_r return the value rather than print on screen, so you can save it.

http://br2.php.net/manual/en/function.print-r.php

petervaz
  • 13,685
  • 1
  • 15
  • 15
0

Somebody got a redirection method presented here.

This I have used to see what I have there, and it shows very clear.

  • This guy uses `ob_start()` to get the return value of a `var_dump()`, which is what `print_r($var, true)` does internally. Seems he is using an old version of php. – petervaz Sep 14 '12 at 13:38