Solved

PHP basic question

Posted on 2014-01-09
5
344 Views
Last Modified: 2014-01-09
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?
0
Comment
Question by:elepil
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 200 total points
ID: 39770060
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

0
 

Author Comment

by:elepil
ID: 39770084
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.
0
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 300 total points
ID: 39770124
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).
0
 
LVL 58

Expert Comment

by:Gary
ID: 39770131
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
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39770175
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

0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question