Jump to content

simple string operation not working..


maya8989
Go to solution Solved by Barand,

Recommended Posts

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 by maya8989
typo
Link to comment
Share on other sites

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 by maya8989
Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.