Programming Tips - PHP: startsWith() and endsWith()

Date: 2018sep27 Language: php Q. PHP: startsWith() and endsWith() A.
function startsWith(string $haystack, string $needle): bool { $length = strlen($needle); if ($length == 0) return true; return substr($haystack, 0, $length) === $needle; } // Alternate function startsWith(string $haystack, string $needle): bool { return $needle === '' || strpos($haystack, $needle) === 0; } function endsWith(string $haystack, string $needle): bool { $length = strlen($needle); if ($length == 0) return true; return substr($haystack, -$length) === $needle; }