Why PHP...???
Tags: php, why, booleans
Published: 2019-07-30 18:20:47

---
title: Why PHP...???
published: true
description: weird things of PHP
tags: php, why, booleans
---
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 is my JS file.<br>
```
<html>
<script>
        (() => {
            const formData = new FormData();
            formData.append('orange', false)
            formData.append('purple', true)
            formData.append('someData', 'lorem ipsum...you know right')
            formData.append('numbers', [1,1,2,3,5,8])

            const sendData = async (url, method, data) => {
                let response = await fetch(url, {method: method, body: data})
                let dataRes = await response.json()
                console.log(dataRes)
            };//senData
            const url = window.location

            sendData(`${url}back.php`, 'post', formData)


        })()    
</script>
<html>
```
I was validating a form and preparing the data to send to my backend and play with this data in my Data Base, ok no problem.
But when i tried to validate the data on the backend...was horrible, because I tried to validate Booleans, and PHP did weird things with Booleans.
```
<?php
// This is my "validation"
if(isset($_POST['orange']) && $_POST['orange'] == true){
    echo "Why im here, orange is false!!";
}
```
And this is the response from server. <br>
<img src="https://thepracticaldev.s3.amazonaws.com/i/7ihyi7kiqmf2ibxkz2vc.png"> <br>
OK, no problem, let me check what types of variables i have:

<img src="https://thepracticaldev.s3.amazonaws.com/i/71mdpan9yscktk2u8zbk.png" > <br>
I got you, i can't compare a string with a boolean, ok, let me cast. <br>
```
<?php
echo "<pre>";
var_dump($_POST['orange']);
echo "</pre>";

$orange = (bool) $_POST['orange'];
echo "casting to boolean...";
echo "<pre>";
var_dump($orange);
echo "</pre>";

// This is my "validation"
if($orange == true){
    echo "Why im here, orange is false!!";
    echo "<pre>";
    var_dump($orange);
    echo "</pre>";
}//if
```
But still true, why? I cas't the variable, now should be false, but still true. <br>
```
<?php

$orange = (bool) $_POST['orange'];
echo "casting to boolean...";
echo "<pre>";
var_dump($orange);
echo "</pre>";

// This is my "validation"
if($orange == true){
    echo "Why im here, orange is false!!";
    echo "<pre>";
    var_dump($orange);
    echo "</pre>";
}//if
echo "<pre>";
print_r($_POST);
echo "</pre>";
```
<img src="https://thepracticaldev.s3.amazonaws.com/i/k8zutcnqp2n7lr7ayuog.png"><br>
And that's the problem, if i want to cast to a boolean I need to use json_decode (WHAT?), I have an string, i cast to a boolean, but PHP don't cast the variable, PHP check if he can cast, if he can, they will return true, not the content of the variable.
```
<?php
$orange = (bool) json_decode($_POST['orange']);
//finally
if($orange == true){
    echo "Why im here, orange is false!!";
}else{
    echo "win win for me.<br>";
    var_dump($orange);
}
```
Finally! <br>
<img src="https://thepracticaldev.s3.amazonaws.com/i/ppcfyy3dxiegpv6ogj7c.png"> <br>
The question is: Why PHP works so weird with booleans?.
I'm pretty sure others progamming langs can be solve this types of problems pretty good.

Other way to fix this problem was:<br>
```
<?php
if($orange == "true"){
    echo "do something";
}
```
But this is not my style.
Tell me situations like these that have happened, to avoid them.