w3resource

PHP error handling function user_error()

Description 

PHP function user_error() generates a user-level error or warning or notice.

The function can be used either along with the built-in error handler or with a user defined function which is set as the new error handler (set_error_handler()).

If you want to generate a specific response to an exception at runtime, using this function is useful.

This function is an alias of trigger_error().

Version:

PHP 4, PHP 5.

Syntax:

trigger_error(error_msg,  error_type )

Parameter:

Parameters Description Required / Optional Type
error_msg A string which will be displayed as an error message. Required string
error_type Specifies the error type for an error message.
Values may be -
1. E_USER_ERROR.
2. E_USER_WARNING.
3. E_USER_NOTICE.
Optional integer

Return Values:

The function returns FALSE if the wrong error_type is specified. Otherwise, it returns TRUE.

Example:

<?php
  $a = 12;
    $b = 5;
    
  if($a%$b == 0)
  trigger_error("This is an error",E_USER_NOTICE );
  ?> 

Previous: trigger_error()
Next: PHP Exercises Introduction



PHP: Tips of the Day

PHP: How to convert string to boolean?

Strings always evaluate to boolean true unless they have a value that's considered "empty" by PHP (taken from the documentation for empty):

  1. "" (an empty string);
  2. "0" (0 as a string)

If you need to set a boolean based on the text value of a string, then you'll need to check for the presence or otherwise of that value.

$test_mode_mail = $string === 'true'? true: false;

EDIT: the above code is intended for clarity of understanding. In actual use the following code may be more appropriate:

$test_mode_mail = ($string === 'true');

or maybe use of the filter_var function may cover more boolean values:

filter_var($string, FILTER_VALIDATE_BOOLEAN);

filter_var covers a whole range of values, including the truthy values "true", "1", "yes" and "on".

Ref : https://bit.ly/33SQ3eP