I'm running a PHP script and continue to receive errors like:
Notice: Undefined variable: my_variable_name in C:\wamp\www\mypath\index.php on line 10
Notice: Undefined index: my_index C:\wamp\www\mypath\index.php on line 11
Line 10 and 11 looks like this:
echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];
What is the meaning of these error messages?
Why do they appear all of a sudden? I used to use this script for years and I've never had any problem.
How do I fix them?
This is a General Reference question for people to link to as duplicate, instead of having to explain the issue over and over again. I feel this is necessary because most real-world answers on this issue are very specific.
Related Meta discussion:
In PHP you need fist to define the variable after that you can use it.
We can check variable is defined or not in very efficient way!.
Simple Explanation
the quick fix is to assign your variable to null at the top of your code
Regarding this part of the question:
No definite answers but here are a some possible explanations of why settings can 'suddenly' change:
You have upgraded PHP to a newer version which can have other defaults for error_reporting, display_errors or other relevant settings.
You have removed or introduced some code (possibly in a dependency) that sets relevant settings at runtime using
ini_set()
orerror_reporting()
(search for these in the code)You changed the webserver configuration (assuming apache here):
.htaccess
files and vhost configurations can also manipulate php settings.Usually notices don't get displayed / reported (see PHP manual) so it is possible that when setting up the server, the php.ini file could not be loaded for some reason (file permissions??) and you were on the default settings. Later on, the 'bug' has been solved (by accident) and now it CAN load the correct php.ini file with the error_reporting set to show notices.
The best way for getting input string is:
This one-liner is almost equivalent to:
If you absolutely want string value, just like:
In reply to ""Why do they appear all of a sudden? I used to use this script for years and I've never had any problem."
It is very common for most sites to operate under the "default" error reporting of "Show all errors, but not 'notices' and 'deprecated'". This will be set in php.ini and apply to all sites on the server. This means that those "notices" used in the examples will be suppressed (hidden) while other errors, considered more critical, will be shown/recorded.
The other critical setting is the errors can be hidden (i.e.
display_errors
set to "off" or "syslog").What will have happened in this case is that either the
error_reporting
was changed to also show notices (as per examples) and/or that the settings were changed todisplay_errors
on screen (as opposed to suppressing them/logging them).Why have they changed?
The obvious/simplest answer is that someone adjusted either of these settings in php.ini, or an upgraded version of PHP is now using a different php.ini from before. That's the first place to look.
However it is also possible to override these settings in
and any of these could also have been changed.
There is also the added complication that the web server configuration can enable/disable .htaccess directives, so if you have directives in .htaccess that suddenly start/stop working then you need to check for that.
(.htconf / .htaccess assume you're running as apache. If running command line this won't apply; if running IIS or other webserver then you'll need to check those configs accordingly)
Summary
error_reporting
anddisplay_errors
php directives in php.ini has not changed, or that you're not using a different php.ini from before.error_reporting
anddisplay_errors
php directives in .htconf (or vhosts etc) have not changederror_reporting
anddisplay_errors
php directives in .htaccess have not changederror_reporting
anddisplay_errors
php directives have been set there.Notice: Undefined variable
From the vast wisdom of the PHP Manual:
From PHP documentation:
This means that you could use only
empty()
to determine if the variable is set, and in addition it checks the variable against the following,0
,0.0
,""
,"0"
,null
,false
or[]
.Example:
Test the above snippet in the 3v4l.org online PHP editor
Although PHP does not require a variable declaration, it does recommend it in order to avoid some security vulnerabilities or bugs where one would forget to give a value to a variable that will be used later in the script. What PHP does in the case of undeclared variables is issue a very low level error,
E_NOTICE
, one that is not even reported by default, but the Manual advises to allow during development.Ways to deal with the issue:
Recommended: Declare your variables, for example when you try to append a string to an undefined variable. Or use
isset()
/!empty()
to check if they are declared before referencing them, as in:This has become much cleaner as of PHP 7.0, now you can use the null coalesce operator:
Set a custom error handler for E_NOTICE and redirect the messages away from the standard output (maybe to a log file):
Disable E_NOTICE from reporting. A quick way to exclude just
E_NOTICE
is:Suppress the error with the @ operator.
Note: It's strongly recommended to implement just point 1.
Notice: Undefined index / Undefined offset
This notice appears when you (or PHP) try to access an undefined index of an array.
Ways to deal with the issue:
Check if the index exists before you access it. For this you can use
isset()
orarray_key_exists()
:The language construct
list()
may generate this when it attempts to access an array index that does not exist:Two variables are used to access two array elements, however there is only one array element, index
0
, so this will generate:$_POST
/$_GET
/$_SESSION
variableThe notices above appear often when working with
$_POST
,$_GET
or$_SESSION
. For$_POST
and$_GET
you just have to check if the index exists or not before you use them. For$_SESSION
you have to make sure you have the session started withsession_start()
and that the index also exists.Also note that all 3 variables are superglobals and are uppercase.
Related: