Display_errors with Strict_types Gives 500 Error

How to fix a problem with strict_types causing internal server errors in PHP.

899 views
d

By. Jacob

Edited: 2020-01-05 02:28

There is a problem in PHP 7 when using display_errors in combination with strict_types which causes a 500 internal server error. The problem likely happens because you are using an integer when setting display_errors.

To correctly turn on errors, place the following at the top of your script, before writing any other code:

<?php declare(strict_types=1);

opcache_invalidate(__FILE__, true);
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(-1);

See also: PHP Data Types

Why you are getting 500 errors

The problem happens because you are using an integer instead of a string when attempting to turn on errors from within a PHP script.

The ini_set function expects a string value, probably because it is modifying configuration file values. Normally PHP will not use strict type checking, and that is why it still works with an integer.

The below does not work as you would expect:

declare(strict_types=1);

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

Links

  1. Custom Error Handling in PHP

Tell us what you think:

  1. In this Tutorial, it is shown how to redirect all HTTP requests to a index.php file using htaccess or Apache configuration files.
  2. How to create a router in PHP to handle different request types, paths, and request parameters.
  3. Tutorial on how to use proxy servers with cURL and PHP
  4. When using file_get_contents to perform HTTP requests, the server response headers is stored in a reserved variable after each successful request; we can iterate over this when we need to access individual response headers.
  5. How to effectively use variables within strings to insert bits of data where needed.

More in: PHP Tutorials