Introduction
Both functions are used to find the first occurrence of a substring inside another string. However, the main difference between them is how they handle letter casing (uppercase and lowercase).
- strstr() → Case-sensitive search
- stristr() → Case-insensitive search
This means strstr() treats uppercase and lowercase letters differently, while stristr() ignores the case of letters.
Understanding the difference between strstr() and stristr() in PHP is important when working with user input, searching data, or validating text.
What is strstr() in PHP?
The strstr() function is used to find the first occurrence of a string inside another string.
It returns the portion of the string starting from the first match to the end of the string.
The search performed by strstr() is case-sensitive, meaning uppercase and lowercase letters must match exactly.
Syntax of strstr()
strstr(string $string, string $search, bool $before_search = false)
Parameters
string
The main string where the search will be performed.
search
The substring that needs to be searched.
before_search (optional)
If set to true, the function returns the part of the string before the match.
Return Value
- Returns the portion of the string starting from the match.
- Returns false if the substring is not found.
Example of strstr()
$text = "Welcome to PHP Programming";
$result = strstr($text, "PHP");
echo $result;
Output
Explanation
- The function finds the word PHP.
- It returns the remaining part of the string starting from that word.
Example with before_search Parameter
$text = "Welcome to PHP Programming";
$result = strstr($text, "PHP", true);
echo $result;
Output
Explanation
When the third parameter is true, the function returns the portion before the matched string.
Case Sensitivity in strstr()
Since strstr() is case-sensitive, the search must match the exact case.
$text = "Welcome to PHP Programming";
$result = strstr($text, "php");
var_dump($result);
Output
Explanation
The function does not find php because the original string contains PHP in uppercase.
What is stristr() in PHP?
The stristr() function works similarly to strstr(), but it performs a case-insensitive search.
This means uppercase and lowercase letters are treated the same.
For example:
- PHP
- php
- Php
- pHp
All will be considered identical when using stristr().
Syntax of stristr()
stristr(string $string, string $search, bool $before_search = false)
Parameters
The parameters of stristr() are exactly the same as strstr().
Return Value
- Returns the portion of the string starting from the match.
- Returns false if the substring is not found.
Example of stristr()
$text = "Welcome to PHP Programming";
$result = stristr($text, "php");
echo $result;
Output
Explanation
Even though the search term is php in lowercase, the function still finds PHP because stristr() ignores case.
Example Using before_search
$text = "Welcome to PHP Programming";
$result = stristr($text, "php", true);
echo $result;
Output
Difference Between strstr() and stristr()
Both functions perform similar tasks, but the key difference is case sensitivity.
| Feature | strstr() | stristr() |
|---|---|---|
| Search Type | Case-sensitive | Case-insensitive |
| Uppercase/Lowercase | Must match exactly | Ignored |
| Function Purpose | Find substring | Find substring |
| Return Value | Part of string from match | Part of string from match |
Example Comparing Both Functions
$text = "Learning PHP Programming";
echo strstr($text, "php");
echo "
";
echo stristr($text, "php");
Output
Explanation:
- strstr() fails because the case does not match.
- stristr() succeeds because it ignores case.
Real-Life Example
Suppose you want to check whether a user entered “php” in a search field.
User input may be:
- PHP
- php
- Php
- pHp
Using stristr() ensures the search still works.
Example:
$text = "This course teaches PHP programming.";
if(stristr($text, "php")){
echo "Keyword found!";
}
Output
This is useful when handling user input, search features, or filtering text.
When to Use strstr()
Use strstr() when:
- Exact case matching is required
- You want strict string comparison
- You are validating fixed-format data
Example:
- Password validation
- Product codes
- Case-sensitive identifiers
When to Use stristr()
- Exact case matching is required
- You want strict string comparison
- You are validating fixed-format data
Use stristr() when:
- Case does not matter
- Handling user input
- Implementing search functionality
Example:
- Website search feature
- Keyword detection
- Text filtering
Conclusion
The strstr() and stristr() functions in PHP are used to search for a substring within another string and return the portion of the string starting from the matched text. While both functions work similarly, the main difference lies in case sensitivity.
The strstr() function performs a case-sensitive search, meaning the uppercase and lowercase letters must match exactly. On the other hand, the stristr() function performs a case-insensitive search, allowing matches regardless of letter casing.
strstr() vs stristr() in php – Objective Questions (MCQs)
Q1. Which function performs a case-insensitive search?
Q2. Which function is case-sensitive?
Q3. What do both strstr() and stristr() return?
Q4. If no match is found, what do both functions return?
Q5. Which function should be used to find a word regardless of case?