3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php // 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 = array( 'str_begins', 'str_ends', ); $tests = array( // 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 $test) { list($args, $expected) = $test; list($main, $part) = $args; echo $func . '("' . $main . '", "' . $part . '"):' . PHP_EOL; echo " - Expected: "; var_dump($expected); echo " - Actual: "; var_dump($func($main, $part)); echo PHP_EOL; } echo PHP_EOL; }
Output for 5.6.0 - 5.6.30, 7.0.0 - 7.3.0rc3
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/SFFKh on line 5 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) str_ends("", "foo"): - Expected: bool(false) - Actual: Warning: substr_compare(): The start position cannot exceed initial string length in /in/SFFKh on line 8 bool(false) str_ends("", ""): - Expected: bool(true) - Actual: Warning: substr_compare(): The start position cannot exceed initial string length in /in/SFFKh on line 8 bool(false)
Output for hhvm-3.15.4
str_begins("foo", "foo"): - Expected: bool(true) - Actual: bool(true) str_begins("foo", ""): - Expected: bool(true) - Actual: Warning: The length must be greater than zero in /in/SFFKh on line 5 bool(false) str_begins("", "foo"): - Expected: bool(false) - Actual: Warning: The start position cannot exceed initial string length in /in/SFFKh on line 5 bool(false) str_begins("", ""): - Expected: bool(true) - Actual: Warning: The length must be greater than zero in /in/SFFKh on line 5 bool(false) str_ends("foo", "foo"): - Expected: bool(true) - Actual: bool(true) str_ends("foo", ""): - Expected: bool(true) - Actual: bool(false) str_ends("", "foo"): - Expected: bool(false) - Actual: Warning: The start position cannot exceed initial string length in /in/SFFKh on line 8 bool(false) str_ends("", ""): - Expected: bool(true) - Actual: Warning: The start position cannot exceed initial string length in /in/SFFKh on line 8 bool(false)