0

I looked in the boolean documentation on PHP http://php.net/manual/en/language.types.boolean.php couldn't find anything. I also tried searching for it, and I get returned no results. Search terms I've tried "operator ? php", "true : false" and still get returned "no results"

I saw the example here http://php.net/manual/en/function.session-status.php where the user coder uses it to return true/false statements. So what exactly does

return session_status() === PHP_SESSION_ACTIVE ? TRUE : FALSE; mean? I understand that === means exact type, but past that I have no idea what ? TRUE : FALSE is.

Timothy
  • 19
  • 4
  • 2
    That is called a [ternary](https://davidwalsh.name/php-shorthand-if-else-ternary-operators). It's basically a shorthand if statement. It evaluates whatever is before the question mark, and if it evaluates true the left side of the colon is used, if false the right side is used. It doesn't necessarily have to be `TRUE : FALSE` either, these can be concatenated into strings to simplify echoing messages. `condition ? TRUE : FALSE` – GrumpyCrouton Nov 14 '17 at 19:29
  • does it matter what order you put it in? could it be false : true and have the same effect? – Timothy Nov 14 '17 at 19:31
  • 1
    @Timothy: Learn what it actually means. – SLaks Nov 14 '17 at 19:32
  • @Timothy Sure it can, If you change the direction of the condition, E.G `session_status() !== PHP_SESSION_ACTIVE`; this will rotate the meaning of the return. – GrumpyCrouton Nov 14 '17 at 19:33
  • 2
    It's actually pointless in that expression (which already returns the right boolean). – mario Nov 14 '17 at 19:33
  • 1
    Yes, order is important. This is somewhat a very short version of a `if (...) { ... } else { ... }` statement... Would the behavior be the same if you'd invert the two blocks of a if statement ? – James Nov 14 '17 at 19:33
  • 1
    As @mario stated, you could just do `return session_status() === PHP_SESSION_ACTIVE;` and it will give you the same result. – GrumpyCrouton Nov 14 '17 at 19:34
  • Also, without parens, is it `( (session_status() === PHP_SESSION_ACTIVE) ? TRUE : FALSE);` or is it `(session_status() === (PHP_SESSION_ACTIVE ? TRUE : FALSE) );` ... kind of like what happens when you leave out parens in a math equation ;) – IncredibleHat Nov 14 '17 at 19:34
  • @Randall I'm pretty sure the 2nd example you had there is invalid. In your code, the "condition" would just be `PHP_SESSION_ACTIVE`. – GrumpyCrouton Nov 14 '17 at 19:35
  • @GrumpyCrouton yup... it would eval very oddly, and always not what you would expect! LOL. Your example of just using `return session_status() === PHP_SESSION_ACTIVE;` is much better overall in this case. – IncredibleHat Nov 14 '17 at 19:36
  • It is the [ternary comparison operator](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary). – axiac Nov 14 '17 at 19:37
  • @Timothy Ternaries are really useful in other cases, like if there is a condition where you want a specific message displayed, such as adding an "s" to something that is plural. `echo "there can be $x brick".($x != 1 ? "s" : "")."";` (As you can see, ternaries can be concatenated into strings). My example code there will add an "s" to the end of the word "brick" if there are 0 or more than 1 of `$x` (As in, if `$x` is not "1"). – GrumpyCrouton Nov 14 '17 at 19:38
  • ah ok thanks @GrumpyCrouton. So in the case he used it, it's pointless because he's already returning a status of a php session. Still good to know I can definitely use that later. – Timothy Nov 14 '17 at 19:41
  • @Timothy Exactly. Using a ternary to give you a TRUE or FALSE response is not usually something you need to do, because the condition will usually already be TRUE or FALSE. – GrumpyCrouton Nov 14 '17 at 19:41

0 Answers0