There are times I feel like my brain is turning to mush.
Seriously—how many times have I used these string functions in PHP and still forget the syntax every time? So I’ve decided to document it once and for all. Maybe writing it down will help it finally stick.
Let’s break it down: these are two common PHP functions used to search for a character or string within another string.
eregi()
– Case-insensitive pattern matching (deprecated)
Here’s the basic syntax:
php복사편집int eregi ( string $pattern , string $string [, array &$regs ] );
Example:
php복사편집echo eregi('A', "ABCDEFG");
Pretty straightforward, right?
- The first parameter is the character or pattern you’re looking for.
- The second is the string you’re searching in.
- The third is optional and related to pattern matching details—I don’t really use it, and honestly, I don’t fully understand it. If you’re curious, check the official PHP documentation.
What’s important:
- If the character exists, it returns
1
. - If not, it returns
0
. - Simple yes/no. No position tracking.
⚠️ Warning: eregi()
was deprecated as of PHP 5.3.0, so you probably shouldn’t use it anymore. But hey, it’s good to know what it was doing!
strpos()
– The smarter alternative
Now this is the modern, go-to function:
php복사편집mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] );
Example:
php복사편집echo strpos("ABCDEFG", "A");
This will return 0
, since “A” is the very first character in the string.
Now try this:
php복사편집echo strpos("ABCDEFG", "G"); // returns 6
echo strpos("ABCDEFG", "H"); // returns false
Here’s the key:
- If the character is found, it returns its position (starting at 0).
- If not found, it returns boolean false, not
-1
or some error code.
So far, so good, right?
But here’s where things get tricky
Let’s say you write this:
php복사편집if (strpos("ABCDEFG", "H") >= 0) {
echo "Found!";
}
At first glance, it seems reasonable.
- If found,
strpos()
returns 0 or higher. - If not, it returns
false
.
But this logic fails—because false >= 0
actually evaluates to true
in PHP!
Why? Because false
is loosely interpreted as 0
. So the if
statement thinks it’s a match when it’s not.
Let’s test a few basics:
php복사편집if (-1) echo "Whoa -1"; // ✅ runs
if ( 0) echo "Whoa 0"; // ❌ doesn't run
if ( 1) echo "Whoa 1"; // ✅ runs
if (true) echo "Whoa true"; // ✅ runs
if (false) echo "Whoa false"; // ❌ doesn't run
Conclusion: 0
and false
behave the same in if()
statements unless you’re being strict about it.
The right way: Use strict comparison!
Here’s what’s recommended:
php복사편집if (strpos("ABCDEFG", "H") !== false) {
echo "Yes! Found it.";
}
This line means:
- If
strpos()
returns anything except false (0
,1
,2
…), it’s considered true. - If it is false (meaning not found), the block won’t run.
The !==
operator checks both the value and the type—which is exactly what we want here.
Final thoughts
Even while writing this, I had to double-check a few things—because it’s confusing when 0
can mean either “found at first position” or “false” depending on the context.
But now I’ve finally made peace with it.
So next time you wonder why strpos()
isn’t working in your if()
statement, remember:
Always use
!== false
to check if a string contains something.
Happy coding!
Reference: