Uncaught Error: Call to undefined function str_starts_with() Fix Solutions

if (!function_exists('str_starts_with')) {
  function str_starts_with($haystack, $needle, $case = true)
  {
    if ($case) {
      return strpos($haystack, $needle, 0) === 0;
    }
    return stripos($haystack, $needle, 0) === 0;
  }
}

Example with the empty string

<?php
if (str_starts_with('abc', '')) {
    echo "All strings start with the empty string"; // <-- this will show as output
}
?>

Example case-sensitivity

  • Case Sensitive is a case where uppercase and lowercase letters are interpreted differently.
  • Case Insensitive is a case where uppercase and lowercase letters are interpreted the same.
<?php
$string = 'The lazy fox jumped over the fence';

if (str_starts_with($string, 'The')) {
    echo "The string starts with 'The'\n"; // <-- this will show as output
}

if (str_starts_with($string, 'the')) {
    echo 'The string starts with "the"'; // this ignored because insensitive
} else {
    echo '"the" was not found because the case does not match'; // <-- this will show as output
}

?>

Uncaught Error: Call to undefined function str_ends_with() Fix Solutions

if (!function_exists('str_ends_with')) {
  function str_ends_with($haystack, $needle, $case = true)
  {
    $expectedPosition = strlen($haystack) - strlen($needle);
    if ($case) {
      return strrpos($haystack, $needle, 0) === $expectedPosition;
    }
    return strripos($haystack, $needle, 0) === $expectedPosition;
  }
}

PHP Thumbnail