3. several solutions for undefinedindex prompt in php I. Related information
- When you use $ _ post [''] or $ _ get [''] to obtain parameters in a form, the following error occurs: Notice: Undefined index :--------;
- And the Undefined index error is often reported when we receive data from form POST.
For example: $ act = $ _ POST ['action']; when you use the above code, the system always prompts Notice: Undefined index: act in D: \ test \ post. php on line 20 In addition, sometimes Notice: Undefined variable: Submit ...... and so on.The above are PHP prompts rather than errors. PHP itself can be directly used without declaring the variables in advance, but a prompt will be prompted if the variables are not declared. Generally, when a website is a formal website, it will turn off the prompts and even the error message.
II. problem description
That is, PHP prompts undeclared variables by default, but this default prompt can be ignored.
III. Solution 1: server configuration modification
- Modify the error display mode in the error configuration in php. ini: change error_reporting = E_ALL to error_reporting = E_ALL &~ E_NOTICE
- The modification takes effect only after the Apache server is restarted.
Method 2: initialize the variable
That is, after a variable is defined, it will be initialized, but this does not determine whether a variable is initialized due to event-driven.
Method 3: perform isset ($ _ post ['']), empty ($ _ post ['']) if -- else judgment method 4: Add @ Before the notice code appears @
@ Indicates that this line has errors or warnings. for example, @ $ username = $ _ post ['username']. add @ in front of the variable @, for example, if (@ $ _ GET ['action'] = 'save '){...
- In this way, if this statement has a warning, no output will be made.
Method 5: Build a function to replace the value method.
The function code is as follows:
function _get($str){ $val = !empty($_GET[$str]) ? $_GET[$str] : null; return $val; }
Then, replace $ _ get ['str'] with _ GET ('str ~
IV. Analysis Summary
Although PHP provides a good reminder mechanism, it may not be what we want,We recommend that you use method 4 to handle the problem.In this way, the reminder mechanism provided by PHP can be retained while the reminder is observed.