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 asked a question about this and I was referred to this post with the message:
I am sharing my question and solution here:
This is the error:
Line 154 is the problem. This is what I have in line 154:
I think the problem is that I am writing if conditions for the variable
$city
, which is not the key but the value in$key => $city
. First, could you confirm if that is the cause of the warning? Second, if that is the problem, why is it that I cannot write a condition based on the value? Does it have to be with the key that I need to write the condition?UPDATE 1: The problem is that when executing
$citiesCounterArray[$key]
, sometimes the$key
corresponds to a key that does not exist in the$citiesCounterArray
array, but that is not always the case based on the data of my loop. What I need is to set a condition so that if$key
exists in the array, then run the code, otherwise, skip it.UPDATE 2: This is how I fixed it by using
array_key_exists()
:Probably you were using old PHP version until and now upgraded PHP thats the reason it was working without any error till now from years. until PHP4 there was no error if you are using variable without defining it but as of PHP5 onwards it throws errors for codes like mentioned in question.
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:
When dealing with files, a proper enctype and a POST method are required, which will trigger an undefined index notice if either are not included in the form.
The manual states the following basic syntax:
HTML
PHP
Reference:
Another reason why an undefined index notice will be thrown, would be that a column was omitted from a database query.
I.e.:
Then trying to access more columns/rows inside a loop.
I.e.:
or in a
while
loop:Something else that needs to be noted is that on a *NIX OS and Mac OS X, things are case-sensitive.
Consult the followning Q&A's on Stack:
Are table names in MySQL case sensitive?
mysql case sensitive table names in queries
MySql - Case Sensitive issue of tables in different server
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.