开发者

PHP errors out on presence of ampersand before variable

In the first foreach statement below, the presence of the ampersand before th开发者_JS百科e $widgets variable is causing older versions of PHP to choke. The problem is that the function does not work without the ampersand (and I can't recall why its required since it was developed by another developer).

My question is, why would this cause PHP 4.4 to error and why is it needed in the first place?

function exclude_widget_check( $sidebars_widgets )
{
    if(is_home())
    {
        foreach ( $sidebars_widgets as $sidebar_id => &$widgets ) {
            if ( 'my-widget' != $sidebar_id ) {
                continue;
            }
            foreach ( $widgets as $idx => $widget_id ) {
                if ( 0 === strncmp( $widget_id, 'my_slider', 6 ) ) {
                    echo 'Unset widget';
                    unset( $widgets[$idx] );
                }
            }
        }
    }
    return $sidebars_widgets;
}


It looks like you are running into issues with references. You may be dealing with problems regarding older implementations of it.


The ampersand is the "pass by reference" operator in PHP. IE, it means that when you edit $widgets, you're editing the actual element in the $sidebars_widgets array and not a copy of it.

If it's not working with older versions of PHP, I would just access the original element by index, changing this:

unset( $widgets[$idx] );

to this:

unset( $sidebar_widets[$sidebar_id][$idx] );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