News Feed
Sections




News Archive
Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

PHP Tip-a-Day:
PHP Tutorial Convoluted Code - Combining Ternary Operators and Anonymous Functions
May 30, 2012 @ 10:09:40

On the PHP Tip-a-Day site Greg Bulmash shares a bit of "convoluted code" that could potentially cause confusion in the future maintenance of your application - combining ternary with anonymous functions.

Following on yesterday's post about chaining if statements without brackets on a single line, I tried to explore other ways to perform this "test if the variable is set, then do a comparison if it is" logic. I created one of the most convoluted lines of code I've ever written. It's no SQL join that spans 5 whiteboards, but it's pretty unreadable.

His example uses not just one ternary comparison, but nested ones with the anonymous function as the first condition. He points out that, if you're not careful with this method and make both sides anonymous functions, you could be in for a "cannot be converted to string" error on the closure side.

I'm sure there might be a very good reason to put two anonymous functions in a ternary operator, but I can't think of one at the moment. It's a fairly ugly proposition.
0 comments voice your opinion now!
ternary nested anonymous function closure compare


blog comments powered by Disqus

Similar Posts

PHPCodeBase.com: PHP Magic Function : glob()

Recess Blog: Functional PHP 5.3 Part I - What are Anonymous Functions and Closures?

Mark Kimsal's Blog: Addslashes(): don't call it a comeback

Saltwater.eu: PHP Cloud Management: Orchestra vs. PHP Fog

Karl Katzke's Blog: PHP: Symfony vs. Zend


Community Events

Don't see your event here?
Let us know!


php7 community example library interview series performance podcast laravel list conference framework release introduction symfony2 opinion version extension api voicesoftheelephpant

All content copyright, 2015 PHPDeveloper.org :: info@phpdeveloper.org - Powered by the Solar PHP Framework