Passing a boolean through HTTP GET request

Posted on March 2, 2020 at 12:43 pm

All GET parameters are strings in PHP.

You can use filter_var() with FILTER_VALIDATE_BOOLEAN:

$myVar = filter_var($_GET['myVar'], FILTER_VALIDATE_BOOLEAN);

Here is description for 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.

To convert the boolean variable to a string use var_export():

$myVar = var_export(filter_var($_GET['myVar'], FILTER_VALIDATE_BOOLEAN), true);

Receive updates via email

Other Posts

Updated Posts