Passing a boolean through PHP GET

两盒软妹~` 提交于 2019-12-01 03:42:57

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.

Roman

If you want to avoid an if statement:

filter_var('true', FILTER_VALIDATE_BOOLEAN);  
//bool(true)

filter_var('false', FILTER_VALIDATE_BOOLEAN); 
//bool(false)

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.

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!