The differences between true, false, and null (or how i accidentally made every single cat hate children)
Tags: api, boolean
Published: 2019-10-28 18:21:45

---
title: The differences between true, false, and null (or how i accidentally made every single cat hate children)
published: true
description: I did a newbie mistake of using only true/false. I built a app for a pet adoption agency that did a API call and displayed the JSON data into an attractive front-end. But I messed up on the logic.
tags: api, boolean
cover_image: https://i.imgur.com/GEpxgD7.jpg
---


## The Problem

I was contracted to update the [PetFinder V2 API](https://www.petfinder.com/developers/v2/docs/) for a Pet Adoption Agency. It's a simple project - a handful of headaches here and there. But nothing that I couldn't figure out. 

> If you're curious on how to pull API data via PetFinder V2, let me know in the comments. Happy to share.

The payload gives you things like: 

* Animal Name
* Animal Type
* Animal Species

It also gives you things like:
* If the animal is housetrained
* If the animal has been declawed. 
* If the animal can co-exist with cats/dogs/children (<-- my OOOF!)

![My display](https://i.imgur.com/oFe1ngz.png)

## My original newbie solution

The payload gives you the following: 

```
"attributes": {
                "spayed_neutered": false,
                "house_trained": true,
                "declawed": false,
                "special_needs": null,
                "shots_current": false
            },
```

Ah, neat!

The piece that I want to point out is this.

```                
    "declawed": false,
```
To read this -- 
If true, the animal "has been declawed". 
If false, the animal "has NOT been declawed". 

Easy - right?

So that means, this code works either way.

```
if ($declawed) {
   $string = "Has been declawed (image of a mitten). ";
} else {
   $string = "Has not been declawed (image of a claws)."
}

```
If the payload says *true*, then kitty doesn't have any sharp bits. (btw, [please don't declaw](https://www.popsci.com/declaw-your-cat/).)

If the payload says *false*, or *NULL*, then kitty has what nature gave it. 


Simple stuff. Coding 101.


## AH CRAP - I'm an idiot

I rolled it out, did my little celebration dance to [FF1 Victory Theme](https://www.youtube.com/watch?v=lz8S0HQzrpU) while wearing my 'I'm a real developer' hat, and called it a day.

Then I get a email a few days later with the following: 

> It appears that the information we are entering into PetFinder to appear on our website is wrong. For example, Piper the dog shows she is NOT good with cats on our website.

AH CRAP. 

This wasn't a Petfinder issue. This was a Rocky issue.

## FALSE != NULL

So the problem came with my True/False Statement.

```
            "environment": {
                "children": null,
                "dogs": false,
                "cats": true
            },
```

Let's break this down.

*True* == Good with [x]. (So the pet is good with children)
*False* == NOT Good with [x]. (So the pet is NOT good with dogs)
*Null* == no data. (The user did not submit this info.)


Based on my code & logic, poor Stripey the cat would have been given a 'NOT GOOD WITH CHILDREN' label. Which is NOT TRUE!
```
"children": null
```

The admins who were submitting data to PetFinder would leave off a lot of data, creating a lot of NULLs. Which is very common, btw, so plan for that.

The payload says that value doesn't exist. 

But my stupid code assumed all Nulls meant false. Which is completely inaccurate. And I totally messed up.

## Updated Code
Really, I need to identify 3 situations.

If the variable is null.
THEN if the variable is true, or the variable is false.

Let's look at my new code.

> For those not familiar with PHP, [isset](https://www.php.net/manual/en/function.isset.php) checks to see if the variable has a value. 

> Note: True/false are values. Null is NOT!


```
    if (!isset($good_with_children)) {
	$string =  "this data doesn't exist.";
    } else {
	
        if ($good_with_children == false) {
	  $string = 'Is NOT good with Children. (sad face)';
	} elseif ($good_with_children == true) {
	  $string = 'Is good with Children. (happy face)';
	}
    }
```

**What does this mean?** 

I fixed the issue by adding another conditional in there to check to see if it's NULL or not.

Check if **$good_with_children is NULL.** If it's NULL, then spit the 'data doesn't exist' bit. (Which you'll remove in production!)

If it isn't NULL, go do the true/false test. 

## Conclusion
Huzzah! This conditional fixed the issue. Every single cat on that adoption site no longer hates children. (Only a few do.)

Well, you live and learn. 

And I hope my code helps these pets get into loving homes.