While you are utilizing PHP, have you ever met the Warning: Illegal string offset yet? If that is all you are facing and trying to solve, don’t worry because we are here to help you to address the trouble.

What causes the Warning: Illegal string offset?

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

You have the code:

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

And when you try to access it, there will be the 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, you can not print 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 in which there will have 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 disappears, 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, in case you are troubled with 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 the reason why loop over the array, there will be a PHP warning. Therefore, before accessing the array inside the foreach function, 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, it’s time for you to fix it by using 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"
}
}

It is noticed that the [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 meet the same problem and have another greater solution, don’t forget to share it with us by leaving your comment below. We will be happy about it.

Furthermore, in case you have an intention 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.

5/5 - (1 vote)
Lt Digital Team (Content &Amp; Marketing)
Latest posts by LT Digital Team (Content & Marketing) (see all)

Flash Sale! Get 35% Off for everything on today, don't miss it. Coupon code: FLASHSALE35 Check it out
Flash Sale! Get 35% Off for everything on today, don't miss it. Coupon code: FLASHSALE35 Redeem Now