4

As we know, if we are using javascript or python, we can use the below statement to get a variable, or its default.

// javascript
alert(a || 'default');

# python
print(a or 'default');

While in php, we may have to call the below:

echo $a ? $a : 'default';

And if the $a is a very long statement, the case is even worse:

echo (
    we_made_many_calculation_here_and_this_is_the_result() ?
    we_made_many_calculation_here_and_this_is_the_result() :
    'default'
);

or

var $result = we_made_many_calculation_here_and_this_is_the_result();
echo $result ? $result : 'default';

any of the above, I think it is not neat.

And I'm blind to find any answer, to find a statement or built-in function to simplifies the work. But after trying to search many ways, I can't find the answer.

So please help.

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189

3 Answers3

6

Relating to this issue: http://php.net/manual/en/language.operators.logical.php#115208

So, see the documentation of the ternary operator, it is possible to use:

echo $a ?: 'defaults for a';

The expression (expr1) ? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.

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.


See: http://www.php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
3

Doing $a when $a is undefined will still give you an error. Unfortunately, the only proper way to do it when handling a variable is:

echo isset($a) ? $a: 'default';

When handling the long function, you still need to check for the conditions that you want to check, because if it returned false, you will still fall into default.

var $result = we_made_many_calculation_here_and_this_is_the_result(); // false
echo $result ? $result : 'default'; // echos default

You need:

var $result = we_made_many_calculation_here_and_this_is_the_result(); 
echo !is_null($result) ? $result : 'default'; 

This is a sad limitation in what php interprets as false. You can see a full list here.

PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
  • I'm searching for a way to made it short, not make it all right. But thank you. – Alfred Huang Jul 08 '15 at 15:40
  • In php, I think you would see more of these statements happening on separate lines rather on the same line you echo it on. You have to think and write your code a little differently based on the language. PHP doesn't support as much shorthand as the other languages. – PressingOnAlways Jul 08 '15 at 15:42
  • Thank you, but `?:` operator is exactly what I want. – Alfred Huang Jul 08 '15 at 15:45
1

Why not try doing it the other way round, and start off by setting the variable to its default value?

$a = "default";
...
echo $a;

Then you don't need to check if it's set or not - just use the variable.

This has the added bonus of it then preventing the (unfortunately very common, and potentially tricky to track down) problem of using an unassigned variable.

Eborbob
  • 1,905
  • 1
  • 15
  • 30