0

I upgraded to PHP 5.4 today and I am receiving some strange warnings:

Warning: Illegal string offset 'quote1' in file.php on line 110
Warning: Illegal string offset 'quote1_title' in file.php on line 111

Those lines are this part of the code:

for($i = 0; $i < 3; $i++) {
    $tmp_url = $meta['quote'. ($i+1)];
    $tmp_title = $meta['quote' . ($i+1) .'_title'];

    if(!empty($tmp_url) || !empty($tmp_title)) {
        $quotes[$src_cnt] = array();
        $quotes[$src_cnt]['url'] = $tmp_url;
        $quotes[$src_cnt]['title'] = $tmp_title;
        $src_cnt++;
    }
}

So the $tmp_url and $tmp_title line.

Why am I receiving this odd warning and what is the solution?

Update:

This code is being used as a Wordpress plugin. $meta includes:

$meta = get_post_meta($post->ID,'_quote_source',TRUE);

So I am suspecting that whenever the quotes fields are empty, this warning appears. Is there any way that I can fix this for when the fields are empty?

Henrik Petterson
  • 6,862
  • 20
  • 71
  • 155
  • `quote1_title` and `quote1` doesn't exist in your $meta array. Can you show us your $meta contents. – Adrian Apr 28 '13 at 14:56
  • 3
    What does `var_dump($meta)` show? It's probably not an array. – Marc B Apr 28 '13 at 14:57
  • This is a plugin for a Wordpress site: `$meta = get_post_meta($post->ID,'_quote_source',TRUE);` – Henrik Petterson Apr 28 '13 at 15:00
  • 3
    The message is clear (`$meta` is a string, not an array) and Marc's tip (`var_dump()`) is the way to know for sure. The fact that code belongs to an undisclosed Wordpress plug-in doesn't change this because Wordpress plugins are PHP code as well. – Álvaro González Apr 28 '13 at 15:10
  • 1
    When executing `var_dump($meta)` on a page where there were nothing written in the quote fields, it shows `NULL`. This is when I receive the warning. However, on pages where the quote fields have been filled it, it works fine. How do I solve this? – Henrik Petterson Apr 28 '13 at 15:18

2 Answers2

7

You need to make sure, that $meta is actually of type array. The warning explicitly tells you, that $meta seems to be a string and not an array

Illegal string offset
        ^^^^^^

To avoid this error you may also check for the needed fields

for($i = 0; $i < 3; $i++) {
    if ( !is_array($meta) || !array_key_exists('quote'. ($i+1), $meta) ){
         continue;
    }
    // your code
}
MatthiasLaug
  • 2,924
  • 6
  • 28
  • 43
  • When executing `var_dump($meta)` on a page where there were nothing written in the quote fields, it shows `NULL`. This is when I receive the warning. However, on pages where the quote fields have been filled it, it works fine. How do I solve this? – Henrik Petterson Apr 28 '13 at 15:18
  • check for null before: `isset($meta['quote'. ($i+1)])` oder `array_key_exists('quote'. ($i+1), $meta)` – MatthiasLaug Apr 28 '13 at 15:22
  • Can you please update your answer with this suggestion. – Henrik Petterson Apr 28 '13 at 15:30
  • I have updated the answer accordingly – MatthiasLaug Apr 28 '13 at 15:32
  • Works great! Thank you, I have one final request before I accept this as correct. Can you please explain what this new IF statement does in detail? I just want to understand the solution. – Henrik Petterson Apr 28 '13 at 15:37
  • 1
    Also, it appears that other people using this plugin are experiencing the same errors, if you see this thread: http://wordpress.org/support/topic/plugin-quote-source-debug-shows-error it is suggested to add `if(!empty)` call before running the for-loop. That is what we did here, correct? – Henrik Petterson Apr 28 '13 at 15:41
  • it is actually doing two things. 1. check if `$meta` is an array (`is_array`) or 2. that the key exists with `array_key_exists` in the array `$meta`. – MatthiasLaug Apr 28 '13 at 15:44
  • So this solution will make that part of the script stop running on pages where the meta is empty, correct? – Henrik Petterson Apr 28 '13 at 15:45
  • it will not stop, just skip that part of the code and I would use `isset` instead of `empty` which is more used for strings – MatthiasLaug Apr 28 '13 at 15:46
  • So in this case, is the solution posted at that thread any different to the one that we have here? I am asking this because I will be posting this solution there for others to use (I will credit you of course). – Henrik Petterson Apr 28 '13 at 15:54
  • yes that should solve the problem stated in the thread – MatthiasLaug Apr 28 '13 at 15:56
0

If $meta is null whenever there's no data to process:

if( !is_null($meta) ){
    for($i = 0; $i < 3; $i++) {
        // ...
    }
}

You should be able to do more checks if necessary. That depends on what that get_post_meta() function is designed to return.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • 2
    It appears that other people are having the same problem with this plugin: http://wordpress.org/support/topic/plugin-quote-source-debug-shows-error In that thread, it is suggested to add `if(!empty)` call before running the for-loop. Can you kindly update your answer with this fix and I will accept it as correct. – Henrik Petterson Apr 28 '13 at 15:29
  • 1
    Feel free to answer your own question, Stack Overflow actually encourages it. But if `$meta` is null my answer should be just fine. – Álvaro González Apr 28 '13 at 15:32
  • It is strange, MatthiasLaug's solution works. Thank you regardless for the effort. – Henrik Petterson Apr 28 '13 at 15:38
  • It isn't strange. They're endless ways to fix one same problem. That's what I mean with doing more checks: you'll need to do more or less checks depending on what `get_post_meta()` can return. Just try to understand each answer you've got and use the one that makes more sense to you. – Álvaro González Apr 28 '13 at 15:40