How to Set Custom PHP error_reporting Level via Command Line Parameters for Syntax Checking


4 views

When performing PHP syntax validation through command line (php -l), developers often need to override the default error reporting settings from php.ini. This becomes particularly important when:

  • Testing legacy code with different error level requirements
  • Enforcing stricter standards during CI/CD pipelines
  • Debugging specific error types during development

The -d parameter allows runtime configuration overrides:

php -d error_reporting=E_ALL -l filename.php

For our find command example:

find ./ -type f -name \\*.php -exec php -d error_reporting=E_ALL -l '{}' \\; | grep -v "No syntax errors detected"
Constant Value Description
E_ALL 32767 All errors and warnings
E_ERROR 1 Fatal run-time errors
E_WARNING 2 Run-time warnings
E_PARSE 4 Compile-time parse errors
E_NOTICE 8 Run-time notices

Combine multiple error levels using bitwise operators:

php -d error_reporting="E_ALL & ~E_NOTICE" -l file.php

For Docker-based environments:

docker run --rm php:alpine php -d error_reporting=E_ALL -l /path/to/file.php

1. Strict mode for new projects:

find src/ -name "*.php" -exec php -d error_reporting="E_ALL|E_STRICT" -l {} \\;

2. Legacy code compatibility check:

php -d error_reporting="E_ALL & ~E_DEPRECATED & ~E_STRICT" -l old_code.php

When performing automated PHP syntax checks using command-line tools, developers often need to override the default error_reporting settings from php.ini. The standard approach with php -l only checks for syntax errors without applying custom error levels.

PHP's command-line interface provides the -d flag to set ini values temporarily:

find ./ -type f -name \*.php -exec php -d error_reporting=E_ALL -l '{}' \; | grep -v "No syntax errors detected"

This sets error_reporting to E_ALL just for the current execution. You can combine multiple settings:

-d error_reporting=E_ALL -d display_errors=1

Use these constants in your command:

E_ALL & E_STRICT       // All errors and strict standards
E_ALL                  // All errors except strict standards
E_ERROR | E_WARNING    // Only fatal errors and warnings
E_PARSE                // Just syntax parsing errors

For a comprehensive check including deprecated notices:

find ./ -type f -name \*.php -exec php -d error_reporting='E_ALL|E_STRICT' -l '{}' \; | grep -v "No syntax"

For complex scenarios, create a wrapper script (check.php):

<?php
ini_set('error_reporting', E_ALL);
foreach (glob('*.php') as $file) {
    system("php -l " . escapeshellarg($file));
}

Then execute with:

php check.php | grep -B1 -A3 "error"

When scanning large codebases, consider these optimizations:

# Use parallel processing with xargs
find . -name '*.php' -print0 | xargs -0 -P8 -n1 php -d error_reporting=E_ALL -l