How to disable “Notice: Undefined Variable” in PHP5.x


To totally unlock this section you need to Log-in


Login

Scenario

My codes is working fine, but when I have upgraded to PHP5 (localhost), it's always show up "Notice: Undefined Variable...", I don't see any wrong in this case. 

How to disable "Notice: Undefined Variable" in PHP5.x

How to disable "Notice: Undefined Variable" in PHP5.x

Error_reporting function

You can use the error_reporting(0); statement in your code to disable all possible errors levels to be displayed on your output page.

The value admissible to error_reporting() function are:

# 0 - Turn off all error reporting
# 1 - Running errors
# 2 - Running errors + notices
# 3 - All errors except notices and warnings
# 4 - All errors except notices
# 5 - All errors

You can see the full function specification on: http://php.net/manual/en/function.error-reporting.php

Solution by coding

You might not want to disable the error message on every page that you have PHP on. Sometimes those errors can be helpful. I would advise that you use the method below:

[tweet]

PHP Code:

<?php

echo ini_get('display_errors');if (!ini_get('display_errors')) {
ini_set('display_errors', 1);
}echo ini_get('display_errors');
?>

Other solution

You can use @ char before variable name (like @$yourvariable) to hide any message.

1 thought on “How to disable “Notice: Undefined Variable” in PHP5.x”

Comments are closed.