Check If String Starts With Another String

This function checks if a given string starts with another string. It also uses strpos but checks specifically if the substring’s position is 0, meaning it’s at the beginning of the string. This function is useful for checking if a string follows a certain format or pattern that starts with a specific sequence of characters.

PHP Function:

function startsWith($str, $substr) { return strpos($str, $substr) === 0; }

// Example
echo startsWith('Hello, World!', 'Hello'); // true
echo startsWith('Hello, World!', 'World'); // false

Member since January 2, 2019

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.


    Your email address will not be published. Required fields are marked *