create-function

PHP create_function with nested arrays

强颜欢笑 提交于 2019-12-25 08:26:01
问题 I need to use create_function because the server the application is running on has an outdated version of PHP. The issue is that the items I pass into usort are arrays and I need to sort by a value within the array: $sort_dist = create_function( '$a, $b', " if( $a['order-dir'] == 'asc' ) { return $a['distance'] > $b['distance']; } else { return $a['distance'] < $b['distance']; }" ); usort( $items, $sort_dist ); Gives an error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T

PHP: How to make variable visible in create_function()?

社会主义新天地 提交于 2019-12-20 07:12:57
问题 This code: $t = 100; $str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/", create_function( '$matches', 'return $matches[1] + $t;' ), $func); How to make $t visible from create_function() in preg_replace() function? 回答1: An anonymous function would work, while making use of the use syntax: $t = 100; $str = preg_replace_callback("/(Name[A-Z]+[0-9]*)/", function($matches) use($t) // $t will now be visible inside of the function { return $matches[1] + $t; }, $func); 回答2: You can't make the

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

Memory leak?! Is Garbage Collector doing right when using 'create_function' within 'array_map'?

守給你的承諾、 提交于 2019-12-12 09:42:12
问题 I found following solution here on StackOverflow to get an array of a specific object property from array of objects: PHP - Extracting a property from an array of objects The proposed solution is to use array_map and within create a function with create_function as following: $catIds = array_map(create_function('$o', 'return $o->id;'), $objects); What happens?: array_map runs through each array element in this case a stdClass object. First it creates a function like this: function($o) {

Error SQL0104 when creating a function in System i V7R1

為{幸葍}努か 提交于 2019-12-08 20:08:59
I'm creating a SQL function on System i V7R1: CREATE FUNCTION MYSCHEMA.GROUPDIBAS(v_code VARCHAR(50)) RETURNS VARCHAR(2048) LANGUAGE SQL BEGIN DECLARE str VARCHAR(2048); SET str = ''; FOR row AS ( SELECT FIELD2 FROM MYSCHEMA.DIBAS WHERE FIELD1 = v_code ) DO SET str = 'Bubi'; --I removed many statements to make clear the problem doesn't come from them END FOR; RETURN str; END ; I execute it with "Run SQL script" tool, which is part of the iSeries Navigator V7R1. It works on another V7R1 server (using iSeries Navigator V5R4), but not in that one where I'm working now. It fails with this message:

Error SQL0104 when creating a function in System i V7R1

大憨熊 提交于 2019-12-08 08:01:45
问题 I'm creating a SQL function on System i V7R1: CREATE FUNCTION MYSCHEMA.GROUPDIBAS(v_code VARCHAR(50)) RETURNS VARCHAR(2048) LANGUAGE SQL BEGIN DECLARE str VARCHAR(2048); SET str = ''; FOR row AS ( SELECT FIELD2 FROM MYSCHEMA.DIBAS WHERE FIELD1 = v_code ) DO SET str = 'Bubi'; --I removed many statements to make clear the problem doesn't come from them END FOR; RETURN str; END ; I execute it with "Run SQL script" tool, which is part of the iSeries Navigator V7R1. It works on another V7R1 server

PHP's create_function() versus just using eval()

烈酒焚心 提交于 2019-12-06 04:12:58
问题 In PHP you have the create_function() function which creates a unique named lambda function like this: $myFunction = create_function('$foo', 'return $foo;'); $myFunction('bar'); //Returns bar Is this actually any better (apart from being more easy) then just doing: do{ $myFunction = 'createdFunction_'.rand(); } while(function_exists($myFunction)); eval("function $myFunction(\$foo) { return \$foo; }"); $myFunction('bar'); //Returns bar Is create_function really better? (apart from the fact

PHP's create_function() versus just using eval()

淺唱寂寞╮ 提交于 2019-12-04 11:37:12
In PHP you have the create_function() function which creates a unique named lambda function like this: $myFunction = create_function('$foo', 'return $foo;'); $myFunction('bar'); //Returns bar Is this actually any better (apart from being more easy) then just doing: do{ $myFunction = 'createdFunction_'.rand(); } while(function_exists($myFunction)); eval("function $myFunction(\$foo) { return \$foo; }"); $myFunction('bar'); //Returns bar Is create_function really better? (apart from the fact that it is more easy) On my understanding of the relevant docs,[1] they both do the same thing, create

How to rewrite an example without the use of create_function?

安稳与你 提交于 2019-12-02 00:35:46
问题 When looking at PHP's create_function it says: If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. I want to recreate same functionality of create_function but using an anonymous function . I do not see how, or if I am approaching it correctly. In essence, how do I change the following so that I no longer use create_function but still can enter free-form formula that I want to be evaluated with my own parameters? $newfunc = create_function( '$a,$b', 'return

How to rewrite an example without the use of create_function?

丶灬走出姿态 提交于 2019-12-01 20:42:53
When looking at PHP's create_function it says: If you are using PHP 5.3.0 or newer a native anonymous function should be used instead. I want to recreate same functionality of create_function but using an anonymous function . I do not see how, or if I am approaching it correctly. In essence, how do I change the following so that I no longer use create_function but still can enter free-form formula that I want to be evaluated with my own parameters? $newfunc = create_function( '$a,$b', 'return "ln($a) + ln($b) = " . log($a * $b);' ); echo $newfunc(2, M_E) . "\n"; Example taken from PHP's create