CS 601 – Murach Product Manager “correction”

While working through the Product Manager application in Murach’s Chapter 4, I was getting errors on the following statement:

1: if(!isset($category_id)) {
2:   $category_id = $_GET['category_id'];
3:   if (!isset($category_id)) {
4:     $category_id = 1;
5:   }
6: }

The error was an invalid index for the expression on line 2.

Notice:  Undefined index: category_id in C:\xampp\htdocs\
cs601\week4\HW\index.php on line [2]

I had originally rewritten the code, but also tried with a direct copy and paste. I fixed the error using the isempty function:

1: if (!isset($category_id)) {
2:   if (!empty($_GET)) {
3:     $category_id = $_GET['category_id'];
4:   } else {
5:     $category_id = 1;
6:   }

(Note, I’m running in PHP 5.3.5, the book requires 5.3.)