2

A bit of a silly question for more advanced programmers, but In my quest to learn php I have come across return statements that involve a ? symbol with values of 0, -1 and 1 such as:

return ($a > $b) ? -1 : 1;

or

[$index ? 0 : 1];

Im trying to understand the logic of what this statement does and why it is used, any help will go a long way, thanks

hakre
  • 193,403
  • 52
  • 435
  • 836
Edmund Rojas
  • 6,376
  • 16
  • 61
  • 92
  • possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php); See as well: [Where can I read about conditionals done with ? and :](http://stackoverflow.com/questions/4055355/where-can-i-read-about-conditionals-done-with-and) – hakre Aug 06 '11 at 11:22

4 Answers4

3
  1. return ($a > $b) ? -1 : 1;

If $a is greater than $b return -1, else return 1.

It is the ternary operator (a.k.a shorthand if/else statement)

Shef
  • 44,808
  • 15
  • 79
  • 90
  • 1
    what does -1 symbolize exactly? does it mean for example true or false? – Edmund Rojas Aug 06 '11 at 11:15
  • 1
    They both evaluate to boolean `true`. However, the returned value will be the number, not a boolean. – Shef Aug 06 '11 at 11:17
  • @edmund in boolean true is denoted by 1 and false is denoted by 0, -1 does not serves no meaning for a boolean. if you want to create a boolean then it should be `return ($a > $b) ? 0 : 1;` or best could be `return ($a > $b) ? true : false;` or simply put use `return ($a > $b)` – Ibrahim Azhar Armar Aug 06 '11 at 11:20
  • interesting! what about 0? I have seen this here and there as well, what would that equate to? – Edmund Rojas Aug 06 '11 at 11:21
  • @Ibrahim Azhar Armar - or directly `return ($a > $b)` :) – Petar Minchev Aug 06 '11 at 11:21
  • @shef that is not correct, `-1` will evaluate to boolean true not false, check this `var_dump((bool) -1);` – Ibrahim Azhar Armar Aug 06 '11 at 11:23
  • @edmund: As Ibrahim has explained, `0` will evaluate to boolean `false`. – Shef Aug 06 '11 at 11:24
  • @edmund: Between the comparison you are making, only 0 evaluates to boolean `false`, not both. – Shef Aug 06 '11 at 11:26
  • @edmund please take a minute to read the answer properly, i have already told 0 is equal to false and 1 is equal to true, apart from integer 0 rest all evaluates to true in boolean. consider boolean like a switch with only two controls `ON` and `OFF`, `ON = 1` & `OFF = 0` as simple as that. – Ibrahim Azhar Armar Aug 06 '11 at 11:28
  • 1
    interesting, just throws me off when i saw some people use a 0, appreciate the help though guys! – Edmund Rojas Aug 06 '11 at 11:29
  • 2
    @edmund: The first statement seems to be part of a comparison function (a function you can pass e.g. to `usort`). Such a function has to return `0` if both arguments are equal, `-1` if the first argument is smaller than the second and `1` if it is bigger. – Felix Kling Aug 06 '11 at 11:31
  • ah yes! I think this would explain why! so the -1 is used in cases where there is a possibility for a true, false and an equal? – Edmund Rojas Aug 06 '11 at 11:42
2

? is the ternary operator. If the boolean expression ($a > $b) is true then -1 is returned else 1 is returned. It is just a short if else combination.

To summarise boolean expression ? x : y is equal to:

if (boolean expression)
   evaluates to x
else
   evaluates to y
Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
2

It's same like

if ($a > $b) 
      return -1; 
else

      return 1;

(It's shorthand)

Search for "ternary" on this page

genesis
  • 50,477
  • 20
  • 96
  • 125
2

Isn't it similar to C's if statement but in one line? So I thought this is the PHP equivalent:

if ($a > $b) {
return -1;
} else {
return 1;
}

Whilst the short hand version would be:

return (($a > $b) ? -1 : 1);

So what you're having is something like this:

(if true) ? then : else;

Check out this for more details.

Max Z.
  • 801
  • 1
  • 9
  • 25