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:
The best way for getting input string is:
This one-liner is almost equivalent to:
If you absolutely want string value, just like:
If working with classes you need to make sure you reference member variables using
$this
: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.
In PHP 7.0 it's now possible to use Null coalescing operator:
Equals to:
PHP manual PHP 7.0
why not keep things simple?
These errors occur whenever we are using a variable that is not set.
The best way to deal with these is set error reporting on while development.
To set error reporting on:
On production servers, error reporting is off, therefore, we do not get these errors.
On the development server, however, we can set error reporting on.
To get rid of this error, we see the following example:
We can initialize the variables to
NULL
before assigning their values or using them.So, we can modify the code as:
This will not disturb any program logic and will not produce Notice even if
$test
does not have value.So, basically, its always better to set error reporting ON for development.
And fix all the errors.
And on production, error reporting should be set to off.