PHP basic question

I am going through a code sample in my PHP book, and I'm getting an "undefined index at line 5" error when I run this PHP page. The PHP script begins with this:

<?php
    $category_id = $_GET['category_id'];
    if (!isset($category_id)) {
        $category_id = 1;
    }
.
.
?>

Line 5 is the very first line where $category_id is trying to obtain its value from the $_GET array.

The book intends this PHP page to be run as a standalone (as opposed to a click on a hyperlink from a prior page). The page itself will have three links that will set the value of $category_id and then call this same page, but since this is the first time I'm running it, and the $_GET array is empty, causing the error message. The page still runs okay though despite the error message.

My question is this. Is the author of the book just sloppy? The book does not forewarn me to expect this error at all. Or are the warning settings of my PHP set too high?
elepilAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kaufmed   ( ⚆ _ ⚆ )Patches? We Ain't Got No Patches! We Don't Need No Patches! I Don't Have to Push You No Stinkin' Patches!Commented:
Your GET parameters come from the querystring of your URL. A querystring is anything that comes after the (first) question mark in a URL.

e.g.

https://secure.experts-exchange.com/askQuestion.jsp?taid=103

Open in new window


You can see a querystring of "taid=103" in the above example. Your PHP is expecting to see a parameter of "category_id" in the request:

e.g.

https://secure.experts-exchange.com/askQuestion.jsp?category_id=103

Open in new window


So, when you run your code on the server, the request that you submit via the browser needs to include this information...currently. You generally want to test for the existence of such values before you try to access them. You can accomplish this via the isset function:

<?php

    if (isset($_REQUEST['category_id'])) {
        $category_id = $_GET['category_id'];
        if (!isset($category_id)) {
            $category_id = 1;
        }
    .
    .
    
    }
?>

Open in new window

elepilAuthor Commented:
Kaufmed,

Thank you for your response. I did understand what was happening.

My question was whether my PHP settings warning levels might be set too high (if there is such a thing with PHP because there is such a thing in other platforms). I'm new to PHP, and I'm not sure if the author of my book is just sloppy without forewarning me this will happen, or if it's a PHP setting in my Apache server, which could explain why the author never said a word.

I normally wouldn't be asking this type of a question, but PHP is so permissive that I'm even surprised it would warn me of something like this.
gr8gonzoConsultantCommented:
PHP does have various error reporting levels. On production systems, it's best not to display errors, but to log them to a file. I usually think it is best to log anything that is a warning or more severe, and this type of warning message is something that I would not ignore. It is most likely the result of an author writing up samples and making tweaks without testing everything.

In a real-world scenario, if I saw this error on the screen or in a log, I would correct it the way kaufmed suggested (check for the variable in the GET array before using it).

To be clear, PHP -is- permissive, and it shows in that it didn't prevent the code from running (it simply assumed that the value was blank). It simply threw a warning. Permissive is not always the same thing as lazy. PHP has plenty of warnings (not all of them very user-friendly).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryCommented:
kaufmed is right in what he is telling you and I think you are right in that the author is being sloppy about his approach and seems to be assuming that you have error messages turned off.
Yes you can turn off all warnings like this which are not critical (critical errors would break the code) and your code will continue to execute but it is getting you into a bad routine whereby you are not doing any error checking or making sure an error would not occur
Dave BaldwinFixer of ProblemsCommented:
I think that's bad code.  This is how I do it at the top of all my PHP pages that are receiving data from a page.  I do this to make sure a default value is set.  This can be important because checkboxes and radio buttons do not send a value if they are not checked.  By setting the value to '', it allows you to tell that there was no value sent without causing an error.  It doesn't have to be POST.  It can be GET or REQUEST depending on your needs.
if (!isset($_POST['toText'])) $toText = ''; else $toText = $_POST['toText'];

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.