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
All GET parameters will be strings in PHP. To get the type boolean, pass as something that evaluates to
true
orfalse
like0
or1
, then use:If you want to pass string
true
orfalse
then:Or even better:
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:
Empty strings evaluate to False, but everything else evaluates to True. So this should not be used for any kind of parsing purposes.
If you want to avoid an if statement: