Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>you can do <strong>conditional</strong> function definiton.</p> <pre><code>if( !function_exists('baz') ) { function baz( $args ){ echo $args; } } </code></pre> <p>But at present, a function becomes a brick when defined. </p> <p>You can use <code>create_function</code>, but I would suggest you <strong>DONT</strong> because it is slow, uses lots of memory, doesn't get free()'d untill php exits, and is a security hole as big as eval(). </p> <p>Wait till PHP5.3, where we have "closures" <a href="http://wiki.php.net/rfc/closures" rel="nofollow noreferrer">http://wiki.php.net/rfc/closures</a></p> <p>Then you'll be permitted to do </p> <pre><code>if( !isset( $baz ) ) { $baz = function( $args ) { echo $args; } } $baz('hello'); $baz = function( $args ) { echo $args + "world"; } $baz('hello'); </code></pre> <p>Upon further reading, this is the effect you want. </p> <pre><code>$fname = 'f_first'; function f_first( $even ) { global $fname; doExpensiveStuff(); $fname = 'f_others'; $fname( $even ); /* code */ } function f_others( $odd ) { print "&lt;b&gt;".$odd."&lt;/b&gt;"; } foreach( $blah as $i=&gt;$v ) { $fname($v); } </code></pre> <p>It'll do what you want, but the call might be a bit more expensive than a normal function call. </p> <p>In PHP5.3 This should be valid too:</p> <pre><code>$func = function( $x ) use ( $func ) { doexpensive(); $func = function( $y ) { print "&lt;b&gt;".$y."&lt;/b&gt;"; } $func($x); } foreach( range(1..200) as $i=&gt;$v ) { $func( $v ); } </code></pre> <p>( Personally, I think of course that all these neat tricks are going to be epically slower than your earlier comparison of 2 positive bits. ;) ) </p> <p>If you're really concerned about getting the <em>best</em> speed everywhere</p> <pre><code>$data = // some array structure doslowthing(); foreach( $data as $i =&gt; $v ) { // code here } </code></pre> <p>You may not be able to do that however, but you've not given enough scope to clarify. If you can do that however, then well, simple answers are often the best :)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload