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:
Using a ternary is simple, readable, and clean:
Pre PHP 7
Assign a variable to the value of another variable if it's set, else assign
null
(or whatever default value you need):PHP 7+
The same except using Null Coalescing Operator. There's no longer a need to call
isset()
as this is built in, and no need to provide the variable to return as it's assumed to return the value of the variable being checked:Both will stop the Notices from the OP question, and both are the exact equivalent of:
If you don't require setting a new variable then you can directly use the ternary's returned value, such as with
echo
, function arguments, etc:Echo:
Function:
The above will work just the same with arrays, including sessions etc, replacing the variable being checked with e.g.:
$_SESSION['checkMe']
or however many levels deep you need, e.g.:
$clients['personal']['address']['postcode']
Suppression:
It is possible to suppress the PHP Notices with
@
or reduce your error reporting level, but it does not fix the problem, it simply stops it being reported in the error log. This means that your code still tried to use a variable that was not set, which may or may not mean something doesn't work as intended - depending on how crucial the missing value is.You should really be checking for this issue and handling it appropriately, either serving a different message, or even just returning a null value for everything else to identify the precise state.
If you just care about the Notice not being in the error log, then as an option you could simply ignore the error log.
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()
:The best way for getting input string is:
This one-liner is almost equivalent to:
If you absolutely want string value, just like:
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.
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:
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