• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Check if String Starts with Given String in php with str_starts_with()

Check if String Starts with Given String in php with str_starts_with()

March 18, 2022 Leave a Comment

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.

Other Articles You'll Also Like:

  • 1.  php Convert Degrees to Radians with php deg2rad() Function
  • 2.  php array_key_exists() Function – Check if Key Exists in Array
  • 3.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 4.  php Convert Radians to Degrees with php rad2deg() Function
  • 5.  Add Months to Date in php
  • 6.  PHP Variables Are Preceded by $
  • 7.  php tanh – Find Hyperbolic Tangent of Number Using tanh() Function
  • 8.  php is_numeric – Check if Variable is a Number
  • 9.  Get Last Element of Array in php
  • 10.  php is_float() Function – Check if Variable is Float in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy