Using PHP's "Caution: undefined variable", "Caution: undefined index", "Warning: undefined array key" and "Caution: undefined offset"
P粉851401475
2023-08-23 11:45:50
<p>I am running a PHP script and continue to receive errors like this: </p>
<blockquote>
<p>Notice: Undefined variable: my_variable_name in C:wampwwwmypathindex.php on line 10</p>
<p>Note: Undefined index: my_index C:wampwwwmypathindex.php line 11</p>
<p>Warning: Array key 'my_index' is not defined in C:wampwwwmypathindex.php on line 11</p>
</blockquote>
<p>Lines 10 and 11 look like this: </p>
<pre class="brush:php;toolbar:false;">echo "My variable value is: " . $my_variable_name;
echo "My index value is: " . $my_array["my_index"];</pre>
<p>What do these error messages mean? </p>
<p>Why do they appear suddenly? I've been using this script for years and never had any problems. </p>
<p>How to fix them? </p>
<hr />
<blockquote>
<p><sub><strong>This is a general reference question</strong> so people can link to duplicate questions without having to explain the question over and over. I feel this is necessary because most real-world answers to this question are very specific. </sub></p>
<p><sub>Related meta-discussion:</sub></p>
<ul>
<li><sub>What should I do about duplicate questions? </sub></li>
<li><sub>Does "reference question" make sense? </sub></li>
</ul>
</blockquote><p><br /></p>
Try these
Or, as a quick and dirty solution:
Notes on sessions:
When using sessions, you need to place
session_start();
in all files that use sessions.http://php.net/manual/en/features.sessions .php
This error message is intended to help PHP programmers detect typos or errors when accessing a variable (or array element) that does not exist. So a good programmer:
Notification/Warning: Undefined variable
While PHP does not require variable declaration, it does recommend it to avoid some security holes or bugs where people forget to assign a value to a variable that is used later in the script. PHP emits an
E_WARNING
level error when a variable is not declared.This warning helps programmers spot misspelled variable names or similar types of errors (such as assigning a value to a variable within a condition that evaluates to false). Additionally, there can be other problems with uninitialized variables. As stated in the PHP manual,
This means that the variable may get its value from the included file, and that value will be used instead of the
null
expected for accessing an uninitialized variable, which may lead to unpredictable results. To avoid this, it is best to initialize all variables in PHP files before use.Methods to deal with problems:
Recommendation: Declare each variable before using it. That way you'll only see this error if you actually make the mistake of trying to use a variable that doesn't exist - which is exactly why this error message exists.
Special case when the variable is defined but not visible in the function. Functions in PHP have their own variable scope, if you need to use an external variable in a function, its value must be passed as a parameter of the function:
Use the empty coalescing operator to suppress errors. But remember, this way PHP won't be able to notify you that you used the wrong variable name.
For older PHP versions (
Please note that it is still essentially error suppression, albeit only for a specific error. Therefore, it may prevent PHP from helping you by marking uniform variables.
Use @operator to suppress errors. Leaving here for historical reasons, but seriously, this shouldn't happen.
Note: It is strongly recommended to only implement point 1.
Note: undefined index/undefined offset/warning: undefined array key
This notice/warning occurs when you (or PHP) try to access an undefined index of an array.
Internal array
The attitude should be exactly the same when dealing with internal arrays defined in code: just initialize all keys before use. This way, the bug will do its intended job: notify programmers of errors in their code. So the method is the same:
Recommended: Declare your array elements:
A special case is when some functions return arrays or other values (such as
null
orfalse
). Then you must perform a test before trying to access the array elements, such asExternal array
For external arrays (such as
$_POST
/$_GET
/$_SESSION
or JSON input), the situation is a bit different because the programmer has no control over this class The contents of the array. Therefore, it is reasonable to check if certain keys exist or even assign default values to missing keys.When a PHP script contains an HTML form, it is natural that there will be no form content when it is first loaded. So such a script should check if the form has been submitted
Some HTML form elements (such as checkboxes) will not be sent to the server if they are unchecked. In this case, it is justified to use the null coalescing operator to assign a default value
Optional QUERY STRING elements or cookies should be handled the same way
But the allocation should be done at the beginning of the script. Validate all inputs , assign them to local variables, and use them throughout your code. Therefore, every variable you access will exist on purpose.
Related: