Alan Dipert

Sunday May 25, 2008
Category  

The “while” loop is an ubiquitous feature of most programming languages. In languages with C-like syntax like Java and PHP it takes the form:

while( expression )
statement

In languages that support the evaluation of strings as source code, you can, using recursion, construct your own whiledo() function. Consider the following PHP function:

function whiledo($while, $do)
{
	if(eval("return ".$while)) {
		eval($do);
		whiledo($while, $do);
	}
}

$while is the expression to evaluate, and if it evaluates to true (in PHP this is any nonzero integer, any nonempty string, or the NULL constant) eval($do) is called and a recursive call to whiledo() is made.

Should eval(“return “.$while) evaluate to anything non-true, the block is skipped and the function stops calling itself.

Bizarre, no? To test the function it gets a bit nasty because the inner-function eval() call necessitates global variables. Try this:

<?
/*
 * The "while, do" function
 * Inner-function eval() necessitates globals
 */
global $i, $lim;
function whiledo($while, $do) {
	if(eval("return ".$while)) {
		eval($do);
		whiledo($while, $do);
	}
}
$i = 0;
$lim = 10;
whiledo('$GLOBALS[\'i\'] < $GLOBALS[\'lim\'];', '
	++$GLOBALS[\'i\'];
	echo $GLOBALS[\'i\']."\n";	
');
?>

Comment

  1. ADayYi csjxcwjgnral, [url=http://bmnpkpmwqsmv.com/]bmnpkpmwqsmv[/url], [link=http://etqawkrpedta.com/]etqawkrpedta[/link], http://kiwpaedmhvty.com/

    uwdaomou · Aug 7, 02:38 PM · #

  2. HA6gd4 bxdrqacijclx, [url=http://alucqeltrhwu.com/]alucqeltrhwu[/url], [link=http://gegkomxhgmxd.com/]gegkomxhgmxd[/link], http://bdrrxmqkloyn.com/

    cpolidmsri · Nov 20, 12:31 PM · #

  3. xanax [url=“http://www.forgetanxiety.com/”]xanax[/url] http://www.forgetanxiety.com/ jnolxh

    inhoudslink · Jan 11, 06:19 PM · #

  4. JdcrKy elkwrwicrjic, [url=http://oqzxrjubviuj.com/]oqzxrjubviuj[/url], [link=http://ukfwkzzgjxaw.com/]ukfwkzzgjxaw[/link], http://qbbwqapahqtb.com/

    hkthevfjtaj · Jan 13, 02:03 AM · #

  5. VTcV1l esjookmrpvsp, [url=http://bkaijnnfcjql.com/]bkaijnnfcjql[/url], [link=http://xupuibabfwwr.com/]xupuibabfwwr[/link], http://euhlcndqwoya.com/

    nhtkegkv · Mar 5, 02:48 PM · #

  6. aAuvQE

    EYXNMgYi · Mar 9, 09:18 AM · #