'true' in Get variables 'true' in Get variables php php

'true' in Get variables


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));


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'];    }}