0

I've perused the questions on ternary operators vs. if/else structures, and while I understand that under normal circumstances there is no performance loss/gain in using ternary operators over if/else structures, I've not seen any mention of this situation. Language specific to PHP (but any language agnostic details are welcome) does the interpreter reassign values in situations like this:

$foo = 'bar'
$foo = strlen($foo) > 3 ? substr($foo, 0, 3) : $foo;

Since this would evaluate to $foo = $foo; is this inefficient, or does the interpreter simply overlook/discard this evaluation?

On a side note, what about:

!defined('SECURE') ? exit : null;
1
  • 1
    My face is going red from the number of answers quoting mine. Commented Jan 9, 2011 at 21:40

4 Answers 4

4

I don't know if your first example is inefficient, but it sure is pointless. I still think an if statement is clearer:

$foo = 'bar';

if (strlen($foo) > 3)
    $foo = substr($foo, 0, 3);

And while the following works, it makes no sense to place null at the end because a ternary operator is meant to be used to evaluate expressions/values, but here null does nothing other than to prevent a parse error:

!defined('SECURE') ? exit : null;

More commonly, you would see this, an example of boolean short-circuiting (or exit doesn't execute if SECURE is not defined, because the or conditional expression evaluates to true automatically once at least one condition is found to be true):

defined('SECURE') or exit;

The point I'm trying to make is this: don't use ternary conditional expressions just because you can.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks BoltClock; It certainly is clearer. I've always wondered why the third expression was necessary when using ternary operators for such circumstances. Perhaps I'm alone, but $foo = strlen($foo) > 3 ? substr($foo, 0, 3); seems very readable to me. Boolean short circuiting would be the answer here again, correct?
@TomcatExodus: Most people reserve short-circuiting for or die lines. Sounds just like a real-life death threat :)
Haha, certainly. The first scenario I pointed out is for my own code refactoring, the second for a colleagues. He's used the !defined ... for securing view files from direct access, and full if conditions seemed too verbose for him I suppose. While I've accepted the answer as it is clear, any thoughts on the PHP interpreter reassigning values like $foo = $foo;?
I'm not sure what goes on under the hood. Maybe it's smart enough to optimize the line away, maybe it's not.
@TomcatExodus: By looking at PHP sources, I think I can confirm that PHP will execute the statement $foo = $foo anyway, except if $foo is an object, the only check happening seems to be when variables are objects. This would not happen in a compiled language however, as the compiler would simply discard the assignment of a variable to itself, because it's useless. ;)
|
0

In this cases, I use the form presented by BoltClock:

if (strlen($foo) > 3) {
    $foo = substr($foo, 0, 3);
}

PHP does not implement something more simple to work in this cases, yet :/

Comments

0

The topic that using a ternary here is not optimal has already been covered above. I'm going to address your question about whether it will reassign the value:

This depends on what you call "reassigning". PHP does not optimize, so the $foo = $foo will be evaluated. On the other hand this will not cause PHP to copy the value of $foo to a new chunk of memory. Probably PHP will just increase the refcount on $foo and then immediately decrease it (though I'm not sure about the exact implementation details of self-assignment). So, even though PHP will execute the statement, it won't affect performance (unless you choose to write $foo = $foo seven million times in your code).

2 Comments

Thanks for the response nikic; For scripts that render to browser, typically I try to remain as efficient as possible, and optimize (via caching mechanisms, etc.) the logic route from request to response. However I do sometimes use PHP for CLI driven scripts, performing complex, high iteration processes (for convenience as I haven't made the transition to Perl) and that's exactly why I was curious. (7 million is high, but certainly not uncommon)
And even in that case optimizing on the assignment is nonesense. You would for example gain much more by replacing the strlen check by isset($foo[3]) :)
0

There is always short-circuiting, although as @BoltClock said, an if statement is probably more readable in my opinion, and opens the door to else if and else conditions as well.

strlen($foo) > 3 && $foo = substr($foo, 0, 3); 

The latter statement will only be executed if the former evaluates to TRUE.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.