News Feed
Sections




News Archive
feed this:

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


PHPMaster.com:
Using the Ternary Operator
November 01, 2011 @ 09:43:35

On PHPMaster.com today there's a new tutorial showing the use of a sometimes overlooked (but very handy) alternate syntax that PHP includes - the ternary operator, a short-hand if/else.

You're probably already familiar with PHP's if statement. It's very similar to its counterparts in many other programming languages and represents one of the most fundamental concepts in programming. [...] But there's a way to build on this concept and increase your $coolFactor a bit in the process. Allow me to introduce you to the ternary operator, which serves as a shorthand notation for if statements.

They introduce the ternary operator's syntax, the ":" and "?" operators and includes a few pieces of code showing its use. Thankfully they also include a warning - don't overuse or abuse it...and especially don't nest them - that just leads to headaches.

0 comments voice your opinion now!
ternary if else shorthand alternate syntax


Fabien Potencier's Blog:
The PHP Ternary Operator Fast or not?
July 18, 2011 @ 10:35:36

In a new post Fabien Potencier looks at the ternary operator in PHP and wonders which is faster - using it or not (well, sort of...)

People like micro-optimizations. They are easy to understand, easy to apply... and useless. But some time ago, while reviewing pull requests for Twig, I read an interesting discussion about the performance of the ternary operator in PHP (thanks to @nikic for the investigation). Do you know which [example] snippet is the fastest (of course, they do exactly the same)? The right answer is: it depends.

He notes that it's all about the data being worked with. As some of his tests show (testing code included) there does end up being a difference between using it on a small and large dataset. After investigation, it was found that the ternary operator copies (copy-on-write) the value versus an "if" that just evaluates. He also mentions the new "?" version of the ternary syntax in PHP 5.3, but notes it still suffers from the same issue.

0 comments voice your opinion now!
ternary optimize speed dataset copyonwrite


Erling Alf Ellingsen's Blog:
PHP Must Die
January 11, 2010 @ 13:49:41

In a (slightly inflammatory) post to his blog today Erling Alf Ellingsen shares why he thinks that "PHP must die", mostly due to some of the inconsistencies his has with other languages.

His examples include:

  • String vs. numeric handling
  • That PHP supports octal numbers "by accident"
  • A lexer bug with hex values
  • A parser bug involving the ternary operator

Comments on the post include those supporting the "die" opinion - that PHP just doesn't have it together like other languages - and those taking a bit more balanced approach on PHP's strengths and weaknesses.

1 comment voice your opinion now!
opinion lexer parser octal ternary


Ask About PHP:
PHP's alternative syntax and why it's useful
April 16, 2009 @ 07:50:56

On the "Ask About PHP" blog today there's a new post looking at some of the alternative syntaxes that PHP has to offer for control structures.

PHP offers an alternative way to write their control structures for as long as I've remembered. It basically does away with the curly brackets and replaces the opening curly with a colon (:) and the closing with '˜end'-whatever. I have to be honest and say I've never really found a need to use the alternative syntax, simply because I'm so used to using the curly braces after so many years of using PHP. Plus, it's less typing!

The one specifically mentioned deals with "if" evaluations. There's two other options to the usual braces method - one being a shorthand using colons and a semicolon and the other using the ternary syntax.

0 comments voice your opinion now!
alternative syntax control structure if example colon ternary


PHPBuilder.com:
The Ternary Conditional Operator
March 07, 2008 @ 12:04:00

The PHPBuilder.com site has a quick reminder about a handy little bit of functionality PHP has to make evaluations quicker - the ternary operator.

This allows you to check for the existence of a variable (or check that the variable has a valid value) and assign a value accordingly. This is very useful when you are dealing with $_GET, $_POST, $_SESSION etc. variables, because you don't know whether the incoming variable will exist, and if it doesn't you might want to assign a default value.

An example is included and explained - evaluating an index in the _GET superglobal to see if it exists. It returns either the value itself or a false.

0 comments voice your opinion now!
ternary conditional operator structure evaluate


WebReference.com:
Unary, Binary, and Ternary Operators in PHP
June 27, 2007 @ 14:51:46

WebReference.com has a new tutorial that looks at the difference sorts of operators that PHP has to offer - unary, binary and ternary - to help you with the logic in your applications.

An operator is a special character or combination of characters that operates on variables. There are 3 types of operators in PHP: unary, binary and ternary. They can be used to manipulate a variable with up to 3 arguments at a time. This article wasn't written to discuss the meaning and usage of each operator in PHP, but rather to explain the differences between these types of operators and to give examples about how each functions.

They look at the different types of operators (and include examples for each) - unary (like ! or ++), binary (things like + / == and &&) and a special case - ternary. This uses two characters in conjunction (the question mark and colon) to replace a simple sort of If statement.

0 comments voice your opinion now!
operator binary unary ternary example tutorial operator binary unary ternary example tutorial



Community Events





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


zendserver introduction language tips framework release list update laravel deployment interview install podcast package community library opinion series api symfony

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