Undefined index - PHP Notice 
Author Message
 Undefined index - PHP Notice

Hi, I have the following trouble (or annoyance)

I get these kind of errors in my php error log:

[12-Feb-2003 15:47:52] PHP Notice:  Undefined index:  test in tst.php on
line 9
[12-Feb-2003 15:48:22] PHP Notice:  Undefined index:  test in tst.php on
line 9

I know they are not harmful, but I would like to learn how to avoid them.

It's normally logged when I use these kind of statements in my code:

if($_GET['test'])
{
    // do something here

Quote:
}

It's ofcourse only when I don't send the get variable that the notice
appears. How do I avoid getting the notice messages?

I've tried stuff like this, and it works if I don't send any get
variables but if I send one or more but doesn't name them to "test" it
will still produce the notice error message.

if($_GET)
{
   if($_GET['test']){
     echo $_GET['test'];
   }

Quote:
}

I'm pretty sure there's some nice simple way around this so please tell
me how ;)

I know that I could change the error reporting level but that's not what
  I'm looking for, I want to know how to write clean, error free code.

Thanks for any help !

/Andreas



Sun, 31 Jul 2005 23:13:29 GMT  
 Undefined index - PHP Notice

Quote:

> Hi, I have the following trouble (or annoyance)

> I get these kind of errors in my php error log:

> [12-Feb-2003 15:47:52] PHP Notice:  Undefined index:  test in tst.php on
> line 9
> [12-Feb-2003 15:48:22] PHP Notice:  Undefined index:  test in tst.php on
> line 9

> I know they are not harmful, but I would like to learn how to avoid them.

> It's normally logged when I use these kind of statements in my code:

> if($_GET['test'])
> {
>    // do something here
> }

> It's ofcourse only when I don't send the get variable that the notice
> appears. How do I avoid getting the notice messages?

> I've tried stuff like this, and it works if I don't send any get
> variables but if I send one or more but doesn't name them to "test" it
> will still produce the notice error message.

The right way to avoid them is to use isset()

if (isset($_GET['test']))
        $test = $_GET['test'];
else
        $test = '';

--
Ryan



Sun, 31 Jul 2005 23:28:30 GMT  
 Undefined index - PHP Notice

Quote:
> Hi, I have the following trouble (or annoyance)

> I get these kind of errors in my php error log:

> [12-Feb-2003 15:47:52] PHP Notice:  Undefined index:  test in tst.php on
> line 9
> [12-Feb-2003 15:48:22] PHP Notice:  Undefined index:  test in tst.php on
> line 9

> I know they are not harmful, but I would like to learn how to avoid them.

> It's normally logged when I use these kind of statements in my code:

> if($_GET['test'])
> {
>     // do something here
> }

> It's ofcourse only when I don't send the get variable that the notice
> appears. How do I avoid getting the notice messages?

> I've tried stuff like this, and it works if I don't send any get
> variables but if I send one or more but doesn't name them to "test" it
> will still produce the notice error message.

> if($_GET)
> {
>    if($_GET['test']){
>      echo $_GET['test'];
>    }
> }

> I'm pretty sure there's some nice simple way around this so please tell
> me how ;)

> I know that I could change the error reporting level but that's not what
>   I'm looking for, I want to know how to write clean, error free code.

> Thanks for any help !

> /Andreas

In your php.ini file you'll find the following line:

error_reporting  =  E_ALL

It's on (approx) line 246.  Change it to read:

error_reporting  =  E_ALL & ~E_NOTICE

That'll disable the notices but keep all the other errors notifications.

--
- p o w e r b o y -

www: http://www.rarextreme.com



Sun, 31 Jul 2005 23:44:05 GMT  
 Undefined index - PHP Notice


Quote:
> Hi, I have the following trouble (or annoyance)

> I get these kind of errors in my php error log:

