Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a boolean through PHP GET

Tags:

php

boolean

get

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

like image 464
Sam Creamer Avatar asked Jun 05 '14 14:06

Sam Creamer


People also ask

How parse boolean in PHP?

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.

Is bool true 1 or 0?

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.

How can check boolean value in if condition in PHP?

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.

How do you pass a boolean value to a string?

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".

How do you return a boolean value in PHP?

<?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

How to cast as if using a Boolean in PHP?

<?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

How to convert a data type to a Boolean in 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.

How to get the type of a Boolean from a string?

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.


1 Answers

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.

like image 157
AbraCadaver Avatar answered Oct 06 '22 00:10

AbraCadaver