What is the rationale behind the empty $needle string behavior with PHP 8 str_contains?

Created
Active
Viewed 230 times
8 replies

You must be a Member of the collective to start a discussion.

3
2657 votes
36 answers
6.6m views

How do I check if a string contains a specific word?

8029 votes
Accepted

Now with PHP 8 you can do this using str_contains: if (str_contains('How are you', 'are')) { echo 'true'; } Please note: The str_contains function will always return true if the $needle (the ...

View answer

I was looking at the answer to this question, and it was strange to me that the str_contains function for PHP 8 always returns true if the $needle parameter is an empty string. If the string being searched has any characters in it, it isn't an empty string, so I'm curious as to why would str_contains work this way?

Was there a technical reason behind this choice? Do other programming languages have "contains" functions that operate this way?

  • 4.2k
  • 13
  • 44
  • 54

8 replies

Sorted by:
77477015
6

I don't know the technical reason, or any other reason why this decision was made, but it does make sense to me. If a string is an ordered set of characters, then a substring is any subset of that set, and the empty set is a subset of any set.

77477644
1

I remember hearing one time that the implementation uses the strpos() function which always returns 0 for an empty needle. It uses zero to indicate a match on the first position of the string. So str_contains() is essentially just doing a return strpos($haystack, $needle) >= 0; which will always return true for an empty needle. Why does strpos() return 0 for an empty needle? Not sure if there is any other reasoning, but the start of a string is considered to be empty. This is also for consistency. The other string position functions will also return 0 on an empty needle.

77477678
-1

"Was there a technical reason behind this choice?" It's hard to say definitively without an explicit statement from the developers. But, one could speculate that this design decision was made to simplify the behavior of these functions and to avoid potential bugs. If an empty string were not considered to be present at every position in a string, it could lead to unexpected behavior in certain scenarios.

"Do other programming langauges have "contains" functions that operate this way?" Example in Python, the in keyword returns False when checking if an empty string is in another string. This is different from php's behavior, but it's just one example of how different languages can handle this situation.

In py; print("hello" in "") # Outputs: False

While in php; echo str_contains("", ""); // Outputs: 1 (which is equivalent to true)

This behavior is consistent with other string functions such as str_starts_with and str_ends_with, which also return true when the $needle parameter is an empty string.

77479287
4

In your python example you are looking for "hello" in empty string which is obviously false. Using print("" in "hello") outputs True

77480543
3

"Was there a technical reason behind this choice?"

Yes, an empty string is considered a substring of any string, including itself in the theory of formal languages and string processing.

From a formal perspective, if S is a string, then an empty string ε is a substring of S. This can be described as: for any string S, there are strings U and V (which can be empty) such that S = U + ε + V, where + denotes string concatenation.

"Do other programming langauges (sic) have "contains" functions that operate this way?"

Yes, see JavaScript's includes(), Python's in operator or str.contains(), Java's String.contains(), C#'s String.Contains(), etc.

77483512
0

That choice is natural. Most (if not all) languages work like that.

Related question: Why does "abcd".StartsWith("") return true?

77488269
0

Needle is what you're searching for. Logically speaking, there's an empty string between every two characters (or boundary) in a string. An empty string also kind of contains an empty string. So yeah, when needle is empty, you'll always get a true, in the same way that 'a' is in 'a' and 'a' is also in 'bac'.

77614664
0

It's logical. Yes, every other language (that I know of) handles it that way, and even Math itself does (an empty set is a subset of any other set).

Think about it: I can say a haystack string contains a needle string if I can show to you how I will assemble the haystack string using at least the needle string, plus other stuff.

For example:

// Does 'hello' contain 'l'?
// Yes:
'he' . 'l' . 'lo'
// or
'hel' . 'l' . 'o'
// So, 'hello' contains 'l' 2 times.

// Does 'hello' contain ''?
// Yes:
'' . 'hello'
// or
'h' . '' . 'ello'
// or
'he' . '' . 'llo'
// or
'hel' . '' . 'lo'
// or
'hell' . '' . 'o'
// or
'hello' . ''
// So, 'hello' contains '' 6 times.

// Does 'hello' contain 'x'?
// No, because I cannot come up with any way how to
// assemble 'hello' using 'x'!