> [12-Feb-2003 15:47:52] PHP Notice:  Undefined index:  test in tst.php
> on line 9
> [12-Feb-2003 15:48:22] PHP Notice:  Undefined index:  test in tst.php
> on line 9

Two ways: modify your php.ini error_reporting setting, changing it to this:

   error_reporting  =  E_ALL & ~E_NOTICE

Alternatively, you can turn off NOTICEs temporarily like so:

   error_reporting (E_ALL ^ E_NOTICE);           // turns off NOTICEs
    < ... code that might throw a notice ... >
   error_reporting ();                           // goto original state

--
eth'nT
http://www.hydrous.net
aim: courtarro



Sun, 31 Jul 2005 23:43:44 GMT  
 Undefined index - PHP Notice

Quote:
>I know that I could change the error reporting level but that's not what
> I'm looking for, I want to know how to write clean, error free code.

I missed that last bit...


Sun, 31 Jul 2005 23:51:05 GMT  
 Undefined index - PHP Notice

Quote:

> if (isset($_GET['test']))
> $test = $_GET['test'];
> else
> $test = '';

Alternatively, take a look at empty(), which is has the opposite effect of
isset().

Regards,

David

--
David Jonathan Grant
http://www.davidjonathangrant.info/



Sun, 31 Jul 2005 23:44:43 GMT  
 Undefined index - PHP Notice

Quote:

> I missed that last bit...

Doesn't look like you were the only one! :)

Regards,

David

--
David Jonathan Grant
http://www.davidjonathangrant.info/



Sun, 31 Jul 2005 23:46:18 GMT  
 Undefined index - PHP Notice
Yeah, so I missed the error code change too ... it was too early.

--
eth'nT
http://www.hydrous.net
aim: courtarro



Mon, 01 Aug 2005 00:13:14 GMT  
 Undefined index - PHP Notice

Quote:
> if (isset($_GET['test']))
>    $test = $_GET['test'];
> else
>    $test = '';

$test = isset($_GET['test']) ? $_GET['test'] : '';

--

never lived never lived :: i'm in love   -- black rebel motorcycle club



Mon, 01 Aug 2005 01:39:38 GMT  
 Undefined index - PHP Notice

Quote:


>> Hi, I have the following trouble (or annoyance)

>> I get these kind of errors in my php error log:

>> [12-Feb-2003 15:47:52] PHP Notice:  Undefined index:  test in tst.php
>> on line 9
>> [12-Feb-2003 15:48:22] PHP Notice:  Undefined index:  test in tst.php
>> on line 9

>> I know they are not harmful, but I would like to learn how to avoid them.

>> It's normally logged when I use these kind of statements in my code:

>> if($_GET['test'])
>> {
>>    // do something here
>> }

>> It's ofcourse only when I don't send the get variable that the notice
>> appears. How do I avoid getting the notice messages?

>> I've tried stuff like this, and it works if I don't send any get
>> variables but if I send one or more but doesn't name them to "test" it
>> will still produce the notice error message.

> The right way to avoid them is to use isset()

> if (isset($_GET['test']))
>     $test = $_GET['test'];
> else
>     $test = '';

Ah, great it has been annoying me for quite some time now..
Thanks again for the help ;)

- Show quoted text -

Quote:
> --
> Ryan



Mon, 01 Aug 2005 02:00:32 GMT  
 
 [ 10 post ] 

 Relevant Pages 

1. switching index.html to index.php

2. Undefined node type using php

3. $_POST brings up Undefined index error

4. Undefined function imagecreatefromjpeg with bundled GD in PHP 4.3

5. PHP 4.3.0: Fatal error: call to undefined function: readline()

6. Withdrawal notice of IBM Ada products Withdrawal notice and transfer of IBM Ada products

7. Php and file indexing

8. index.php loading automatically

9. HELP: index.php?page=files

10. Pb php4/apache2 avec index.php en module

11. index.php/params filename spoofing

12. Please help with Index errors in setup.php

 

 
Powered by phpBB® Forum Software