Array to string conversion error in php

Are you having this error Array to string conversion in your code? Here is how to fix it.

Array to string conversion

What you’re doing wrong

Actually, the error itself is self-explaining, somewhere in your code, you are trying to convert an array to a string. Here is an example

$data = ['John','Mike','Mary'];

echo $data;

As you can see in the example above, when I try to echo a variable ($data) that holds an array of strings, I get the error

Error Exception Array to string conversion

Solution to

A simple solution will be to replace echo with var_dump.

var_dump($data);

Follow this article to learn more on how echo works in php.

Leave a Comment

%d bloggers like this: