-2

For a template engine I would like to use shorthand if condition. I need to check if a value is != null print out some line if true. What I tried:

echo "test" ($user->affiliate_id !=null) ?   

I have no idea what to write behind the ?.

peke_peke
  • 391
  • 5
  • 18
  • 2
    that's the [ternary operator](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary), just read the doc – Federkun Jan 27 '16 at 12:42
  • `$variable = $condition ? $ifTrue : $ifFalse` is equivalent to `if($condition) { $variable = $ifTrue; } else { $variable = $isFalse; }` – Tomasz Kowalczyk Jan 27 '16 at 12:43
  • Possible duplicate of [What are the PHP operators "?" and ":" called and what do they do?](http://stackoverflow.com/questions/1080247/what-are-the-php-operators-and-called-and-what-do-they-do) – Gerald Schneider Jan 27 '16 at 12:48

5 Answers5

2

The line $someVariable = $condition ? $valueA : $valueB is equivalent to:

if ( $condition ) {
    $someVariable = $valueA;
}
else {
    $someVariable = $valueB;
}

So, basically, if the condition is TRUE, $someVariable will take the first value after the ? symbol. If FALSE, it will take the second value (the one after the : symbol).

There's a special case where you can not define the first value and it is like this: $someVariable = $someValue ?: $someOtherValue. It's equivalent to:

if ( $someValue ) {
    $someVariable = $someValue;
}
else {
    $someVariable = $someOtherValue;
}

So, if $someValue evaluates to TRUE (any value different than 0 is evaluated to TRUE), then $someVariable will catch that value. Otherwise, it will catch $someOtherValue.

To give you an example:

function printGender( $gender ) {
    echo "The user's gender is: " . ( $gender == 0 ? "Male" : "Female" );
}

printGender(0); // Will print "The user's gender is: Male"
printGender(1); // Will print "The user's gender is: Female"

Another example:

function printValuesStrictlyDifferentThanZero( $value ) {
    echo "Value: " . ( $value ?: 1 );
}

printValuesStrictlyDifferentThanZero(0); // $value evaluates to FALSE, so it echoes 1
printValuesStrictlyDifferentThanZero(1); // $value evaluates to TRUE, so it echoes that $value

EDIT:

The operator ?: is NOT called the ternary operator. There are multiple ways to define a ternary operator (an operator that takes three operands). It is a ternary operator, but not the ternary operator. Some people just call it the ternary operator because they're used to do it and, probably, it's the only ternary operator vastly known in PHP, but a ternary operator is way more general than that.

It's name is the conditional operator or, more strictly, the ternary conditional operator.

Let's suppose I define a new operator called log base that evaluates, inline, if the logarithm of a number $A with base $C equals to $B, with its syntax like $correct = $A log $B base $C and it returns TRUE if the logarithm is right, or FALSE if it's not.

Of course that operation is hypothetical, but it is a ternary operator, just like ?:. I'm gonna call it the logarithm checker operator.

Alejandro Iván
  • 3,539
  • 1
  • 17
  • 30
1

Syntax should be like this

echo (condition) ? write if true code part here : write else part here

that means in your case it will be like

echo ($user->affiliate_id !=null) ? 'test' : 'not null'
Jaykumar Patil
  • 329
  • 3
  • 12
1

As mentioned by @Federinco it's called the ternary operator, the official docs are here: http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary

The ternary operator works like so:

condition ? true branch : false branch

So an example would be:

$cost = isset($discount) ? $total - $discount : $total;

In the above example we check if discount has been set, and if it has we removed the discount from our current total, if not we just set the cost to the total.

There are other cool things you can do with the ternary operator like so:

$name = $username ?: 'Guest';

In the above code we are checking whether $username exists, if it is we set $name to $username otherwise we set $name equal to Guest

A word of caution though, the ternary operator can lead to readability issues if you abuse it, so be careful where you use it and don't sacrifices the extra lines of a standard conditional statement if the ternary operator is not going to be clear in what it is doing.

Mikey
  • 2,231
  • 1
  • 10
  • 19
  • It's called the **conditional operator** actually. And yes, it is a ternary operator (since it takes three operands). When used as a binary operator (two operands), it's also known as the **Elvis operator** (`?:` look at it like a text emoticon). – Alejandro Iván Jan 28 '16 at 15:28
0

Try this:

echo ($user->affiliate_id != null )?"test":"";
Devendra Soni
  • 331
  • 2
  • 10
0

What you want is the ternary operator

Your code should look like this

echo "test" . (($user->affiliate_id !=null) ? 'stringToOutput ifNotNull' : 'stringToOutput if is null');

Also, PHP 7 has introduced the Null coalescing operator which you could use. You can do this like this

echo 'test' . ($user->affiliate_id ?? 'ID not found!');

In which case, if $user->affiliate_id is set and not null, it will be printed instead of the 'ID not found!' message

thgs
  • 144
  • 5