Strict mode in PHP

Strict mode in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Strict Mode in PHP

Perl developers are familiar with the concept of strict variable declaration. When use strict; is active, you cannot simply reference a variable that hasn't been defined or declared beforehand. The interpreter throws an error immediately if you attempt to use an undeclared variable, forcing you to be explicit about your data structures. This prevents silent bugs where a typo in a variable name might accidentally create a new global variable instead of triggering a clear error.

PHP does not offer a direct, single-line equivalent to Perl's use strict; that activates automatically for the entire script. However, it provides mechanisms to achieve similar safety guarantees depending on your environment and version. The approach you take depends heavily on whether you are writing raw PHP code or using a modern framework like Laravel.

In standard PHP environments without an external engine, achieving strict mode requires explicit configuration via php.ini. You must set the error reporting level to include warnings for undefined variables. This is done by setting:

error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT
display_errors = Off
log_errors = On
report_memleaks = 1

However, this configuration alone often isn't enough because PHP's default behavior treats accessing an undeclared variable as a silent failure or a simple notice, rather than a hard stop. To enforce strictness at the runtime level without restarting the web server for every file, you can leverage the E_USER_ERROR directive or wrap your code in checks.

A more robust solution involves using PHP's built-in error handlers to treat undefined variable notices as fatal errors. You can register a custom error handler:

set_error_handler(function ($errno, $errstr, $errfile, $errline) {
    // Treat undefined variables and similar strict violations as errors
    if ($errno === E_NOTICE || $errno === E_WARNING && strpos($errstr, 'Undefined') !== false) {
        throw new ErrorException($errstr, 0, $errno);
    }
});

This approach forces the script to crash immediately upon encountering an undefined variable, mimicking the behavior of Perl's strict mode. It catches issues like $foo when $foo was never declared, or using a property on an object that doesn't exist without triggering a __get() method.

For developers working within the Laravel ecosystem, the concept of "strict mode" is handled differently due to the framework's architecture. Laravel relies on type hinting and explicit return types in modern versions (Laravel 8+). By enforcing strict typing in your services and controllers, you eliminate the ambiguity of undefined variables for object properties.

Furthermore, Laravel's php.ini configuration often includes settings that align with strict development practices. While you cannot toggle a global strict flag like in Perl, you can configure the application to throw exceptions on errors by setting display_errors = On and ensuring error_reporting includes E_ALL. The framework also encourages using type declarations:

public function index(): string
{
    // Laravel will help enforce structure, reducing reliance on implicit globals
}

The trade-off with these manual configurations is that they add friction to the development workflow. If you are debugging a legacy codebase, turning strict errors into fatal exceptions can make it harder to trace down to the root cause if you aren't prepared for the script to halt immediately. In such cases, using a tool like PHPStan or Psalm alongside runtime error handling provides a layered defense: static analysis catches potential issues before execution, while runtime handlers catch the ones that slip through.

Ultimately, PHP doesn't have a magic switch that turns on strict mode globally without configuration. You must consciously choose to treat undefined variables as fatal errors, either through php.ini adjustments or custom error handlers. This discipline mirrors the intent of Perl's strict mode and significantly reduces the class of bugs known as "magic variable" issues.

Our network