如何修复这些非法字符串偏移警告?
Context: On one of the sites where my WordPress plugin is installed I'm seeing a series of PHP warnings, but I'm not entirely sure why this is happening. I'm hoping someone here can help me figure out how to solve this warning.
Code Sample:
function my_function( $array ) {
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
The Warning:
Warning: Illegal string offset 'where' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 14
Warning: Illegal string offset 'echo' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 15
Warning: Cannot assign an empty string to a string offset in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 15
Warning: Illegal string offset 'content' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16
Warning: Cannot assign an empty string to a string offset in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 16
Warning: Illegal string offset 'shortcode' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 18
Warning: Illegal string offset 'devs' in /home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php on line 19
For some reason, it's throwing a warning every time it encounters one of the indices in the array. How do I fix this? Thanks!
上下文: strong>在我安装WordPress插件的其中一个网站上,我看到了 一系列的PHP警告,但我不完全确定为什么会发生这种情况。 我希望有人可以帮我弄清楚如何解决这个警告。 p>
代码示例: strong> p>
警告: strong> p>
警告:非法字符串 在第14行的
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
中偏移'where' p>
警告:第15行
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
中的非法字符串偏移'echo' p> \ n
警告:无法将空字符串分配给
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php中的字符串偏移量 第15行 p>
警告:
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/中的非法字符串偏移'内容' 第16行的shortcodes.php
p>
警告:无法分配 n在第16行的
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
中将空字符串转换为字符串偏移量 p>
\ n 警告:第18行
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
中的非法字符串偏移'短代码' p>
警告:
/home/playitda/public_html/domain.com/wp-content/plugins/my_plugin/functions/frontend-output/shortcodes.php
中的非法字符串偏移'devs' 第19行 p>
blockquote>
出于某种原因,每次遇到数组中的一个索引时都会发出警告。 我该如何解决? 谢谢! p>
div> function my_function($ array){
if(!isset($ array ['where'])){$ array ['where'] ='after'; }
if(!isset($ array ['echo'])){$ array ['echo'] = false; }
if(!isset($ array ['content'])){$ array ['content'] = false; }
$ array ['shortcode'] = true;
$ array ['devs'] = true;
返回social_warfare($ array);
}
add_shortcode('my_shortcode','my_function ');
code> pre>
The usage of the function is_array()
in the beginning of your function can give you the insurance, that, if someone pass you something else than an array, the variable is reinitialised as an empty array.
Unsetting or nulling it before doing that is useless, because, as of PHP 5.3, PHP does have a garbage collector mechanism.
/**
* @params array $array
*/
function my_function( $array ) {
if ( ! is_array ( $array ) ) { $array = [] };
/**
* Or, if you don't like the short array notation:
* if ( ! is_array ( $array ) ) { $array = array(); };
*/
if ( ! isset( $array['where'] ) ) { $array['where'] = 'after'; }
if ( ! isset( $array['echo'] ) ) { $array['echo'] = false; }
if ( ! isset( $array['content'] ) ) { $array['content'] = false; }
$array['shortcode'] = true;
$array['devs'] = true;
return social_warfare( $array );
}
add_shortcode( 'my_shortcode', 'my_function' );
It looks like the function is expecting an array and is getting a string instead.
You can require an array in the function definition.
function my_function(array $array) { ...
Then if you call it with something other than an array, you'll get a TypeError.
Unfortunately, there will still be a problem somewhere else in your code, where something you thought was an array is actually a string.
Setting up your function like this will generate an error earlier, which is good, because it will make it more obvious where the problem is. If you modify your function to ignore the problem instead, it will probably just create more confusing behavior and potentially different errors.