Category
stdlib · runtime
Summary
array_rand() argument #2 ($num) must accept numeric strings (e.g. "1") with Zend int coercion. VM/JIT require native TYPE_INTEGER and throw LogicException.
Problem
Second parameter $num is not coerced from numeric strings before validation, so valid Zend calls fail at runtime.
php-src reference
Repro
HARNESS_DOCKER_RUN_OPTS='--memory=8g --cpus=2' ./script/docker-exec.sh -- bash -lc '
source script/php-env.sh
php -r "var_dump(array_rand([\"a\",\"b\"], \"1\"));"
php bin/vm.php -r "var_dump(array_rand([\"a\",\"b\"], \"1\"));"
'
| Mode |
array_rand(['a','b'], '1') |
| Zend |
int(0) or int(1) (valid key) |
| VM |
LogicException — argument #2 must be an integer |
Scope (this repo)
| Area |
Files |
| VM |
ext/standard/array_rand.php, ext/standard/VmArray.php (arrayRandPacked()) |
| JIT |
ext/standard/JitArrayRand.php |
| Tests |
test/compliance/cases/stdlib/array_rand_num_numeric_string.phpt |
Done when
Implementation notes
- Use shared int coercion helper (see closed numeric-string issues in
ext/standard/).
- Reuse
test/repro-maintainer/array_rand_validation.php where applicable.
Links
Category
stdlib·runtimeSummary
array_rand()argument #2 ($num) must accept numeric strings (e.g."1") with Zend int coercion. VM/JIT require nativeTYPE_INTEGERand throwLogicException.Problem
Second parameter
$numis not coerced from numeric strings before validation, so valid Zend calls fail at runtime.php-src reference
ext/standard/array.c—PHP_FUNCTION(array_rand)(zend_parse_parameterslong / numeric string)Repro
array_rand(['a','b'], '1')int(0)orint(1)(valid key)LogicException— argument #2 must be an integerScope (this repo)
ext/standard/array_rand.php,ext/standard/VmArray.php(arrayRandPacked())ext/standard/JitArrayRand.phptest/compliance/cases/stdlib/array_rand_num_numeric_string.phptDone when
TypeError/ValueErrorper php-srcarray_chunk/array_slicenumeric-string issuesImplementation notes
ext/standard/).test/repro-maintainer/array_rand_validation.phpwhere applicable.Links