php / php/php-src

Too big `string` to `int` cast results nonsense

Open
#19,118 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Status: Needs Triage
Dominant language
C
Stars
40.4k
Forks
8.1k
Avg merge
2d 13h
Merged PRs (30d)
96

Description

Description

The following code:

<?php
if ( (string) PHP_INT_MAX === '9223372036854775808' ) {
    echo "string identical" . PHP_EOL;
}

if ( PHP_INT_MAX === (int) '9223372036854775808' ) {
    echo "int identical" . PHP_EOL;
}

Resulted in this output:

int identical

But I expected this output instead:

Deprecated: Conversion from string '9223372036854775808' to int loses precision

A very similar error already exists when converting float to int implicitly e.g. $a[ PHP_INT_MAX + 1 ] = '';

See also https://github.com/php/php-src/issues/17081 which reports that for float/int casts

PHP Version
PHP 8.4.8
Operating System

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the string-to-int cast behavior on PHP 8.4.8, then trace the PHP interpreter's string-to-integer conversion path. Compare it with the existing float-to-int precision-loss handling referenced in the issue. Done means the oversized string conversion reports the expected deprecation instead of silently producing PHP_INT_MAX.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, php
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.