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:
Try these
Or, as a quick and dirty solution:
Note about sessions:
When using sessions,
session_start();
is required to be placed inside all files using sessions.http://php.net/manual/en/features.sessions.php
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:
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
Generally because of "bad programming", and a possibility for mistakes now or later.
if (isset($varname))
, before using itIt 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.
I used to curse this error, but it can be helpful to remind you to escape user input.
For instance, if you thought this was clever, shorthand code:
...Think again! A better solution is:
(I use a custom
html()
function to escape characters, your mileage may vary)