E_NOTICE (8) identifies a runtime notice. The examples show historical PHP 7 diagnostics. PHP 8 reports undefined variables, array keys, and properties as warnings.
A simple example
E_NOTICE
Start withThe PHP constant E_NOTICE
Do thisRead its diagnostic-level value
Result8
The constant names the notice level in error-reporting masks. Individual diagnostics can change severity across PHP versions.
Example: #1- PHP Error Constant E_NOTICE
A student tests a conditional message before its input variable has been assigned. Check the diagnostic against the PHP version used in class.
PHP diagnosticE_NOTICE
First output lineNotice: Undefined variable: i in ~\Documents\php-error-constants.php on line 7
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Un-initialized variable $i
if($i >= 0) {
echo("The number is positive");
} else {
echo("The number is negative");
}
Output
Notice: Undefined variable: i in ~\Documents\php-error-constants.php on line 7
The number is positive
Advertisements
Example: #2- PHP Error Constant E_NOTICE
A front-desk screen prints a greeting before its message variable exists. The diagnostic helps the developer trace the missing initialization.
PHP diagnosticE_NOTICE
ResultNotice: Undefined variable: msg in ~\Documents\php-error-constants.php on line 7
Notice: Undefined variable: msg in ~\Documents\php-error-constants.php on line 7
Example: #3- PHP Error Constant E_NOTICE
A pupil opens a page directly without the optional message normally included in its link. Check for the query key before the page tries to display that message.
PHP diagnosticE_NOTICE
ResultNotice: Undefined index: msg in ~\Documents\php-error-constants.php on line 7
Notice: Undefined index: msg in ~\Documents\php-error-constants.php on line 7
Example: #4- PHP Error Constant E_NOTICE
A local preview may lack the HTTPS key supplied by the hosted web server. Check the server data before asking the template to report the connection state.
PHP diagnosticE_NOTICE
First output lineNotice: Undefined index: HTTPS in ~\Documents\php-error-constants.php on line 8
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// The notice will arise only if the page was loaded in HTTP protocol
// in other words this notice occur only in HTTP Protocol
if($_SERVER[HTTPS] == "on") {
echo("The server has a valid HTTPS Protocol");
} else {
echo("The server doesn't hava a valid HTTPS Protocol");
}
Output
Notice: Undefined index: HTTPS in ~\Documents\php-error-constants.php on line 8
The server doesn't hava a valid HTTPS Protocol
Example: #5- PHP Error Constant E_NOTICE
A roster view assumes an object has a field that its importer never sets. The diagnostic points the developer to the property read in the view.
PHP diagnosticE_NOTICE
ResultNotice: Undefined variable: msg in ~\Documents\php-error-constants.php on line 11
PHP source code
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
class HelloWorld {
public $msg;
public function greetings() {
// undefined instance variable
echo($msg);
}
}
$obj = new HelloWorld();
$obj->greetings();
Output
Notice: Undefined variable: msg in ~\Documents\php-error-constants.php on line 11