<?php
if (defined('HHVM_VERSION')) {
exit;
}
// https://www.mail-archive.com/internals@lists.php.net/msg87079.html
function str_begins($a, $b) {
return substr_compare($a, $b, 0, strlen($b)) === 0;
}
function str_ends($a, $b) {
return substr_compare($a, $b, -strlen($b)) === 0;
}
// Let's test edge cases
$funcs = [
'str_begins',
'str_ends',
];
$tests = [
// array(array($main, $part), $expected)
array(array("foo", "foo"), true),
array(array("foo", ""), true),
array(array("", "foo"), false),
array(array("", ""), true),
];
foreach ($funcs as $func) {
foreach ($tests as list(list($main, $part), $expected)) {
echo $func . '(' . var_export($main, true) . ', ' . var_export($part, true) . '):' . PHP_EOL;
echo " - Expected: "; var_dump($expected);
echo " - Actual: "; var_dump($actual = $func($main, $part));
if ($actual !== $expected) {
echo " => DIFF!" . PHP_EOL;
}
echo PHP_EOL;
}
echo PHP_EOL;
}
- Output for 7.2.18 - 7.2.33, 7.3.5 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
- str_begins('foo', 'foo'):
- Expected: bool(true)
- Actual: bool(true)
str_begins('foo', ''):
- Expected: bool(true)
- Actual: bool(true)
str_begins('', 'foo'):
- Expected: bool(false)
- Actual: bool(false)
str_begins('', ''):
- Expected: bool(true)
- Actual: bool(true)
str_ends('foo', 'foo'):
- Expected: bool(true)
- Actual: bool(true)
str_ends('foo', ''):
- Expected: bool(true)
- Actual: bool(false)
=> DIFF!
str_ends('', 'foo'):
- Expected: bool(false)
- Actual: bool(false)
str_ends('', ''):
- Expected: bool(true)
- Actual: bool(true)
- Output for 8.3.5
- Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0
str_begins('foo', 'foo'):
- Expected: bool(true)
- Actual: bool(true)
str_begins('foo', ''):
- Expected: bool(true)
- Actual: bool(true)
str_begins('', 'foo'):
- Expected: bool(false)
- Actual: bool(false)
str_begins('', ''):
- Expected: bool(true)
- Actual: bool(true)
str_ends('foo', 'foo'):
- Expected: bool(true)
- Actual: bool(true)
str_ends('foo', ''):
- Expected: bool(true)
- Actual: bool(false)
=> DIFF!
str_ends('', 'foo'):
- Expected: bool(false)
- Actual: bool(false)
str_ends('', ''):
- Expected: bool(true)
- Actual: bool(true)
- Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.17, 7.3.0 - 7.3.4
- str_begins('foo', 'foo'):
- Expected: bool(true)
- Actual: bool(true)
str_begins('foo', ''):
- Expected: bool(true)
- Actual: bool(true)
str_begins('', 'foo'):
- Expected: bool(false)
- Actual:
Warning: substr_compare(): The start position cannot exceed initial string length in /in/AlBBh on line 9
bool(false)
str_begins('', ''):
- Expected: bool(true)
- Actual: bool(true)
str_ends('foo', 'foo'):
- Expected: bool(true)
- Actual: bool(true)
str_ends('foo', ''):
- Expected: bool(true)
- Actual: bool(false)
=> DIFF!
str_ends('', 'foo'):
- Expected: bool(false)
- Actual:
Warning: substr_compare(): The start position cannot exceed initial string length in /in/AlBBh on line 12
bool(false)
str_ends('', ''):
- Expected: bool(true)
- Actual:
Warning: substr_compare(): The start position cannot exceed initial string length in /in/AlBBh on line 12
bool(false)
=> DIFF!
preferences:
214.66 ms | 404 KiB | 291 Q