One to Remember - GET Equals 0 is Empty

PHP
3rd June 2019 One to Remember - GET Equals 0 is Empty

The joys of programing is always learning. Yesterday I spent a fair amount of time working on a table filter using _GET, depending on what variables you wanted to filter by. All good until it came to testing.

The problem was that one of the filters was for whether something was open or closed - stored in the database as 0 or 1 respectively. Everything worked as expected when filtering for closed items, however filtering on open items still returned everything. After a bit of searching, I found the answer.

The problem was in using 0 along with how I was querying my database. My initial query pulls back everything it needs to with no filters added. I then check for four potential GET values from the URL. If the GET is empty, the addition to the SQL query is ignored. Otherwise it appends some extra code. It turns out that a zero in a GET is counted as the GET being empty. Marvellous. As I'd never used a GET to look for a zero before, this caused me some real puzzlement.

The solution therefore was to keep the 1 as closed, but change the 0 to a 2. I then change that to be a zero once the empty check has been performed. The GET is back as I want it ready to query the database.

 

Don't expect I'll use this often, but it's a good one to remember!