DEV Community

Why PHP...???

Kevin on July 30, 2019

Hello, today i was working with some PHP and AJAX, and i had a problem, finally i fixed it, but i was asking myself, why PHP works like this. This ...
Collapse
 
mikcat profile image
mikcat

What about using the filter_input function from php?

$orange = filter_input(INPUT_POST, 'orange', FILTER_VALIDATE_BOOLEAN);

Returns TRUE for "1", "true", "on" and "yes". Returns FALSE otherwise.

$orange = filter_input(INPUT_POST, 'orange', FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);

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.

Collapse
 
anwar_nairi profile image
Anwar • Edited

I am afraid that FormData will always convert your values to string, if it is not a Blob or a File type.

Check this StackOverflow answer, you solved your issue the right way, but I guess this has nothing to do with PHP (you would have the same issue with a NodeJS backend for instance).

Tips for your client side payload

You can take advantage of JSON.stringify to encode your data:

const response = await fetch("/fruits.php", {
  headers: {
    Accept: "application/json",
    "Content-Type": "application/json"
  },
  method: "POST",
  body: JSON.Stringify({
    "orange": true,
    "purple": false
  })
});

Decode your payload seemlesly server side

I found this GitHub issue, with an interesting solution. I adapted it to make this seemless in your actual code:

$payload = file_get_contents('php://input');
$_POST = json_decode($payload);

var_dump($_POST["orange"]); // bool(true)
var_dump($_POST["purple"]); // bool(false)

Hope it helps!

Collapse
 
sebbdk profile image
Sebastian Vargr • Edited

PHP is how you get ants. :)

On a side note, all fields send in a post request are strings.

Use integers instead, then you can parse the string to a number, and check for 0 or 1.

Or use a JSON payload instead like Khalyomede suggests. ;)

JSON has boolean as a type

Collapse
 
anwar_nairi profile image
Anwar

lmao your first sentence :')

Collapse
 
kevinhch profile image
Kevin

I will started to use it, thnx dudes :)

Collapse
 
anfly0 profile image
Viktor Hellström

Well since the dawn of time the following has held true:

false == 0
true != false

And of course "false" != 0 and must therefore be true.

This is also the reason for the following weirdness:

echo true + 1;
2

echo false * 1;
0

echo true + true;
2

echo (bool) 99 + 1;
2
Collapse
 
jnunez166 profile image
jnunez166

All HTTP 1.0/1.1 payload is string and integer values. Even binary files are BinHex'ed before transmitting.

If another language has booleans it's because it is converting the string for you. Will it do it for an online questionnaire? Answer a true/false question on a form and then save it to the database? Are you saving 0/1? Can't assume all answers will be boolean or ints? How about reports on the questionnaire? "98% answered 1". that will require the dev to flag the question as a boolean output then convert it back to a string.

Since you need to sanitize your data you should use the PHP filter_input library that was recommended by /mikcat.

Note: I am not lecturing or talking down to you but trying to provide you with the possible view of the PHP developers.

Collapse
 
defman profile image
Sergey Kislyakov

Not sure about PHP, but in most cases casting strings to booleans works like this:

bool("true") // true
bool("false") // true
bool(" ") // true
bool("") // false