views:

1184

answers:

10

Hay All, I've converting a lot of my code to the short hand IF statement to cut down loading time/code.

Most of these have been IF statements with ELSE's. However i can see to get it to work on just plain IF statements

($this->left_eye_sph >= 0) ? $this->transpose_left_eye()

Any suggestions?

+9  A: 

Ternary expressions must have an ELSE piece. They cannot live without the : .

EDIT: It depends a bit on the context, but you could do something like this

($this->left_eye_sph >= 0) ? $this->transpose_left_eye() : NULL;

I would prefer

if($this->left_eye_sph >= 0)
    $this->transpose_left_eye();
Lex
could my code be used and add a blank else?
dotty
Yep, that's reason it's called a *ternary* operator — it takes three operands.
Chuck
I decided to go with this answerif($this->left_eye_sph >= 0) $this->transpose_left_eye();a one line approach
dotty
I likw the way that loads of people tell you not to do this but you do it anyway. You stick to your gins and write crazy unreadable code - that'll teach them :)
Davy
+2  A: 

You can just evaluate anything, e.g. a literal zero

($this->left_eye_sph >= 0) ? $this->transpose_left_eye() : 0;

But I would question your motives in converting if statements to conditional operators just because you think it will be more optimal. This surely can't be the number one bottleneck in your code?

In fact, even if you profiled your code and found that, unbelievably, "if" statements were the most time consuming call, then you can stop optimizing! You're done! You win the Internets!

Paul Dixon
Agreed. Without some accurate profiling, you have no idea if this sacrifice to readability is actually worth it.
Mark Biek
+2  A: 

It's called "ternary operator" because it has three operands. :)

Replacing regular ifs with it will not speed your code up. I'd expect the opposite, but the differences would be hardly measurable. It also makes your code less readable.

Lukáš Lalinský
+3  A: 

Also, for what it's worth, I have never read anywhere that shorthand is faster then simply wrinting if/else, also, this is widely considered less readable in most cases (not all!).

You should run a profiler and see where the real bottlenecks are, even if tenerary is faster I highly doubt it will make much of a difference.

Pim Jager
A: 

If these are only ever used in a statement context, you can use a dummy value for the else expression:

($this->left_eye_sph >= 0) ? $this->transpose_left_eye() : 0

I forgot what PHP uses for null/nothing/None - use that.

Daren Thomas
+2  A: 

To me, this type of refactoring don't save you a lot of time, if it save you time at all.

By the way, you can convert the if without the else branch with the && operator

($this->left_eye_sph >= 0) && $this->transpose_left_eye();
Eineki
Lex
@Lex: nope. The difference between the two is the priority of execution (`and` and `or` have lower priority). See <http://de2.php.net/manual/en/language.operators.precedence.php>
Konrad Rudolph
A: 

Dotty, it doesn't take very long at all for a lexer to pass over the extra white space, or to parse the "if()" versus ":".

Not only that, I think (I could be wrong) the PHP isn't usually parsed each time it is executed, so it really shouldn't make a difference.

Snarfblam
+1  A: 

Why ?: should be faster than plain if...else? First: the gain (if any) is negligible. Second: there is no gain. Running the following code does not show any performance gain (on my system the if...else version is even quicker).

$start = microtime(true);
for ($n = 1; $n < 10000000; ++$n)
    if (($n & 1) == 1)
        $odd = true;
    else
        $odd = false;
$elapsed = microtime(true) - $start;

echo "Using  if  needs $elapsed sec\n";

$start = microtime(true);
for ($n = 1; $n < 10000000; ++$n)
    $odd = (($n & 1) == 1) ? true : false;
$elapsed = microtime(true) - $start;

echo "Using  ?:  needs $elapsed sec\n";
ntd
+14  A: 

loading time, as in from the server. a 20k page loads faster than a 40k page

That’s just wrong. PHP code is not transferred from the server, it’s handled on the server. And I promise you, personally, that you won’t gain a single microsecond from your so-called “optimization”. Making PHP files minimally smaller will have no effect whatsoever on the performance of the PHP interpreter.

why the downvote?

Because, to put it bluntly, your plan seems stupid once you know that

  1. PHP code isn’t transferred to the client,
  2. Loading the PHP code form disc is easily the fastest part of running it
  3. by several orders of magnitude!
  4. What you want simply doesn’t work, at all, as a simple test will conclusively demonstrate.
  5. Even if all that were wrong, it would still be a bad idea because no trivial performance gain could justify such a drastic deterioration of the code quality.

(And I suspect that the downvoter thinks you’re pulling our leg.)

Konrad Rudolph
A: 

Ternary statements do not need an else clause.

Use $this->left_eye_sph >= 0 ?: $this->transpose_left_eye()

Simon
Forgot to add that the ?: (without false) is only available in PHP v5.3
Simon