0

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.

1

6 Answers 6

9

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

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

8 Comments

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.
Despite the naming issue, that is the sort of functionality that I am looking for, but I guess it is relatively "new" to PHP.
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?
Let's call it the ternary obfuscator.
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)?
|
4

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; 

2 Comments

+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.
it might be less clear, but i don't think it is less concise :)
4

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.

1 Comment

you could save one line by doing the first assignment inside the test: stackoverflow.com/questions/1574273/…
4

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.

Comments

3

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; 

Comments

1

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!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.