WordPress Notice:Undefined variable Solution
During WordPress development, it is not uncommon to encounter the warning message “Notice : Undefined variable”, which pinpoints the file and line where the problem occurs. The reason for this warning is that the variable is used before it is defined. Take a look at the following code.
$action = $_GET['action'];
$output = $output . 'Output'- In the first line of code, if there is no action parameter in the get request, the
$actionThe variable is not defined - And in the second line, $output is not defined before the connection string.
Used before the variable is defined, theUndefined variableThe warning message.
Knowing the cause solves the problem, and there are several ways to fix it.
Method I:
Initializing the variable before using it is the correct solution, for the code that went wrong above, just write it in the following way.
$action = ( $_GET['action'] ) ? $_GET['action'] : '';
$output = '';
$output = $output . 'Output'Method II:
Putting the @ symbol in front of a variable, in PHP, the @ symbol in front of the variable name serves to hide the error messages that appear in the variable, and it is not recommended to use this method because the variable may have other error messages, and if they are hidden, it is not easy to find out what the problem is, except for the problem.
Method III.
Modify php.ini , unrealistic warnings, again this method is not recommended, the role of the warning message is to alert us to possible problems in the program, to provide us with the basis for solving the problem , if hidden, possible problems will be directly ignored. Because this method is not recommended, the specific modification changes will not be said here. Problems arise, directly in accordance with the first approach to solve it.
Thanks for sharing.
No thanks, come on!