note 106235 added to function.eval

From: Date: Thu, 20 Oct 2011 12:23:56 +0000
Subject: note 106235 added to function.eval
Groups: php.notes 
Request: Send a blank email to php-notes+get-184016@lists.php.net to get a copy of this message
Hey guys. With the eval()-function it is possible to create functions with specific names. Imagine if you have an Array and want to create some function of these names. <?php // Setting up the names for the functions. $names = Array ( "foo", "bar", "test", "foobar"); $prefix = "func_"; // Running a loop for the names and setting up the functions. foreach ($names as $j) { $function = $prefix.$j; $code = "function ".$function." () { echo '".$j."<br />'; }"; eval($code); } // Now we can run the functions easily now. func_foo(); func_bar(); func_test(); func_foobar(); ?> Output will be: foo bar test foobar -------- The point is that you don't have to run the function from a variable [create_function()]. I hope this is helpful for someone. ;) Kevin ---- Server IP: 69.147.83.197 Probable Submitter: 91.38.19.112 ---- Manual Page -- http://www.php.net/manual/en/function.eval.php Edit -- https://master.php.net/note/edit/106235 Del: integrated -- https://master.php.net/note/delete/106235/integrated Del: useless -- https://master.php.net/note/delete/106235/useless Del: bad code -- https://master.php.net/note/delete/106235/bad+code Del: spam -- https://master.php.net/note/delete/106235/spam Del: non-english -- https://master.php.net/note/delete/106235/non-english Del: in docs -- https://master.php.net/note/delete/106235/in+docs Del: other reasons-- https://master.php.net/note/delete/106235 Reject -- https://master.php.net/note/reject/106235 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184016) next »