-4

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I upgrade/install PHP 5.4.4 (xampp 1.8.0) in localhost. now i see many notice error in my page. what's problem? how to fix this?

Secion Of Error :

Notice: Undefined index: language in C:\xampp\htdocs\tube\include\config.php on line 93

Notice: Undefined variable: max_avatar_width in C:\xampp\htdocs\tube\include\lang\english.php on line 495

Notice: Undefined variable: max_avatar_height in C:\xampp\htdocs\tube\include\lang\english.php on line 496

Notice: Undefined index: USERID in C:\xampp\htdocs\tube\index.php on line 26

Config PHP page :

if ($_REQUEST['language'] != '') // <---- Line 93 
{
    if ($_REQUEST['language'] == 'english')
    {

        $_SESSION['language'] = 'english';
    }
    elseif ($_REQUEST['language'] == 'spanish')
    {

        $_SESSION['language'] = 'spanish';
    }
}

if ($_SESSION['language'] == "")
{

    $_SESSION['language'] = "english";
}

if ($_SESSION['language'] == "english")
{
include("lang/english.php");
}
elseif ($_SESSION['language'] == "spanish")
{
include("lang/spanish.php");
}
else
{
include("lang/english.php");
}

English Lang Page (line 495 and 496) :

$lang['491'] =  "The image width is too big. Max width is $max_avatar_width pixels."; 
$lang['492'] =  "The image height is too big. Max height is $max_avatar_height pixels.";

Index PHP Page:

if($_SESSION['USERID'] == "") // <-- Line 26
{
    $showfamfilter = "AND mature='0'";
}
elseif($_SESSION['FAMILYFILTER'] == "0")
{
    $showfamfilter = "";
}
else
{
    $showfamfilter = "AND mature='0'";
}
Community
  • 1
  • 1
BBKing
  • 2,279
  • 8
  • 38
  • 44
  • Could you clarify: What do you not understand from the message "undefined index" and "undefined variable"? Additional I guess, that this is not a PHP5.4 problem, but you havent setup your error-settings within your development environment properly. – KingCrunch Jul 30 '12 at 14:15
  • 1
    Did you think to try to correct the notice ? For the first `Notice: Undefined index`, you have to test if the index is set, etc ... – PoulsQ Jul 30 '12 at 14:15
  • 7
    Why is this downvoted so heavily, without further explanation or with more upvotes to the comments in here? Please help this guy improve his question. – Evert Jul 30 '12 at 14:27
  • 2
    @Evert : I do not understand why this is a negative vote. A little embarrassed and confused. I was looking for a solution JUST THIS :) Thanks Evert for Sympathy. – BBKing Jul 30 '12 at 14:34

4 Answers4

6

You can change the error_reporting setting in php.ini to not include E_NOTICE. There should be some examples right in the php.ini file.

However, this is unwise.. What you should do is fix your code. For example, instead of:

if ($_REQUEST['language'] != '')

You should write:

if (isset($_REQUEST['language']))

Fixing all E_NOTICE errors will make your code much more robust.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • 1
    Not right, `isset()` will only tell if the var is defined or not. It will not tell if it's different of `''` – PoulsQ Jul 30 '12 at 14:18
  • 2
    @PoulsQ: if you look at his sourcecode, just the isset will actually give the correct behavior. The lines are not identical in behavior, but the full script is. – Evert Jul 30 '12 at 14:24
  • Not the problem, you can't give a solution that will only work in 1 case ! If he has to use it anywhere else and don't understand what it does, he will make huge mistakes ! – PoulsQ Jul 30 '12 at 14:28
  • 1
    I said 'for example' and never implied that that fix will work for all his errors. Some are also not undefined index errors. I think you're being a bit pedantic. – Evert Jul 30 '12 at 14:29
1

At the end of your day, the issue is your code. You are trying to reference array items and indexes without being initialized first.

The reason you see it now, is the old server had E_NOTICE disabled for error_reporting. I bet a PHP.ini was never specified, as the default is to not display errors but log all non-notices and non-deprecation events.

; Common Values:
;   E_ALL & ~E_NOTICE  (Show all errors, except for notices and coding standards warnings.)
;   E_ALL & ~E_NOTICE | E_STRICT  (Show all errors, except for notices)
;   E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR  (Show only errors)
;   E_ALL | E_STRICT  (Show all errors, warnings and notices including coding standards.)
; Default Value: E_ALL & ~E_NOTICE
; Development Value: E_ALL | E_STRICT
; Production Value: E_ALL & ~E_DEPRECATED
; http://php.net/error-reporting
error_reporting = E_ALL & ~E_NOTICE | ~E_DEPRECATED

; This directive controls whether or not and where PHP will output errors,
; notices and warnings too. Error output is very useful during development, but
; it could be very dangerous in production environments. Depending on the code
; which is triggering the error, sensitive information could potentially leak
; out of your application such as database usernames and passwords or worse.
; It's recommended that errors be logged on production servers rather than
; having the errors sent to STDOUT.
; Possible Values:
;   Off = Do not display any errors
;   stderr = Display errors to STDERR (affects only CGI/CLI binaries!)
;   On or stdout = Display errors to STDOUT
; Default Value: On
; Development Value: On
; Production Value: Off
; http://php.net/display-errors
display_errors = Off

This is a bandaid and can be applied now:

error_reporting( E_ALL ^ ~E_NOTICE );

If you want to check if an array key exists before accessing it, use:

if(array_key_exists( 'key you are looking for', $array ) ){
    ....
}
Mike Mackintosh
  • 13,917
  • 6
  • 60
  • 87
  • `array_key_exists()` is times slower than both `empty()` and `isset()`. It should only be used if you need to check for an array element that can be set to `NULL`. – Narf Jul 30 '12 at 14:24
1

Using undefined variables, or undefined indexes in an array in expressions will raise an "Undefined variable" or "Undefined index" notice.

You can avoid this by always checking if the value is defined/null before using it. That way you are aware of the state of things in your application.

On the other hand both notices and warnings can be issued for abnormal - not necessarily incorrect - behavior. Which means that if you wish you can ignore them by setting the proper error_reporting level.

Mihai Stancu
  • 15,848
  • 2
  • 33
  • 51
0

You should check to see if the array index exists before attempting to access it, and make sure any variables are declared before you use them.

Matt
  • 6,993
  • 4
  • 29
  • 50