Nice PHP code - Blog - Open Source - - php, photography and private stuff

Nice PHP code

At our weekly usergroup meeting, which is taking place just right now, we again discussed our beloved topic, the Ternary Operator. I like that thing really much and I believe it makes code much more readable in a lot of cases. Anyway, I tend to stack ternary operators and I didn't even know, that the PHP manual recommends to not do that. I think this recommendation is only valid of you don't add appropriate braces, to indicate the precendences. Anyway, while having a beer, we thought about some more "funny" things you could do with it... For example, executing mutliple statements, within one section of the operator... here is the result:

echo ( ( 1 == 1 ) ? call_user_func( create_function( '$a, $b', 'return ($a * 2) + $b;' ), 10, 15 ) : 0 );


Some more funny constructs after some more beer. I simply love lambda calculus. :)

call_user_func(($func = create_function('$func, $x', 'echo $x; return ($x < 10 ? $func($func, $x+1) : $x);')), $func, 1);

If you liked this blog post or learned something, please consider using flattr to contribute back: .



Add new comment

Fields with bold names are mandatory.