Asked  2 Years ago    Answers:  5   Viewed   46 times
($DAO->get_num_rows() == 1) ? echo("is") : echo("are");

This dose not seem to be working for me as intended, I get an error "Unexpected T_ECHO". I am expecting it to echo either 'is' or 'are'.

I have tried it without the brackets around the conditional. Am I just not able to use a ternary operator in this way?

The $DAO->get_num_rows() returns an integer value.



The Ternary operator is not identical to an if-then. You should have written it

echo ($DAO->get_num_rows() == 1) ? "is" : "are";

It returns the value in the 2nd or 3rd position. It does NOT execute the statement in the 2nd or 3rd position.

Saturday, September 24, 2022

When your first argument is null, they're basically the same except that the null coalescing won't output an E_NOTICE when you have an undefined variable. The PHP 7.0 migration docs has this to say:

The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.

Here's some example code to demonstrate this:


$a = null;

print $a ?? 'b'; // b
print "n";

print $a ?: 'b'; // b
print "n";

print $c ?? 'a'; // a
print "n";

print $c ?: 'a'; // Notice: Undefined variable: c in /in/apAIb on line 14
print "n";

$b = array('a' => null);

print $b['a'] ?? 'd'; // d
print "n";

print $b['a'] ?: 'd'; // d
print "n";

print $b['c'] ?? 'e'; // e
print "n";

print $b['c'] ?: 'e'; // Notice: Undefined index: c in /in/apAIb on line 33
print "n";

The lines that have the notice are the ones where I'm using the shorthand ternary operator as opposed to the null coalescing operator. However, even with the notice, PHP will give the same response back.

Execute the code:

Of course, this is always assuming the first argument is null. Once it's no longer null, then you end up with differences in that the ?? operator would always return the first argument while the ?: shorthand would only if the first argument was truthy, and that relies on how PHP would type-cast things to a boolean.


$a = false ?? 'f'; // false
$b = false ?: 'g'; // 'g'

would then have $a be equal to false and $b equal to 'g'.

Wednesday, October 5, 2022

You need to add some parenthesis.

$b = 'a';
$c = 'd';
echo ($b == 'a') ? 2 : ($c == 'a' ? 1 : 0);
Monday, December 19, 2022

You have:

'linke_url' => (empty($child->permalink)) ? $child->link_url : $child->permalink,

I'll assume you meant 'link_url' and not 'linke_url', so it looks like you're trying to do:

If $child->permalink is empty, set 'link_url' to $child->link_url, but, if $child->permalink isn't empty, set 'link_url' to $child->permalink.

I would recommend using the shorthand version of the ternary operator ?: which is the form: $a = $b ?: $c which is the same as if $b evaluates to true, i.e. if $b has any value, $a = $b, otherwise $a = $c. For you:

'link_url' => $child->permalink ?: $child->link_url

If $child->permalink has a value, that will be used, otherwise, the value of $child->link_url will be used. Good luck!

Sunday, September 11, 2022

This might be a bit confusing for a "newbie", but you could attach a ternary method to the Boolean class like so.

implicit class Ternary[T](condition: Boolean) {
  def ??(a: => T, b: => T): T = if (condition) a else b


(4 == 4)??("yes","no")         // res0: String = yes
("abc".length < 2).??(1,0)     // res1: Int = 0
List('c').isEmpty.??('X','+')  // res2: Char = +
Sunday, September 4, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages