Jeśli jesteś właścicielem tej strony, możesz wyłączyć reklamę poniżej zmieniając pakiet na PRO lub VIP w panelu naszego hostingu już od 4zł!

PHP Tip-a-Day: PHP Tutorial: Convoluted Code – Combining Ternary Operators and Anonymous Functions

PHP Tip-a-Day: PHP Tutorial: Convoluted Code – Combining Ternary Operators and Anonymous Functions

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.

Source: http://www.phpdeveloper.org/news/18023

<!–
var d = new Date();
r = escape(d.getTime()*Math.random());
document.writeln('’);
//–>

Posted Maj 31st, 2012 in WEB and PHP Development.

Leave a response: