Skip to content

Fix PHP 8.x TypeError: cast AJAX numeric params to int before arithmetic#263

Open
TeleDark wants to merge 1 commit into
ajaxloadmore:mainfrom
TeleDark:fix/php8-type-casting-offset-calculation
Open

Fix PHP 8.x TypeError: cast AJAX numeric params to int before arithmetic#263
TeleDark wants to merge 1 commit into
ajaxloadmore:mainfrom
TeleDark:fix/php8-type-casting-offset-calculation

Conversation

@TeleDark

Copy link
Copy Markdown

On PHP 8.0+, arithmetic on strings throws a fatal TypeError. AJAX params from filter_input_array(INPUT_GET) are always strings, so performing math directly on them causes 'Unsupported operand types: string * string'.

Affected line:
$args['offset'] = $offset + ( $posts_per_page * $page );

Variables $offset, $posts_per_page, $page and $preloaded_amount were all read from $params without type casting.

Fix: cast all four variables to (int) at point of assignment.

Tested on PHP 8.4. No functional change on PHP 7.x.

On PHP 8.0+, arithmetic on strings throws a fatal TypeError. AJAX params
from filter_input_array(INPUT_GET) are always strings, so performing math
directly on them causes 'Unsupported operand types: string * string'.

Affected line:
  $args['offset'] = $offset + ( $posts_per_page * $page );

Variables $offset, $posts_per_page, $page and $preloaded_amount were all
read from $params without type casting.

Fix: cast all four variables to (int) at point of assignment.

Tested on PHP 8.4. No functional change on PHP 7.x.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant