PHP 7.1 Warnings: Illegal String Offset + Cannot Assign Empty String To String Offset (working fine in PHP 7.0 and lower)

This was a simple question, but googling couldn’t lead me to a clear and proper answer. So, here’s the answer/fix/solution as a personal reminder and potential help to others. I had a strange and annoying problem while updating to PHP 7.1 — everything was working fine, except this little piece of code:

$array = $something; // $something can be anything, another function call, for example, that might return empty/null, array or string
if(!isset($array['id']) ) {
    $array['id'] = '';
}

Now, this returns PHP warnings under PHP 7.1 (and up):

PHP Warning: Illegal string offset ‘id’ on line 3
PHP Warning: Cannot assign an empty string to a string offset on line 3

Any explanation why?

Answer: Problem is that our variable $array (line #1) is not set or initialized as an array() in case of $something part being undefined in the first place! This was apparently fine in PHP 7.0 and below, but not anymore. Now, PHP will NOT assume or automatically fix our problem any longer, and we need to explicitly set it as array() a priory.

Fixed code version:

$array = (array) $something; // simply cast $array variable as array()
if(!isset($array['id']) ) {
    $array['id'] = '';
}

Hope this helps someone, as the solution is really easy & simple.

Categories
PHP Web Development

Comments


  1. comments

    6 Comments

    Add Your Comment
  2. 1. Ivan A

    Thanks, this was exactly what we need on an issue today! :D

  3. 2. obed

    i have this block of code which gives me this error

    ” PHP Warning: Illegal string offset ‘ph_id’ in /user/help.php on line 203″

    how can i solve this????

    <?php
    sum_paidPHs($ph['ph_id']);
    ?>

  4. 3. TehnoBlog (In reply to obed)

    Is $ph variable initialized as an (array) in the first place? If it is, then this issue is not related to this, but from the fact that key ‘ph_id’ does not exist inside of it.

    While you can solve this very easily with isset(), for example:

    if (isset($ph['ph_id'])) {
    sum_paidPHs($ph['ph_id']);
    }

    However, if $ph[‘ph_id’] being not set is abnormal case, you have to go earlier in the code and examine what’s causing it.

    Hope this helps!

  5. 4. ubrain

    This was a simple question, but googling lead me to a clear and proper answer… here :-)

    So BIG thanks!

  6. 5. mitra

    I write this code and encounter a problem as i mentioned bellow. how can i fix it.

    (incomplete code – removed by editor)

    Warning: Illegal string offset ‘Math’ in C:\xampp\htdocs\raz\php1\incs\test1-1.php on line 40

  7. 6. TehnoBlog (In reply to mitra)

    Hi mitra,
    I am sorry, but your code was not received here, only last few lines partially. You can paste your code on pastebin.com (for example), then I might be able to help you.

    Your line number #40 (whatever was on it, I cannot tell, as I did not see it) calls for element in array which does not exist. That’s the outline of the “error” (warning) in a nutshell. Check if you properly defined that element earlier in your code.