PHP suggests Notice: Undefined variable's solution

  • 2020-05-26 08:05:04
  • OfStack

The default configuration of PHP will report this error. My version of PHP is 5.2.13. There is this problem:
Notice: Undefined variable
This is about printing out warnings on the page, and while this is good for exposing problems, there are a lot of problems with the implementation.

You need to set the display error level to fix the problem.
A common solution on the network is to modify the configuration of php.ini:

Solutions:

1) error_reporting setting:

Find error_reporting = E_ALL
Change to error_reporting = E_ALL & ~E_NOTICE

2) register_globals setting:

Find register_globals = Off
Change to register_globals = On

I found it used directly in the php code

error_reporting(E_ALL & ~E_NOTICE);

Can solve this problem, tips down 1, hehe.

The following is a supplement:

Notice: Undefined variable: email in D:\PHP5\ENOTE\ADDNOTE.PHP on line 9
Notice: Undefined variable: subject in D:\PHP5\ENOTE\ADDNOTE.PHP on line 9
Notice: Undefined variable: comment in D:\PHP5\ENOTE\ADDNOTE.PHP on line 9

........

In fact, the above is the undefined variable, we directly determine the variable caused by the code.

php doesn't need to define variables, but what if that happens?

Just find php.ini in C:\WINDOWS

In line 302 of php.ini, error_reporting = E_ALL

Modified into

error_reporting = E_ALL & ~E_NOTICE and restart apache2.2

Solution: modify php.ini

Put: error_reporting = E_ALL

Change to: error_reporting = E_ALL & ~E_NOTICE

If you don't want any errors to be displayed, modify them directly:

display_errors = Off

If you do not have permission to modify php.ini, you can do so in the php header

ini_set("error_reporting","E_ALL & ~E_NOTICE");

Can be


We know in php, variables are typically don't need to define the used directly, but sometimes because php environment problems will appear all sorts of strange things, today when the debugger directly tip Notice: Undefined variable mistake, this is because some variable is not defined, is dizzy, ok, this problem can get this done in 1 block error sentences directly.

Insert a sentence error_reporting(0) directly at the top of the program. I believe that will not expose such a problem, also do not delay the operation of the program, of course, this is only an emergency method, we still want to check the program where the problem, and then the remedy, to directly block the problem summer blog summed up several ways.

Modifications in the php.ini configuration file

In line 302 of php.ini, error_reporting = E_ALL is changed to error_reporting = E_ALL & ~E_NOTICE and restart apache2.2. If you don't want any errors to be displayed, directly modify: display_errors = Off

Modification in php file

If you do not have permission to modify php.ini, you can add ini_set("error_reporting","E_ALL ") to the php header & ~ E_NOTICE "); Or error_reporting (0); / / xiariboke com can.

In general, it is recommended to add in php file directly. This method is simple without modifying php.ini on the server.


Related articles: