php / php/php-src

Use better error message for non-numeric strings in implicit contexts

Open
#20,632 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

When a "non-numeric" string is coerced to an integer or float, PHP raises an error rather than proceeding with the implicit cast. This is a good thing.

However, the error message implies that any string would be invalid, rather than that the content of the string is the problem.

Additionally, a common cause in my experience is that the string is empty, for instance, missing from some input data. It would be helpful for debugging to highlight when this is the case.

Proposal

When coercion to a numeric, float, or int context fails:

  • If the string being coerced is empty, use "empty string" as a pseudo-type in the error message
  • Otherwise, use "non-numeric string" as a pseudo-type in the error message

Current Behaviour

Mathematical operators:

echo 1 + "1";
// 2
echo 1 + "hello";
// TypeError: Unsupported operand types: int + string
echo 1 + "";
// TypeError: Unsupported operand types: int + string

Function calls in coercive-call mode:

declare(strict_types=0);

function foo(int $a) { echo $a; }

foo("1");
// 1
foo("a");
// TypeError: foo(): Argument #1 ($a) must be of type int, string given
foo("");
// TypeError: foo(): Argument #1 ($a) must be of type int, string given

Proposed behaviour

Mathematical operators:

echo 1 + "1";
// 2
echo 1 + "hello";
// TypeError: Unsupported operand types: int + non-numeric string
echo 1 + "";
// TypeError: Unsupported operand types: int + empty string

Function calls in coercive-call mode:

declare(strict_types=0);

function foo(int $a) { echo $a; }

foo("1");
// 1
foo("a");
// TypeError: foo(): Argument #1 ($a) must be of type int, non-numeric string given
foo("");
// TypeError: foo(): Argument #1 ($a) must be of type int, empty string given

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

The issue does not identify files or tests; start by tracing the numeric coercion and coercive-call argument type-error paths in php-src, then locate their existing coverage. Done means errors distinguish empty strings from other non-numeric strings in both mathematical operators and coercive function calls.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.