views:

326

answers:

6

Does anybody know if there is a shortcut for the following statement in PHP?

$output = isset($some_value) ? $some_value : "Some Value Not Set";
echo $output;

This something that I often run into, where $some_value is actually very long and possibly involves a function, such as:

$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value) ? $this->db->get_where('my_db',array('id'=>$id))->row()->some_value) : "Some Value Not Set";
echo $output;

It seems that there should be an operator or function that does this. I could easily write one, and I am not looking for that answer, but rather if anybody knows of a built-in shortcut.

+4  A: 

You should be setting a variable with the results of your database call before using the conditional operator for this purpose. Your example makes the database call twice.

For example:

$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value);
$output = $output ? $output : "Some Value Not Set";
echo $output;

And with that established, this is a good case where it's really wiser to not use the conditional operator, which really isn't meant to be used as a general purpose if-then shortcut.

eyelidlessness
you could save one line by doing the first assignment inside the test: http://stackoverflow.com/questions/1574273/conditional-operator-shortcut-in-php/1574294#1574294
ax
+3  A: 

I do believe that the conditional operator is the shortcut :) For the sake of saving function calls and readability, I suggest saving the value to a variable first.

$some_value = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value);
$output = $some_value ? $some_value : "Some Value Not Set";
echo $output;
Paulo
+5  A: 

Since PHP 5.3, it is possible to leave out the middle part of the ternary operator. Expression expr1 ?: expr3 returns expr1 if expr1 evaluates to TRUE, and expr3 otherwise.

http://php.net/manual/en/language.operators.comparison.php

chelmertz
Wow, Zend's stupidity finally comes full circle. In their ignorance they called the conditional operator "the ternary operator" in their documentation, and now they've implemented a form of it where **IT IS A BINARY OPERATOR NOT A TERNARY ONE**. It's like Christmas for irony.
chaos
Despite the naming issue, that is the sort of functionality that I am looking for, but I guess it is relatively "new" to PHP.
jeffp
i guess if there is only one ternary operator in a language, calling it "the (php) ternary operator" is not completely wrong. also, considering that "expr1 ?: expr3" is just syntactic sugar for "expr1 ? expr1 : expr3", with the first expr1 being evaluated boolean and the second "by value", and that this is being used a lot of times, i wouldn't be so strict about calling it binary operator. did you consider filing a (documentation) bug report?
ax
Let's call it the ternary obfuscator.
GZipp
ax: Is `expr1 ?: expr3` really just syntactic sugar? If `expr1` is a function call, will it execute twice (meaning this is really syntactic sugar) or once (meaning the `?:` operator behaves differently without `expr2`)?
eyelidlessness
ax
I think chaos' response may be hyperbolic, but its point isn't wrong. Ultimately, the problem isn't just the documentation, but the overloading of the behavior of the conditional operator to produce unexpected behavior. If you are familiar with the operator as ternary, then you will be surprised by its behavior **regardless** of whether it executes `expr1` once or twice. That is **absolutely** a stupid language change. It **never** helps programmers write clear code when you introduce an operator that behaves unexpectedly.
eyelidlessness
chaos
+1  A: 

Best way is to:

$output = $this->db->get_where('my_db',array('id'=>$id))->row()->some_value)
echo $output =($output)?$output:"Some Value Not Set";

Only executes once then!

Lizard
+4  A: 

if you need to reuse the long expression from the test after the ?, you can assign it to a variable inside the test (because assignments are expressions returning the assigned value) and use this variable after the ?:

$output = ($some_value = $this->db->get_where('my_db', array('id' => $id))->row()->some_value))
  ? $some_value 
  : "Some Value Not Set";
echo $output;
ax
+1 for correct solution, but generally this code is less clear and concise than simply declaring the variable before the ternary operator. Saving an extra line of code is decreasing readability.
cballou
it might be less clear, but i don't think it is less concise :)
ax
+3  A: 

You seem to be afraid of whitespace. Use it! Liberally! Your code is much eaiser to read if you add a space before and after the question mark and the colon, respectively. If your statements get too long, add a newline. Try it, it won't hurt you.

innaM