Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'true' in Get variables

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?

like image 660
Nilbert Avatar asked Aug 02 '10 04:08

Nilbert


People also ask

Is it true 1 or 0?

Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true. To make life easier, C Programmers typically define the terms "true" and "false" to have values 1 and 0 respectively.

How do you know if a variable is true?

If you want to check that a variable is explicitly True or False (and is not truthy/falsy), use is ( if variable is True ). If you want to check if a variable is equal to 0 or if a list is empty, use if variable == 0 or if variable == [] .

Is 1 for true or false?

Instead, comparison operators generate 0 or 1; 0 represents false and 1 represents true.

Can true be a variable in Python?

In general, a Boolean variable can have only two values - True or False. Or in other words, if a variable can have only these two values, we say that it's a Boolean variable. It's often used to represent the Truth value of any given expression. Numerically, True is equal to 1 and False is equal to 0.


1 Answers

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));
like image 123
Artefacto Avatar answered Oct 19 '22 09:10

Artefacto