maya8989 Posted March 19, 2023 Share Posted March 19, 2023 (edited) hi, this is my first positing here.. I've been wracking my brains with this for two days now.. it's something so simple and elemental, that I have done so many times, I can't understand why it's not working.. (I'm pre-PHP 8 ) $Host = $_SERVER['HTTP_HOST']; // evaluates to "localhost:8888" (MAMP localhost) $HostLength = strlen($Host); // evaluates to 14 // but this returns 0!! $isLocalhost = strpos($Host, 'localhost'); echo "$isLocalhost"; // prints "0" I use this to test whether user is on localhost on the server.. thus.. if ($isLocalhost) { <load this or that or the other>; } // if on localhost the condition is not met!! (and needless to say, I have so far tested this only on localhost..) I can't understand why this is not working.. would appreciate some help.. thank you very much.. Edited March 19, 2023 by maya8989 typo Quote Link to comment https://forums.phpfreaks.com/topic/316021-simple-string-operation-not-working/ Share on other sites More sharing options...
Solution Barand Posted March 19, 2023 Solution Share Posted March 19, 2023 Looking up strpos() in the manual would have explained exactly what your problem is, and the first example gives you the cure. Quote Link to comment https://forums.phpfreaks.com/topic/316021-simple-string-operation-not-working/#findComment-1606634 Share on other sites More sharing options...
maya8989 Posted March 19, 2023 Author Share Posted March 19, 2023 (edited) hi Brand, thank you for your response... wow.. that worked.. thank you... now the reason this surprises me is that I do this throughout the website (& another website) $atEmpl = strpos($URL, 'empl.php'); if ($atEmpl) { $bodyClass = "empl"; } and it's always worked.... so why does the above code work for detecting a substring in the current URL and not for detecting a substring in the localhost var ($_SERVER['HTTP_HOST'], which is also a string..) (so should I change the above code too? to if ($atEmpl !== false) { ?) thank you for your help..... Edited March 19, 2023 by maya8989 Quote Link to comment https://forums.phpfreaks.com/topic/316021-simple-string-operation-not-working/#findComment-1606635 Share on other sites More sharing options...
Barand Posted March 19, 2023 Share Posted March 19, 2023 41 minutes ago, maya8989 said: so why does the above code work for detecting a substring in the current URL and not for detecting a substring in the localhost var ($_SERVER['HTTP_HOST'], which is also a string..) Beacause in this case, the "needle" is at position 0 evaluates to "localhost:8888" ^ pos 0 Quote Link to comment https://forums.phpfreaks.com/topic/316021-simple-string-operation-not-working/#findComment-1606636 Share on other sites More sharing options...
requinix Posted March 20, 2023 Share Posted March 20, 2023 https://www.php.net/manual/en/function.str-contains.php Quote Link to comment https://forums.phpfreaks.com/topic/316021-simple-string-operation-not-working/#findComment-1606640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.