Warning: Illegal string offset in PHP 5.4 [duplicate]

匿名 (未验证) 提交于 2019-12-03 01:58:03

问题:

This question already has an answer here:

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 

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?

回答1:

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

Illegal string offset         ^^^^^^ 

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

for($i = 0; $i 


回答2:

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

if( !is_null($meta) ){     for($i = 0; $i 

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



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!