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

NetTuts.com:
The Ternary Operator in PHP
May 29, 2015 @ 16:41:44

If you're relatively new to the PHP language, there's an interesting "shorthand" method for evaluation that you may not know about. In this new tutorial from NetTuts.com they show you this handy method - the ternary operator.

The ternary operator allows us to simplify some PHP conditional statements. We'll see how it can be used, with test-driven development and refactoring, to simplify code.

While the tutorial is largely a screencast, they do provide a quick summary of what the operator is mainly used for and an example for quick reference.

tagged: ternary operator tutorial screencast introduction evaluation

Link: http://code.tutsplus.com/tutorials/the-ternary-operator-in-php--cms-24010

PHP Tip-a-Day:
PHP Tutorial: Convoluted Code - Combining Ternary Operators and Anonymous Functions
May 30, 2012 @ 15: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.
tagged: ternary nested anonymous function closure compare

Link:

PHPMaster.com:
Using the Ternary Operator
Nov 01, 2011 @ 14: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.

tagged: ternary if else shorthand alternate syntax

Link:

Fabien Potencier's Blog:
The PHP Ternary Operator: Fast or not?
Jul 18, 2011 @ 15: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.

tagged: ternary optimize speed dataset copyonwrite

Link:

Erling Alf Ellingsen's Blog:
PHP Must Die
Jan 11, 2010 @ 19: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.

tagged: opinion lexer parser octal ternary

Link:

Ask About PHP:
PHP's alternative syntax and why it’s useful
Apr 16, 2009 @ 12: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.

tagged: alternative syntax control structure if example colon ternary

Link:

PHPBuilder.com:
The Ternary Conditional Operator
Mar 07, 2008 @ 18: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.

tagged: ternary conditional operator structure evaluate

Link:

WebReference.com:
Unary, Binary, and Ternary Operators in PHP
Jun 27, 2007 @ 19: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.

tagged: operator binary unary ternary example tutorial operator binary unary ternary example tutorial

Link:

WebReference.com:
Unary, Binary, and Ternary Operators in PHP
Jun 27, 2007 @ 19: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.

tagged: operator binary unary ternary example tutorial operator binary unary ternary example tutorial

Link:


Trending Topics: