<tutorialjinni.com/>

startWith in PHP

Posted Under: PHP, Snippets, Tutorials on Aug 18, 2018
startWith in PHP
PHP snippet function to check whether a string startWith with a specified string or not. Function returns Boolean.This function can also handle case sensitivity. For endsWith() function click here.
function startsWith($haystack, $needle,$ignoreCase=false)
{
    if($ignoreCase){
        $haystack= strtolower($haystack);
        $needle= strtolower($needle);
    }
    return (substr($haystack, 0, strlen($needle)) === $needle);
}
A sample call to above function can look like
// Case Sensitive test.
var_dump(startsWith("Starts with function testing", "start"));
// Ignoring case
var_dump(startsWith("Starts with function testing", "start",true));

Output

/var/www/html/tutorials/index.php:10:boolean false
/var/www/html/tutorials/index.php:11:boolean true


imgae