Reputation: 5361
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
Upvotes: 26
Views: 28266
Reputation: 78994
All GET parameters will be strings (or an array of strings) in PHP. Use filter_var (or filter_input) 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);
For INPUT vars that can be arrays there is filter_var_array and filter_input_array.
Another way to get the type boolean, pass something that evaluates to true
or false
like string 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.
Upvotes: 49
Reputation: 1
if I'm sure that the only thing being passed is a boolean, I normally just check if it's equal to String "true" if it is, then it return true, if not then it return false
$is_true= $_GET['is_true'] == "true";
Upvotes: 0
Reputation: 1127
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']));
Upvotes: 2
Reputation: 323
If you want to avoid an if statement:
filter_var('true', FILTER_VALIDATE_BOOLEAN);
//bool(true)
filter_var('false', FILTER_VALIDATE_BOOLEAN);
//bool(false)
Upvotes: 5
Reputation: 2379
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.
Upvotes: 3