Php - understanding create_function() - passing simple variable

帅比萌擦擦* 提交于 2019-12-19 08:11:11

问题


First time I am trying to use the dynamic create_function, and up to now, not much success :-)

My function is this :

 function o99_brsa_custom_widgets() {
        global $wp_meta_boxes;
        global $o99_brsa_options;

        for($i=0; $i> count($o99_brsa_options[content]); $i++) {

            $widgt_id = 'o99_dashboard_widget_dyn' . $i;
            $widgt_name = 'obmek99 widget name' . $i;
            $out = $o99_brsa_options[content][$i];
            $f = create_function(' $out ',' global $out; echo $out;');
            do_the_widgets($widgt_id, $widgt_name, $f);
         }
    } 

The do_the_widgets() action is accepting only a direct echo and prints the content of the widget.

The $o99_brsa_options[content] is a verified array with $i elements (each is content) .

The strange thing is that the $i is working on the $widgt_id and $widgt_name but on the create_function() I get the same value printed in all widgets . ( echo $out )

It seems that I do not know how to pass a simple variable to the new function ( I am using global inside create_function(), but it helps little as for now .

So, what is my mistake / misunderstanding / misuse now :-) ??


回答1:


create_function was during the stone age , when kaᵠ used pen and paper to write applications, when PeeHaa埽 got a beer because he wrote hello would, The world is better now please use closures

$f = function ($out) {
    echo $out;
};

$f("Welcome");

You would thank me one day, But you can only use create_function if you are Gordon (The machine from the past sent here to torment us) he wrote this

$fn = create_function(
    '$x',
    'return $x; } $foo = 42; function foo($int) { return $int; '
);

See Live Demo



来源:https://stackoverflow.com/questions/16401270/php-understanding-create-function-passing-simple-variable

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