To check if a string starts with a particular string in php, for users of php 8.0.0 or later, the easiest way is to use the php str_starts_with() function.

$variable = "I'm a variable";

echo var_dump(str_starts_with($variable, "I'm"));
echo var_dump(str_starts_with($variable, "I'm a"));
echo var_dump(str_starts_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

If you are using a version of php lower than 8.0.0, you can define your own str_starts_with() function.

function starts_with( $haystack, $needle ) {
     return substr( $haystack, 0, strlen( $needle ) ) === $needle;
}

$variable = "I'm a variable";

echo var_dump(starts_with($variable, "I'm"));
echo var_dump(starts_with($variable, "I'm a"));
echo var_dump(starts_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

When working with string variables in our php programs, it is useful to be able to easily extract information about the values of the strings. In php, we can check if a string begins with another string easily.

To check if a string begins with a given string, we can use the str_starts_with() function.

The str_starts_with() function takes two parameters, the string you want to search (the haystack), and the string you want to search for (the needle), and determines if the string you want to search for is at the start of the string you are searching.

Below is a simple example showing how we can use str_starts_with() to check if a string starts with another string.

$variable = "I'm a variable";

echo var_dump(str_starts_with($variable, "I'm"));
echo var_dump(str_starts_with($variable, "I'm a"));
echo var_dump(str_starts_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

Check if String Starts with Specified String in php before Version 8.0.0

If you are using a version of php that is less than 8.0.0, then you can’t use the built in str_starts_with() function as this was introduced in version 8.0.0.

Instead, you can easily define your own function which checks to see if the string starts with another string using the substr() function.

We will check to see if the substring of length needle from the beginning is equal to the value of the needle.

Below is the function you can use which is equivalent to str_starts_with() in php version 8.0.0 and some examples.

function starts_with( $haystack, $needle ) {
     return substr( $haystack, 0, strlen( $needle ) ) === $needle;
}

$variable = "I'm a variable";

echo var_dump(starts_with($variable, "I'm"));
echo var_dump(starts_with($variable, "I'm a"));
echo var_dump(starts_with($variable, "variable"));

//Output:
bool(true)
bool(true)
bool(false)

Checking if a String Ends with a Specified String in php with str_ends_with()

If you’d instead like to check if a string ends with a specified string, you can use the str_ends_with() function in a similar way as the str_starts_with() function.

The str_ends_with() function will check to see if the specified string is at the end of a string.

Below are a few examples of how to use str_ends_with() to check if a string is at the end of another string.

$variable = "I'm a variable";

echo var_dump(str_ends_with($variable, "I'm"));
echo var_dump(str_ends_with($variable, "a variable"));
echo var_dump(str_ends_with($variable, "variable"));

//Output:
bool(false)
bool(true)
bool(true)

Hopefully this article has been useful for you to learn how to use the php str_starts_with() function to check if a string starts with a given string in php.

Categorized in:

PHP,

Last Update: February 26, 2024