Using print_r in an exception?

php

Solution

Or will that cause problems?

Yes, it will. The `print_r` will be evaluated before the string is fully constructed, and the output will look something like this, if you try and print out the `Exception` message:

Array
(
    [x] => 5
    [y] => 65
)
Oh no, an exception! 1

To fix this, you need to make sure you set `print_r`'s `$return` parameter to `true` so the value is returned rather than echoed:

throw new Exception('Oh no, an exception! ' . print_r($variable, true));

Problem

Possible Duplicate: putting print_r results in variable I'm throwing an exception and trying to include a variable in the exception, like so: ``` throw new Exception('Oh no, an exception! ' . $variable); ``` (Where `$variable` is an array) The problem is, this only puts the following in my log file: On no, an exception! Array Unfortunately I'm not an expert at PHP, I'm guessing this could mean one of two things: 1) $variable is an empty array 2) $varialbe is an array with data in it, but outputting it as such in an exception does not output all of its contents Please let me know if 1) is the case here (I hope it isn't though) However, if 2) is the case, how can I get more information about `$variable`? Is it possible to do `print_r` or `var_dump` inside the exception like follows: ``` throw new Exception('Oh no, an exception! ' . print_r($variable)); ``` Or will that cause problems?

Original source

Related problems