Check if a string starts with a given substring.

Use strpos() to find the position of $needle in $haystack.

代码实现

function startsWith($haystack, $needle)
{
  return strpos($haystack, $needle) === 0;
}

使用样例

startsWith('Hi, this is me', 'Hi'); // true