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.
Bạn đ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 mix or not.
darkedeneurope.comNotice: Undefined variable: age in estsite.locvaraible.php on line 4In the above example, we are displaying value stored in the‘name’ và ‘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.
To fix this type of error, you can define the variable as global và use the isset() function to lớn check if thevariable is set or not.
";echo "Age: " . $age;?>
Set Index as blank";echo "Age: " . $age;?>
Ignore PHP Notice: Undefined variableYou 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 tìm kiếm for text “error_reporting”the mặc định value is E_ALL. You can change it to E_ALL & ~E_NOTICE.
error_reporting = E_ALL
Change it to:
error_reporting = E_ALL và ~E_NOTICE
Now your PHP compiler will show all errors except "Notice."
2.Disable Display Notice in PHP Code
If you don’t have access lớn make changes in the php.ini file, In this case, you need khổng lồ disable the notice by adding the following code on the đứng đầu of your PHP page.