Skip to content

Stdlib: internal string function numeric coercion — strpos/str_contains vs strlen (ext/standard/string.c parity) #3549

Description

@PurHur

Category

stdlib / runtime

Problem

Zend coerces scalar operands to string for many internal string functions via zend_parse_parameters / _convert_to_string(). This compiler is inconsistent:

Call Zend PHP 8.x This compiler
strlen(123) 3 3
str_contains(123, '2') true LogicException
strpos(123, '2') 1 LogicException

Strict string-only checks in ext/standard/str_contains.php and strpos.php diverge from php-src.

php-src reference

  • ext/standard/string.cPHP_FUNCTION(str_contains), PHP_FUNCTION(strpos) parameter parsing with string coercion
  • Zend/zend_operators.c_convert_to_string(), convert_to_string() on zvals
  • ext/standard/basic_functions.c — shared parse_str helpers

Repro (today)

<?php
var_export(str_contains(123, '2'));
echo "\n";
var_export(strpos(123, '2'));
echo "\n";
echo strlen(123), "\n";
./script/docker-exec.sh -- bash -lc 'source script/php-env.sh && ./script/apply-patches.sh >/dev/null && php bin/vm.php repro.php'
php repro.php

Expected (Zend): true, 1, 3
This compiler: fatal on str_contains / strpos.

Scope (this repo)

Module Path
VM helpers ext/standard/VmString.php — shared coerceToString() used by string builtins
Stdlib ext/standard/str_contains.php, strpos.php, audit siblings (stripos, str_starts_with, …)
JIT mirror coercion in lib/JIT/Builtin/Standard/ stubs or defer with matrix note
Tests test/compliance/cases/stdlib/string_coerce_numeric.phpt

Done when

  • Repro matches Zend output on VM
  • Audit of PHP 8 string search/compare builtins uses same coercion policy as php-src (document exceptions like strcmp strictness)
  • ./script/ci-fast.sh --filter StringCoerce green
  • docs/capabilities.md notes coercion behavior where non-obvious

Links

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:vmVirtual machineenhancementNew feature or requestimplementation-readySpec complete: repro, php-src ref, done-when — safe for workers to claimphase-4:stdlibPhase 4 – stdlib for web apps

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions