Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating PHP warnings

How can I generate custom PHP warnings in the same way that warnings for the built-in functions work.

For example:

php > fopen(null);
PHP Warning:  fopen() expects at least 2 parameters, 1 given in php shell code on line 1
php > fopen(null, 'w');
PHP Warning:  fopen(): Filename cannot be empty in php shell code on line 1
php > fopen(array('a'), 'w');
PHP Warning:  fopen() expects parameter 1 to be a valid path, array given in php shell code on line 1

Let's say I have a function as follows:

function my_func($a, $b, $c);

What code can I use such that my_func will throw similar warnings when called incorrectly?

like image 315
Joshua Spence Avatar asked Jul 30 '26 18:07

Joshua Spence


1 Answers

You're probably looking for trigger_error. These will show a standard error and are handled by the error_log, display_errors, etc. settings in php.ini

A minimal example is:

if (!DoAFunction()) {
  trigger_error("DoAFunction returned false!", E_USER_ERROR);
}
like image 111
Mark Ormston Avatar answered Aug 02 '26 09:08

Mark Ormston



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!