I have tried to send boolean data type using postman form-data, but in php it read as string. I have tried to convert it using manual casting (bool) and boolVal(), but the result always true whenever I send false or true value in the postman.
Is there any solution with this problem? I can't use it in if statement cause it always return true whenever I tried to cast it to bool.
if ($request->isDiscount) {
$price = $request->price * 10/100;
}
the above code will always get executed whenever the value of isDiscounted is true or false
have tried below too
if ($request->isDiscount == true) {
$price = $request->price * 10/100;
}
or
if ((bool)$request->isDiscount == true) {
$price = $request->price * 10/100;
}
or
if (boolVal($request->isDiscount) == true) {
$price = $request->price * 10/100;
}
but the code always get executed
CodePudding user response:
As there are no data-types within the form-data this is pretty normal.
The text true
is a non-empty string and therefore interpreted as boolean true
within PHP. The same for the text false
which is also a non-empty string and therefore interpreted as boolean true
.
It is up to your code to interpret submitted data as integer/text/boolean/whatever. In this case I would submit the text 1
for true
and 0
for false
and on the server's side convert it to integer which you can use for your if
as integer 0
is interpreted as boolean false
and all other integers as boolean true
.
Keep in mind to sanitize all the data submitted to avoid SQL-injections and so on.
CodePudding user response:
Not familiar with postman, but if it's sending via GET maybe try
if ($_GET['isDiscount']) {
$price = $_GET['isDiscount'] * 10/100;
}
OR
$price = $_GET['isDiscount'] ? $_GET['isDiscount']*10/100 : false;
Also I sanitize _GET with this function from my library
function num ($input){
if(strlen($input) > 24){ $input=0; }
if(!preg_match('/^[0-9] $/', $input)){ $input=0; }
return $input;
}
Using function is fun & easy...
$discount = num($_GET['isDiscount']) ?: false;
For those wondering why I check a numbers length with strlen... years ago a player in my game discovered if he entered in a big enough number, he would give him a butt load of free war units.. you could remove that part or change length.
Let me know if this helps you, i'll check back in 10 mins.
CodePudding user response:
PHP reads all form data as strings.
There's 2 approaches you can take (plus more, but I'll explain 2 common ones):
- Pass data as JSON
- Use PHP's filter_var function
Pass data as JSON
https://www.php.net/manual/en/function.json-decode
https://www.php.net/manual/en/wrappers.php.php#wrappers.php.input
Sending the data as a JSON string and then decode it in PHP. An easy way I've found to do this is like so:
All data will be decoding into the relevant types.
Use PHP's filter_var function
https://www.php.net/manual/en/function.filter-var.php
This allows you to use built-in PHP functions to convert from a string, the the selected type (using the $filter argument).
An example using filter_var to return a bool value:
The drawbacks of using filter_var is that you are limited to interpreting a string which means you need to ensure that data being posted is in the correct format