问题
Pretty simple question here, not sure the answer though. Can I pass a boolean variable through get? For example:
http://example.com/foo.php?myVar=true
then I have
$hopefullyBool = $_GET['myVar'];
Is $hopefullyBool
a boolean or a string? My hypothesis is that it's a string but can someone let me know? Thanks
回答1:
All GET parameters will be strings in PHP. Use filter_var
and 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.
$hopefullyBool = filter_var($_GET['myVar'], FILTER_VALIDATE_BOOLEAN);
Another way to get the type boolean, pass as something that evaluates to true
or false
like 0
or 1
:
http://example.com/foo.php?myVar=0
http://example.com/foo.php?myVar=1
Then cast to boolean:
$hopefullyBool = (bool)$_GET['myVar'];
If you want to pass string true
or false
then another way:
$hopefullyBool = $_GET['myVar'] == 'true' ? true : false;
But I would say that filter_var
with FILTER_VALIDATE_BOOLEAN
was meant for this.
回答2:
If you want to avoid an if statement:
filter_var('true', FILTER_VALIDATE_BOOLEAN);
//bool(true)
filter_var('false', FILTER_VALIDATE_BOOLEAN);
//bool(false)
回答3:
It would be passed as a string. While you can convert it using the bool cast, it is recommended to not do so in some cases.
You would be better off doing if myVar == "True"
Be cautious:
>>> bool("foo")
True
>>> bool("")
False
Empty strings evaluate to False, but everything else evaluates to True. So this should not be used for any kind of parsing purposes.
回答4:
There's a few ways to do it. Firstly we could use PHP's in-built (boolean)
method, which casts a string value to a boolean:
$hopefullyBool = (boolean)$_GET['myVar'];
This would result in a boolean value of true
or false
depending on the value of the string in $_GET['myVar']
.
From v5.2.1 upwards, we can also use json_decode()
to ascertain the boolean value:
$hopefullyBool = json_decode($_GET['myVar]);
The json_decode()
method parses a JSON Object (which is a string) into a PHP variable, and takes type-casting into account. As a result, the string 'true'
from the URL param will be cast to the boolean true
.
You could use both of the above methods in tandem, using:
$hopefullyBool = (boolean)json_decode($_GET['myVar]);
To mitigate against uppercase characters being passed in the URL param (eg ?myVar=True
or ?myVar=FALSE
), you should use the strtolower()
method, which will convert a string to all lowercase letters:
$hopefullyBool = (boolean)json_decode(strtolower($_GET['myVar]));
Finally, we'll want to fall-back to false
if the parameter is not present in the URL's query string, otherwise PHP will throw an Undefined Index notice. To do this, we can use the isset()
method:
$hopefullyBool = false;
if ( isset($_GET['myVar']) ) {
$hopefullyBool = (boolean)json_decode(strtolower($_GET['myVar]));
}
To shorten this, you could initiate $hopefullyBool
using a conditional statement like so:
$hopefullyBool = isset($_GET['myVar']) && (boolean)json_decode(strtolower($_GET['myVar']));
来源:https://stackoverflow.com/questions/24063590/passing-a-boolean-through-php-get