Why WordPress 5.5.3 with PHP 8 shows 404 on every page?

WordPress 5.5.3 produces a 404 error when trying to enable just released PHP 8. Why?

Why WordPress 5.5.3 with PHP 8 shows 404 on every page?


WordPress in the currently latest version 5.5.3 produces a 404 error when trying to enable just released PHP 8. Why?

Officially WordPress will only be compatible with PHP 8 from version 5.6, which is scheduled for December 8, 2020. RC core version of WordPress 5.6 works correctly with PHP 8, the problem has been fixed. However, it is interesting to understand what is the source of the problem.

First let’s try to answer the question, what is the value of the expression?

'' < 0

(empty string less than zero)?

The first answer that comes to mind is false. The empty line is zero, the comparison is 0 < 0, the result is false.

This was always the case in PHP, and before PHP 8, the result of the expression is false. But not in PHP 8. Here the result of the expression is true, and there is an official statement on it. When comparing with a numeric string, a comparison of numbers is used. But an empty string is not a numeric string, and PHP 8 uses string comparison: '' < '0', and the result is true.

What does this have to do with WordPress?

The kernel has a method that is called at each request: \WP_Query::parse_query. It contains the lines

if ( ! is_scalar( $qv['p'] ) || $qv['p'] < 0 ) {
 $qv['p'] = 0;
 $qv['error'] = '404';
  ...

In most cases $qv['p'] contains an empty line, which causes if to trigger and set the 404 error.

In WordPress 5.6 the comparison line looks like this:

if ( ! is_scalar( $qv['p'] ) || (int) $qv['p'] < 0 ) {

This fixes the problem in PHP 8 and works correctly in all versions of PHP.

Summary: You should not use implicit type conversion, especially when developing code that should work under PHP 8.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.