if $_SERVER['REQUEST_URI'] already contains a ? then using & instead
$HL_lnk = strpos($_SERVER['REQUEST_URI'],'?')?'&':'?';
------
Often, we see two approaches for this.
Option 1
One option is to use the strpos PHP function. The following example shows how this is used:
<?php
$pos = strpos($haystack,$needle);
if($pos === false) {
// string needle NOT found in haystack
}
else {
// string needle found in haystack
}
?>
Watch out, checking for substring inside string in PHP can be tricky. Some people do just something like
if ($pos > 0) {}
or perhaps something like
if (strpos($haystack,$needle)) {
// We found a string inside string
}
and forget that the $needle string can be right at the beginning of the $haystack, at position zero. The
if (strpos($haystack,$needle)) { }
code does not produce the same result as the first set of code shown above and would fail if we were looking for example for "moth" in the word "mother".
Option 2
Another option is to use the strstr() function. A code like the following could be used:
if (strlen(strstr($haystack,$needle))>0) {
// Needle Found
}
Note, the strstr() function is case-sensitive. For a case-insensitive search, use the stristr() function.
Which is Better?
The strpos() function approach is a better way to find out whether a string contains substring PHP because it is much faster and less memory intensive.
Any Word of Caution?
If possible, it is a good idea to wrap both $haystack and $needle with the strtolower() function. This function converts your strings into lower case which helps to eliminate problems with case sensitivity.