Warning: illegal string offset

While using PHP, have you ever encountered the Warning: Illegal string offset? If that is what you are facing and trying to solve, don’t worry because we are here to help you fix it. What causes the Warning: Illegal string…

Warning: illegal string offset

While using PHP, have you ever encountered the Warning: Illegal string offset? If that is what you are facing and trying to solve, don’t worry because we are here to help you fix it.

What causes the Warning: Illegal string offset?

Let’s take a look at the following example so that we can explain it clearly.

You have the code:

Array
(
[host] => 127.0.0.1
[port] => 11211
)

And when you try to access it, you will get this warning:

print $memcachedConfig['host'];
print $memcachedConfig['port'];

Warning: Illegal string offset 'host' in ....
Warning: Illegal string offset 'port' in ...

In this case, you are trying to use a string as if it was a full array. In other words, you think the code you are using is an array, but in fact, it is a string. This is not allowed. As a result, it cannot be printed and you will get the warning: Illegal string offset in PHP.

Besides that, you can also face the warning if you have a multi-dimensional PHP array that contains a string instead of an array.

So, now, we will show you 2 solutions to tackle the 2 reasons for the issue mentioned above.

How to fix the Warning: Illegal string offset?

In the first case, in order to solve the warning: Illegal string offset or make it disappear, you need to ensure that you are accessing an array instead of a string. All you need to do now is check whether you are accessing an array by calling the is_array() and the isset function before you access the variable. For instance:

$a_string = "string";
$an_array = array('port' => 'the_port');


if (is_array($a_string) && isset($a_string['port'])) {
// No problem, we'll never get here.
echo $a_string['port'];
}


if (is_array($an_array) && isset($an_array['port'])) {
// Ok!
echo $an_array['port']; // the_port
}


if (is_array($an_array) && isset($an_array['unset_key'])) {
// No problem again, we won't enter.
echo $an_array['unset_key'];
}

Moreover, if you are getting the PHP warning: Illegal string offset because of the second reason, you can also check where the string is with the help of is_array() and var_dump(). Now, let’s move on to another example:

$rows = [
["name" => "John"],
"",
["name" => "Dave"]
];


foreach ($rows as $row) {
echo $row["name"]. "\n";
}

In this situation, the $rows array contains a string in line 3. That is why there will be a PHP warning when you loop over the array. Therefore, before accessing the array inside the foreach loop, you need to check the $row type:

foreach ($rows as $row) {
if(is_array($row)){
echo $row["name"]. "\n";
}
}

If the $row is not an array, PHP will ignore the row and then move to the next one. As a result, you will receive the output:

John
Dave

Next, you can also use var_dump() to see the output of the variable:

var_dump($rows);

Now, in the dump result, you can check whether a string remains in your $rows or not:

array(3) {
[0]=>
array(1) {
["name"]=>
string(4) "John"
}
[1]=>
string(0) ""
[2]=>
array(1) {
["name"]=>
string(4) "Dave"
}
}

Notice that [1] is not an array; it is a string. Hence, you need to use the “if” statement and call the is_array() function to deal with this issue.

Closing thoughts

All in all, we hope that the blog today will help you address the Warning: Illegal string offset in PHP effectively. If you have run into the same problem and have a better solution, don’t forget to share it with us by leaving your comment below. We would be happy to hear it.

Furthermore, if you intend to give your site a new and fresh appearance, don’t hesitate to visit our site, then get the best free WordPress themes and Joomla 4 Templates. We hope you will like them.

Build it with a template

Start from a finished design: Joomla templates, WordPress themes or the AI-ready block themes. Every template has a free version and a live demo.

Free download

We'll send the link, setup tips and update notices, and you can reach us anytime through our AI assistant and contact form.

The download starts at once. We email you the link and ask you to confirm your address. Privacy policy.