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:
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
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 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
If working with classes you need to make sure you reference member variables using
$this
: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.I use all time own useful function exst() which automatically declare variables.
Your code will be -