trigger_error($message, $level) raises an error from your own code at an E_USER_* level (default E_USER_NOTICE), through the same handlers and settings as the engine's messages. Its main modern use is E_USER_DEPRECATED, which warns callers without breaking them.
<?php
error_reporting(E_ALL); // include E_USER_DEPRECATED
trigger_error('legacyPrice() is deprecated, use Price::of()', E_USER_DEPRECATED);
trigger_error('discount of 80% capped at 50%', E_USER_WARNING);
trigger_error('stock file missing', E_USER_ERROR);Output
PHP Deprecated: legacyPrice() is deprecated, use Price::of() in /home/dev/shop/trigger.php on
line 3
PHP Warning: discount of 80% capped at 50% in /home/dev/shop/trigger.php on line 4
PHP Deprecated: Passing E_USER_ERROR to trigger_error() is deprecated since 8.4, throw an
exception or call exit with a string message instead in /home/dev/shop/trigger.php on line 5
PHP Fatal error: stock file missing in /home/dev/shop/trigger.php on line 5
Stack trace:
#0 /home/dev/shop/trigger.php(5): trigger_error()
#1 {main}Throw an exception instead of E_USER_ERROR. To deprecate a whole function, the #[\Deprecated] attribute (PHP 8.4, Built-In Attributes) is better still: the engine raises the notice, and IDEs see it.