0
($some_var) ? true_func() : false_func();

What is this in php, and what does this do? existence, boolean, or what?

jahroy
  • 22,322
  • 9
  • 59
  • 108
jimbo1qaz
  • 29
  • 1
  • 1
  • 3
  • It's the ternary operator, and this code executes one of those two function calls depending on the boolean value of $somevar. http://php.net/manual/en/language.operators.comparison.php – Marc B Jul 11 '12 at 03:08
  • [bamf](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) make sure you know your operators. – zzzzBov Jul 11 '12 at 03:13

6 Answers6

4

It's the same thing as this:

if ($some_var) {
    true_func();
}
else {
    false_func();
}

If $some_val is true, it executes the function before the :.

If $some_val is false, it executes the function after the :.

It's called the ternary operator.

Typically it's used as an expression when assigning a value to a variable:

$some_var = ($some_bool) ? $true_value : $false_value;

It's one of the most abused programming constructs (in my opnion).

jahroy
  • 22,322
  • 9
  • 59
  • 108
  • Well, "almost the same". The `?:` operator is an expression (that evaluates to a value), however. While not shown in the example, since using it as an *expression* is the *useful non-"abused" way* to use `?:` it deserves to be mentioned. –  Jul 11 '12 at 03:20
  • @pst - I don't follow what you're trying to say. Please explain. I wasn't saying that there is no place for the `?:` operator. I was just stating that it tends to get abused often. I feel like you're trying to distinguish between using it for assignment vs using it to execute different functions, but I don't quite understand your comment. – jahroy Jul 11 '12 at 04:07
  • I believe the way it's used in the example it simply executes one of two functions. There is no assignment involved. It does evaluate to an expression, but in the given context that expression is not used. Therefore, I believe my code does _exactly the same thing_. – jahroy Jul 11 '12 at 04:20
  • I am saying the form, as shown, would *never* appear in my code. However, the form that uses the *expression* (e.g. does something with the *result* of `?:`) can be quite common if it makes sense. Because the form shown is *not* using the expression where it excels, as it is merely "clever golf code", the "correct" way to use the ternary operator should also be mentioned (as done with the later edits, +1) .. that's all :) –  Jul 11 '12 at 06:36
  • Got it. Thanks. I agree that it can be useful, and I do in fact use it (when appropriate). I also think it is often abused. – jahroy Jul 11 '12 at 06:47
1

Taken from the PHP Manual: Comparison Operators

<?php
// Example usage for: Ternary Operator
$action = (empty($_POST['action'])) ? 'default' : $_POST['action'];

// The above is identical to this if/else statement
if (empty($_POST['action'])) {
    $action = 'default';
} else {
    $action = $_POST['action'];
}

?>
Fluffeh
  • 33,228
  • 16
  • 67
  • 80
1

It's the ternary operator.

Instead of writing

if ($a < $b) {
  $minVal = $a;
} else {
  $minVal = $b;
}

You can write is as

$minVal = ($a < $b) ? $a : $b;
Yada
  • 30,349
  • 24
  • 103
  • 144
1

It's actually a ternary operator. (I mean the operator ?: is a ternary operator).

($some_var) ? func1() : func2();

'$some_var' is a boolean expression. If it evaluates to true 'func1()' is executed else 'func2()' is executed.

Anirudh Rayabharam
  • 737
  • 1
  • 6
  • 12
0

Well, the way it's written, it does the same as just

func();

(If $somevar is true, invoke func; otherwise, invoke func too!)

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
0

it checks for boolean:

When converting to boolean, the following values are considered FALSE:

the boolean FALSE itself
the integer 0 (zero)
the float 0.0 (zero)
the empty string, and the string "0"
an array with zero elements
an object with zero member variables (PHP 4 only)
the special type NULL (including unset variables)
SimpleXML objects created from empty tags

Every other value is considered TRUE (including any resource).

Also have a look at: PHP type comparison tables

stewe
  • 41,820
  • 13
  • 79
  • 75