0

Possible Duplicate:
What is the PHP ? : operator called and what does it do?

I feed like a goof but I don't entirely understand what's happening in this code:

$var .= ($one || $two) ? function_one( $one, $another) : function_two( $two, $another);

Does that say if $one or $two then $var is equal to fuction_one(), else function_two()? What's the purpose of using this syntax -- speed?

Community
  • 1
  • 1
buley
  • 28,032
  • 17
  • 85
  • 106
  • 3
    Good ol' ternary: http://stackoverflow.com/questions/1080247/what-is-the-php-operator-called-and-what-does-it-do – Pekka Jun 10 '10 at 20:40
  • 1
    This is closed, but regarding what it's used for - speed and cleaner code as this example shows: http://en.wikipedia.org/wiki/Conditional_operator#Usage – Jan K. Jun 10 '10 at 20:50
  • Thanks everyone for the overwhelming response! – buley Jun 10 '10 at 20:56
  • For what it's worth, it's hard to figure out if your question is a duplicate if your question is "what is this?" – buley Apr 17 '12 at 21:49

4 Answers4

4

If either $one is true, or $two is true, then the result of calling function_one is appended to $var. Otherwise, the result of calling function_two is appended to $var.

It's basically shorthand for:

if ($one || $two) {
  $var .= function_one( $one, $another);
} else {
  $var .= function_two( $two, $another);
}
Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
3

$var would append to itself the value from the return of function_one() if $one or $two evaluates to true, and would append the result of function_two() otherwise.

Alexandru Luchian
  • 2,760
  • 3
  • 29
  • 41
1

function_one() and function_two() both return a value.

You are concatenating $var to the return value of one of these function based on an if statement that evaluates $one or $two, If $one or $tow are assigned or return true the returned from function_one() is concatenated otherwise the value returned from function_tow() is.

Babiker
  • 18,300
  • 28
  • 78
  • 125
1

$var .= ($one || $two) ? function_one( $one, $another) : function_two( $two, $another);

append $var with output of function_one() or function_two()

if $one is true then execute function_one() else execute function_two()

Foobar
  • 11
  • 1