
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
str_starts_with and str_ends_with function in PHP 8
str_starts_with and str_ends_with function are added in PHP 8 to check if a given string starts or ends with another string or not. If it starts and ends with another string, it returns true, otherwise false.
Example
str_starts_with('hello haystack', 'hello'); //starts string found 'True' str_starts_with('hello haystack', 'stack'); //ends string found 'True'
str_starts_with('hello haystack', 'hay'); //starts string found 'False' str_starts_with('hello haystack', 'hay'); //ends string found 'False'
str_starts_with() function in PHP 8
This function checks if a given string starts with the string needle. It returns true if the first string is found, otherwise false.
str_starts_with(string $haystack, string $needle): bool
Example : using str_starts_with() function.
<?php if (str_starts_with('hellohaystack', "hello")) { echo "string starts with hello"; } ?>
Output
String starts with 'hello'
Note: If the given first start string is not found in the second string, it returns false.
str_ends_with() function in PHP 8
This function checks if a given string ends with the string needle. It returns true if the given string ends found, otherwise false.
str_ends_with(string $haystack, string $needle):bool
Example : using str_ends_with() function
<?php if (str_ends_with('hellohaystack', "stack")) { echo "string ends with stack"; } ?>
Output
String ends with 'stack'
- Related Articles
- str() vs repr() in Python?
- What does the str() function do in Python Object Oriented Programming?
- Explain str() vs repr() functions in Python
- How can I concatenate str and int objects in Python?
- How to check the data frame structure without using str function in R?
- Count substrings that starts with character X and ends with character Y in C++
- Find if a string starts and ends with another given string in C++
- How to save the str output as a string in R?
- Python - Check whether a string starts and ends with the same character or not
- Write a program in C++ to count the Number of substrings that starts with ‘1’ and ends with ‘1’
- Program to build DFA that starts and ends with ‘a’ from the input (a, b)
- Check if a string starts with given word in PHP
- Check if a string ends with given word in PHP
- Python: Cannot understand why the error - cannot concatenate 'str' and 'int' object ?
- In function INSERT(str, Pos, len, newstr), what would be the result if ‘Pos’ is not within the length of the string?

Advertisements