25: Show Function Arguments in PHP Errors
Overview
Section titled “Overview”Compare the same warning with argument display turned off and on. This setting can help you identify the inputs that reached a failing function.
Prerequisites
Section titled “Prerequisites”- PHP 8.6 CLI. Confirm the version with
php --version. - Review the previous chapter if you’re following the series in order.
Allow about 10 minutes for the walkthrough and exercise.
What You’ll Build
Section titled “What You’ll Build”A runnable error-include-args.php example.
Step 1: Run the example (~5 min)
Section titled “Step 1: Run the example (~5 min)”Run the following from the root of the Code with PHP repository.
php code/php-8-6/25-error-include-args/error-include-args.php<?php
declare(strict_types=1);
set_error_handler(static function (int $level, string $message): bool { echo $message, PHP_EOL; return true;}, E_WARNING);
try { foreach (['0', '1'] as $enabled) { ini_set('error_include_args', $enabled); echo "Setting $enabled: "; unlink('php://memory'); }} finally { restore_error_handler();}Expected Result
Section titled “Expected Result”Setting 0: unlink(): PHP does not allow unlinkingSetting 1: unlink('php://memory'): PHP does not allow unlinkingWhy It Works
Section titled “Why It Works”The php://memory wrapper rejects the unlink operation, so PHP emits a warning without touching a disk file. The custom error handler prints the complete warning message. With the INI setting enabled, PHP inserts the supplied argument into the function signature. It uses the engine’s argument formatting, including truncation and sensitive-parameter redaction.
Exercise
Section titled “Exercise”Run the script again with php -d zend.exception_string_param_max_len=6. Check that the enabled warning shows 'php://...'.
Wrap-up
Section titled “Wrap-up”Continue with Reverse Text by Grapheme Cluster.