Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php
communitycreator

What is str_starts_with in PHP?

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The str_starts_with function in PHP is used to check if a string starts with a certain string.

Note: This function is only available in PHP 8 onwards.

Syntax

str_starts_with(string $haystack, string $needle): bool

Parameters

  • haystack: This is the string where we want to search.
  • needle: This is the string we will search for.

Return value

The function returns true if the given string begins with the given needle. Otherwise, it returns false.

Code

In this example, we will check if our string begins with Hello:

<?php
var_dump(str_starts_with('Hello, Educative!', 'Hello'));

Expected output

bool(true)

RELATED TAGS

php
communitycreator

CONTRIBUTOR

Dario Vincenzo Tarantini

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring