Notice: Undefined variable

This error means that within your code, there is a variable or constant which is not set. But you may be trying to use that variable.

Đang xem: How to check if a variable is undefined in php

The error can be avoided by using the isset()function.This function will check whether thevariable is set or not.

Error Example:

Output:

darkedeneurope.comNotice: Undefined variable: age in estsite.locvaraible.php on line 4In the above example, we are displaying value stored in the‘name’ and ‘age’variable, but we didn’t set the ‘age’ variable.

*

Here are two ways to deal with such notices.

Resolve such notices.Ignore such notices.

Fix Notice: Undefined Variable by using isset() Function

This notice occurs when you use any variable in your PHP code, which is not set.

Xem thêm: Giải Đáp Thắc Mắc Của Các Mẹ Bầu: Ăn Gì Để Đẻ Không Đau ? Ăn Gì Để Sinh Con Không Đau

Solutions:

To fix this type of error, you can define the variable as global and use the isset() function to check if thevariable is set or not.

Example:

“;echo “Age: ” . $age;?>

Set Index as blank

“;echo “Age: ” . $age;?>

Ignore PHP Notice: Undefined variable

You can ignore this notice by disabling reporting of notice with option error_reporting.

1.Disable Display Notice in php.ini file

Open php.ini file in your favorite editor and search for text “error_reporting”the default value is E_ALL. You can change it to E_ALL & ~E_NOTICE.

By default:

error_reporting = E_ALL

Change it to:

error_reporting = E_ALL & ~E_NOTICE

Now your PHP compiler will show all errors except “Notice.”

2.

Xem thêm: File Html Là File Gì Và Tại Sao Nó Quan Trọng Với Website? Cho Xin Phần Mềm Đọc Html

Disable Display Notice in PHP Code

If you don’t have access to make changes in the php.ini file, In this case, you need to disable the notice by adding the following code on the top of your PHP page.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *