views:

55

answers:

2

In PHP, when you have something in the URL like "var=true" in the URL, does the 'true' and 'false' in the URL get translated to boolean variables, or do they equal the text 'true' or 'false'? For instance, would, with the url having "var=false" in it:

if ($_GET['var'] == false) { ... }

work? Or would the variable always be true since it has text in it?

A: 

They are passed as strings, so are always truthy unless they are one of these, which evaluate to false instead:

  • The empty string ''
  • A string containing the digit zero '0'

To make my life easier I just pass boolean GET variables as 1 or 0 and validate them to be either one of those values, or decide on a default value appropriately:

// Default value of false
$var = false;

if (isset($_GET['var']))
{
    if ($_GET['var'] === '1' || $_GET['var'] === '0')
    {
        $var = (bool) $_GET['var'];
    }
}
BoltClock
Wow PHP has type casting which means it is in another way like C++? Sweet!
Nilbert
@Nilbert: [yeah.](http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting)
BoltClock
@Nilbert Welcome to the very basics of PHP. :) Types are casted all the time implicitly in PHP.
deceze
" || $_GET['var'] === '0'" condition branch is superfluous here. if (isset($_GET['var']) }
zerkms
or even: $var = isset($_GET['var'])
zerkms
@zerkms That's not quite the same.
deceze
@deceze: please give me an input sample when mine and his code will act in a different manner.
zerkms
@zerkms: good point. I did include the `=== '0'` condition to illustrate my point though. Oh well, at least OP now knows PHP features typecasting :)
BoltClock
@BoltClock: Yeah, your sample is quite good in the fact that it is very academic and easy to understand. It is decomposed well and is illustrating how developer should think when solves such task.
zerkms
@zerkms for `?var=2` your code will validate an incorrect value of `var`.
pascal
Dear downvoter: care to explain what's wrong with my answer and Artefacto's answer?
BoltClock
@zerkms Never mind, glanced over the code too quickly.
deceze
@pascal: are you sure? check your evaluations again and compare mine and BoltClock's results, please.
zerkms
@pascal: actually, no, `'2'` won't meet the conditions. Fine if you want all non-zero number values to evaluate to true, but that's not what my answer is illustrating.
BoltClock
@BoltClock yes, I know. That's why I was addressing my comment to @zerkms whose *shorter* code considers any value of `var` as correct. This was somewhat to answer the question addressed to @deceze, about "act in a different manner".
pascal
Ooh, another downvote!
BoltClock
+4  A: 

No, $_GET will always contain only strings.

However, you can filter it to get a boolean.

FILTER_VALIDATE_BOOLEAN:
Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise. If FILTER_NULL_ON_FAILURE is set, FALSE is returned only for "0", "false", "off", "no", and "", and NULL is returned for all non-boolean values.

Example:

$value = filter_input(INPUT_GET, "varname", FILTER_VALIDATE_BOOLEAN,
    array("flags" => FILTER_NULL_ON_FAILURE));
Artefacto
+1 Didn't know there was a filter for booleans.
BoltClock
A link to http://php.net/filter would be appropriate.
deceze
A little bit monstruous but very handy, +1.
zerkms
@zerkms Well, but you'll have a correctly type variable, no need to check if `isset`, and the flag is of course optional.
Artefacto
or if it's an array or not...
Artefacto