The SQL to PHP Translation Trap
You are looking for a direct equivalent to the SQL NOT LIKE operator, specifically the pattern %substring%, which checks whether a string does not contain a specific sequence of characters. In SQL, this is often written as WHERE column NOT LIKE '%word%'. If you try to port this syntax directly into PHP using something like $string NOT LIKE '%word%', your code will fail immediately because PHP's engine doesn't understand the LIKE keyword in that context.
The most straightforward way to replicate this logic in vanilla PHP is by leveraging the built-in strpos() function. This function searches for the first occurrence of a substring within a string and returns the position on success or false on failure. To invert this logicâchecking that the substring is absentâyou need a conditional check that evaluates to true only when strpos() returns false.
Here is the idiomatic PHP implementation:
$searchTerm = 'word';
$haystack = $string;
if (strpos($haystack, $searchTerm) === false) {
// The substring was NOT found. Proceed with your logic here.
echo "The string does not contain '$searchTerm'.";
}
Notice the strict comparison operator ===. While a loose equality check (==) might seem acceptable here because both false and an integer return type could be involved, using === prevents subtle issues if strpos() ever returns an integer position that evaluates loosely to false (though it technically won't in this specific context, the habit is good). More importantly, strpos() returns 0 if the substring is found at the very beginning of the string. If you used a loose != check and somehow confused 0 with "false", your logic would break. Using === false ensures that only the actual absence of the match triggers your block.
Handling Case Sensitivity
A common pitfall when porting SQL logic to PHP is assuming case sensitivity matches between databases and scripting languages by default. In many database engines, comparisons are case-insensitive depending on the collation settings. However, strpos() in PHP is strictly case-sensitive unless you explicitly change it.
If your application data contains mixed casing (e.g., "Word" vs "word"), a simple strpos() check might miss valid matches if your SQL query was forgiving of case differences. To mirror a standard SQL behavior that ignores case, you must wrap the search term and the target string in strtolower():
if (strpos(strtolower($haystack), strtolower($searchTerm)) === false) {
// Case-insensitive check: substring not found regardless of casing
}
This approach ensures consistency regardless of how the data was stored or entered, making your application less brittle against user input variations.
Modern Alternatives and Edge Cases
While strpos() is the native solution and works in every PHP version ever released, there are scenarios where you might prefer a different approach. The preg_match() function offers a regex-based alternative, which can be useful if you need to treat the substring as a whole word or apply complex pattern matching rules that SQL patterns handle awkwardly.
if (!preg_match('/\b' . preg_quote($searchTerm, '/') . '\b/', $haystack)) {
// Matches whole words only, case-insensitive by default with /i flag
}
However, regex introduces its own overhead and potential for syntax errors if the search term contains special characters like dots or asterisks. You must remember to escape the search term using preg_quote() before embedding it into a pattern. Unless you specifically need word boundaries or complex matching logic, sticking to strpos() is generally more performant and easier to debug.
If you are working within the Laravel framework, you don't need to reinvent this wheel. The Eloquent ORM allows you to express this "does not contain" logic directly in your queries using the notLike or whereNotIn methods, depending on your database driver's support for specific operators. For raw string checks within controllers or services, the strpos() pattern remains the most readable and efficient choice.
