Recent Posts

How to solve: Notice: Array to string conversion

This simply happens if you try to treat an array as a string:
$arr = array('foo', 'bar');

echo $arr;  // Notice: Array to string conversion
$str = 'Foo ' . $arr;  // Notice: Array to string conversion
An array cannot simply be echo'd or concatenated with a string, because the result is not well defined. PHP will use the string "Array" in place of the array, and trigger the notice to point out that that's probably not what was intended and that you should be checking your code here. You probably want something like this instead:
echo $arr[0];
$str = 'Foo ' . join(', ', $arr);
If this notice appears somewhere you don't expect, it means a variable which you thought is a string is actually an array. That means you have a bug in your code which makes this variable an array instead of the string you expect.

No comments:

Post a Comment