1
0
mirror of https://github.com/php/php-src.git synced 2026-03-26 09:12:14 +01:00
Files
archived-php-src/ext/standard/tests/array/compact.phpt
dwgebler f5139878f9 Check parameters on compact() and throw warning if not string or array of strings (#6921)
compact() is documented (https://www.php.net/manual/en/function.compact) as a variadic function accepting parameters which are strings or arrays of strings referencing defined symbols.

In actuality, passing nonsense parameters e.g. compact(true, 42) merely returns an empty array. I propose throwing a warning in these cases, to prevent silent bugs.
2021-05-11 09:36:12 +02:00

37 lines
715 B
PHP

--TEST--
compact()
--FILE--
<?php
$çity = "San Francisco";
$state = "CA";
$event = "SIGGRAPH";
$location_vars = array("c\\u0327ity", "state");
$result = compact("event", $location_vars);
var_dump($result);
$result = compact(true);
$foo = 'bar';
$bar = 'baz';
$result = compact($foo, [42]);
var_dump($result);
?>
--EXPECTF--
Warning: compact(): Undefined variable $c\u0327ity in %s on line %d
array(2) {
["event"]=>
string(8) "SIGGRAPH"
["state"]=>
string(2) "CA"
}
Warning: compact(): Argument #1 must be string or array of strings, bool given in %s on line %d
Warning: compact(): Argument #2 must be string or array of strings, int given in %s on line %d
array(1) {
["bar"]=>
string(3) "baz"
}