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
Use filter_var() function to convert string to boolean value. Approach using PHP filter_var() Function: The filter_var() function is used to filter a variable with specified filter. This function is used to both validate and sanitize the data.
Boolean Variables and Data Type ( or lack thereof in C ) Zero is used to represent false, and One is used to represent true. For interpretation, Zero is interpreted as false and anything non-zero is interpreted as true.
The is_bool() function checks whether a variable is a boolean or not. This function returns true (1) if the variable is a boolean, otherwise it returns false/nothing.
To convert String to Boolean, use the parseBoolean() method in Java. The parseBoolean() parses the string argument as a boolean. The boolean returned represents the value true if the string argument is not null and is equal, ignoring case, to the string "true".
<?php function boolNumber($bValue = false) { // returns integer return ($bValue ? 1 : 0); function boolString($bValue = false) { // returns string return ($bValue ? 'true' : 'false'); $a = true; // boolean value
<?php $a = !array(); // This will === true; $a = !array('a'); // This will === false; $s = ! ""; // This will === true; $s = !"hello"; // This will === false; To cast as if using a (bool) you can NOT the NOT with "!!" (double '!'), then you are casting to the correct (bool). Example: <?php
Any data type can be explicitly converted to boolean with the help of casting operator (bool) or (boolean), although, most of the times, conversion is done automatically whenever required. This description is applicable to all versions of PHP.
Another way to get the type boolean, pass something that evaluates to true or false like 0 or 1: If you want to pass string true or false then another way: But I would say that filter_var with FILTER_VALIDATE_BOOLEAN was meant for this. Show activity on this post. Show activity on this post. It would be passed as a string.
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 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With