PHP Error Constant E_NOTICE
The PHP E_NOTICE Constant is a non-fatal run time notice and E_NOTICE Constant has an Integer value 8.
PHP engine doesn't halt the script execution when it arises and it logs the notice in error_log file. When an E_NOTICE occurs, you may not lose whatever data the program was currently processing
and it mostly occurs in undefined usage of server variables, local variables, instance variables, static variables, constants or an undefined index of an array.
Example: #1- PHP Error Constant E_NOTICE
Check the undefined variable holds a positive or negative number, it will throw a notice.
PHP Input Screen
<?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");
}
PHP Output Screen
Notice: Undefined variable: i in ~\Documents\php-error-constants.php on line 7
The number is positive
Example: #2- PHP Error Constant E_NOTICE
Print the undefined variable using echo function, it will throw a notice.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Un-initialized variable $msg
echo($msg);
PHP Output Screen
Notice: Undefined variable: msg in ~\Documents\php-error-constants.php on line 7
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #3- PHP Error Constant E_NOTICE
Get the value from a server variable $_GET using an undefined index 'msg', it will throw a notice.
PHP Input Screen
<?php
ini_set("display_startup_errors", 1);
ini_set("display_errors", 1);
error_reporting(-1);
// Un-initialized server variable
$str = $_GET['msg'];
PHP Output Screen
Notice: Undefined index: msg in ~\Documents\php-error-constants.php on line 7
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #4- PHP Error Constant E_NOTICE
Check the server has a HTTPS protocol using a server variable $_SERVER using an undefined index 'HTTPS', it will throw a notice only if the server has a HTTP protocol.
PHP Input Screen
<?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");
}
PHP Output Screen
Notice: Undefined index: HTTPS in ~\Documents\php-error-constants.php on line 8
The server doesn't hava a valid HTTPS Protocol
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #5- PHP Error Constant E_NOTICE
Print the undefined instance variable using echo function, it will throw a notice.
PHP Input Screen
<?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();
PHP Output Screen
Notice: Undefined variable: msg in ~\Documents\php-error-constants.php on line 11
BBMINFO