Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions ext/standard/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -1152,6 +1152,17 @@ ZEND_FRAMELESS_FUNCTION(implode, 2)
zend_string *str;
zval *pieces;

/* Like PHP_FUNCTION(implode): implode($array, null) is the same as implode($array) */
if (Z_TYPE_P(arg1) == IS_ARRAY) {
Z_FLF_PARAM_ARRAY_OR_NULL(2, pieces);
if (pieces) {
zend_argument_type_error(1, "must be of type string, array given");
} else {
php_implode(ZSTR_EMPTY_ALLOC(), Z_ARR_P(arg1), return_value);
}
return;
}

Z_FLF_PARAM_STR(1, str, str_tmp);
Z_FLF_PARAM_ARRAY_OR_NULL(2, pieces);

Expand Down
41 changes: 41 additions & 0 deletions ext/standard/tests/strings/implode_array_null_frameless.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--TEST--
implode() with an array first argument behaves the same in direct and dynamic calls
--FILE--
<?php
$array = ['a', 'b'];
$null = null;

function test(string $label, callable $direct, callable $dynamic) {
foreach (['direct' => $direct, 'dynamic' => $dynamic] as $kind => $fn) {
try {
$result = var_export($fn(), true);
} catch (TypeError $e) {
$result = $e->getMessage();
}
echo "$label ($kind): $result\n";
}
}

test('array, null', fn() => implode($array, $null), fn() => call_user_func('implode', $array, $null));
test('array, null literal', fn() => implode($array, null), fn() => call_user_func('implode', $array, null));
test('empty array, null', fn() => implode([], $null), fn() => call_user_func('implode', [], $null));
test('array, string', fn() => implode($array, ','), fn() => call_user_func('implode', $array, ','));
test('array, array', fn() => implode($array, $array), fn() => call_user_func('implode', $array, $array));
test('string, array', fn() => implode(',', $array), fn() => call_user_func('implode', ',', $array));
test('join', fn() => join($array, $null), fn() => call_user_func('join', $array, $null));
?>
--EXPECT--
array, null (direct): 'ab'
array, null (dynamic): 'ab'
array, null literal (direct): 'ab'
array, null literal (dynamic): 'ab'
empty array, null (direct): ''
empty array, null (dynamic): ''
array, string (direct): implode(): Argument #2 ($array) must be of type ?array, string given
array, string (dynamic): implode(): Argument #2 ($array) must be of type ?array, string given
array, array (direct): implode(): Argument #1 ($separator) must be of type string, array given
array, array (dynamic): implode(): Argument #1 ($separator) must be of type string, array given
string, array (direct): 'a,b'
string, array (dynamic): 'a,b'
join (direct): 'ab'
join (dynamic): 'ab'
Loading