Home >Backend Development >PHP Tutorial >How to fix PHP Undefined index error_PHP tutorial
Although this prompt can be hidden by setting the error display mode, this also has a hidden danger, that is, these prompts will be recorded in the server's log, causing the log file to be abnormally large.
First of all, this is not an error, it is a warning. So if the server cannot be changed, each variable should be defined before use. There are several popular solutions on the Internet:
Method 1: Modify the server configuration. Modify the php.ini configuration file, error_reporting = E_ALL & ~E_NOTICE.
Method 2: Initialize the variables and write them in a standardized way (it is more cumbersome because there are a large number of variables). But I haven't found a good definition method yet, I hope you can give me some advice.
Method 3: Add: error_reporting(0) to the header of each file; If that doesn’t work, just open php.ini, find display_errors, and set display_errors = Off. Any future errors will not be prompted.
Method 4: Make judgment: isset($_GET["page"]) if-else judgment. Or add '@' to indicate that this line should not be output if there is an error or warning. For example: @$page=$_GET["page"]
Method 5: The file1.php file assigns a value to the $xx variable and passes it to file2.php using post. If file2.php does not have the definition of $xx, and If you use $yy=$xx; directly, the system will report an error: "undifined variaable $xx". If the file file2.php starts to be defined with $xx="";, then the $xx value of file1.php cannot be passed. This can be done in file2.php: if(!isset($xx)) $xx="";
If you feel that the above method is not very easy to use, you can also use the following method: