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:
I didn't want to disable notice because it's helpful, but wanted to avoid too much typing.
My solution was this function:
So if I want to reference to $name and echo if exists, I simply write:
For array elements:
In page if I want to refer to $_REQUEST['name']:
Its because the variable '$user_location' is not getting defined. If you are using any if loop inside which you are declaring the '$user_location' variable then you must also have an else loop and define the same. For example:
The above code will create error as The if loop is not satisfied and in the else loop '$user_location' was not defined. Still PHP was asked to echo out the variable. So to modify the code you must do the following:
One common cause of a variable not existing after an HTML form has been submitted is the form element is not contained within a
<form>
tag:Example: Element not contained within the
<form>
Example: Element now contained within the
<form>
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.