Reputation: 965
I'm trying to show the content of array into my error log when one Exception occurs.
I don't understand why print_r
is not working as expected in this code:
throw new ExcepcionApi(BAD_URL, ("Bad URL: {print_r($peticion,TRUE)}"));
In the log file:
PHP Fatal error: Uncaught ExcepcionApi: Bad URL: {print_r(Array,TRUE)} in / ...
This works fine:
$errorMsg=print_r($peticion,TRUE);
throw new ExcepcionApi(BAD_URL, ("Bad URL: $errorMsg"));
In the log file:
PHP Fatal error: Uncaught ExcepcionApi: Bad URL: Array
(
[0] => fakeURL
[1] => fakeParams
)
Why print_r
not works in the first case?
Upvotes: 0
Views: 970
Reputation: 4654
Quote from the PHP Manual:
Note: Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables.
That means you can use function and method calls inside the expression but only to obtain a variable name, e.g.:
$myvar = "Hello";
function whichvar() {
return "myvar"; // Returns a variable name
}
// gets variable by a name from the function
print "Result is: {${whichvar()}}"; // Result is: Hello
or to obtain array index etc.
$myarr = array(
'notthis' => 'Bad',
'andnotthistoo' => 'Too Bad',
'this' => 'Good'
);
function whichidx() {
return 'this';
}
print "I like {$myarr[whichidx()]}"; // I like Good
But it is not possible to get result of the function itself.
And actually I do not understand why you need it in the first place. You always can use simple string concatenation using .
operator:
throw new ExcepcionApi(BAD_URL, ("Bad URL: " . print_r($peticion,TRUE)));
Upvotes: 1