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:
Those notices are because you don't have the used variable
defined
andmy_index
key was not present into$my_array
variable.Those notices were triggered every time, because your
code
is not correct, but probably you didn't have the reporting of notices on.Solve the bugs:
Another way to get this out:
It means you are testing, evaluating, or printing a variable that you have not yet assigned anything to. It means you either have a typo, or you need to check that the variable was initialized to something first. Check your logic paths, it may be set in one path but not in another.
undefined index means in an array you requested for unavailable array index for example
undefined variable means you have used completely not existing variable or which is not defined or initialized by that name for example
undefined offset means in array you have asked for non existing key. And the solution for this is to check before use
In PHP 7.0 it's now possible to use Null coalescing operator:
Equals to:
PHP manual PHP 7.0
Error display
@
operatorFor undesired and redundant notices, one could use the dedicated
@
operator to »hide« undefined variable/index messages.isset?:
or??
super-supression however. Notices still can get logged. And one may resurrect@
-hidden notices with:set_error_handler("var_dump");
if (isset($_POST["shubmit"]))
in your initial code.@
orisset
only after verifying functionality.@
is mainly acceptable for$_GET
/$_POST
input parameters, specifically if they're optional.And since this covers the majority of such questions, let's expand on the most common causes:
$_GET
/$_POST
/$_REQUEST
undefined inputFirst thing you do when encountering an undefined index/offset, is check for typos:
$count = $_GET["whatnow?"];
Secondly, if the notice doesn't have an obvious cause, use
var_dump
orprint_r
to verify all input arrays for their curent content:Both will reveal if your script was invoked with the right or any parameters at all.
Alternativey or additionally use your browser devtools (F12) and inspect the network tab for requests and parameters:
POST parameters and GET input will be be shown separately.
For
$_GET
parameters you can also peek at theQUERY_STRING
inPHP has some rules to coalesce non-standard parameter names into the superglobals. Apache might do some rewriting as well. You can also look at supplied raw
$_COOKIES
and other HTTP request headers that way.More obviously look at your browser address bar for GET parameters:
http://example.org/script.php?id=5&sort=desc
The
name=value
pairs after the?
question mark are your query (GET) parameters. Thus this URL could only possibly yield$_GET["id"]
and$_GET["sort"]
.Finally check your
<form>
and<input>
declarations, if you expect a parameter but receive none.<input name=FOO>
id=
ortitle=
attribute does not suffice.method=POST
form ought to populate$_POST
.method=GET
(or leaving it out) would yield$_GET
variables.action=script.php?get=param
via $_GET and the remainingmethod=POST
fields in $_POST alongside.$_REQUEST['vars']
again, which mashes GET and POST params.If you are employing mod_rewrite, then you should check both the
access.log
as well as enable theRewriteLog
to figure out absent parameters.$_FILES
$_FILES["formname"]
.enctype=multipart/form-data
method=POST
in your<form>
declaration.$_COOKIE
$_COOKIE
array is never populated right aftersetcookie()
, but only on any followup HTTP request.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: