davidannis Posted January 26, 2014 Share Posted January 26, 2014 I want to use a different configuration file if I am on my test server, so I was testing to see if the URL begins with test but it is not working. I have done this before but I must be doing something differently because I expect strpos('test', $_SERVER['HTTP_HOST']) to return a 0 but it is returning a bool false. I have tested it with the following code: echo $_SERVER['HTTP_HOST'].'<br>'; $x = strpos('test', $_SERVER['HTTP_HOST']); var_dump($x); die(); which gives me the following output: test.ezvaluation.com bool(false) Why? Quote Link to comment https://forums.phpfreaks.com/topic/285694-strpos-not-functioning-as-expected/ Share on other sites More sharing options...
Solution davidannis Posted January 26, 2014 Author Solution Share Posted January 26, 2014 Never mind, flipped needle and haystack Quote Link to comment https://forums.phpfreaks.com/topic/285694-strpos-not-functioning-as-expected/#findComment-1466645 Share on other sites More sharing options...
kicken Posted January 26, 2014 Share Posted January 26, 2014 strpos: mixed strpos ( string $haystack , mixed $needle [, int $offset = 0 ] ) You're calling it with the arguments in the wrong order. You're checking to see if the string 'test.ezvaluation.com' exists within the string 'test'. Quote Link to comment https://forums.phpfreaks.com/topic/285694-strpos-not-functioning-as-expected/#findComment-1466646 Share on other sites More sharing options...
.josh Posted January 27, 2014 Share Posted January 27, 2014 also note to make sure to do a strict comparison when using strpos. $foo = "bar"; if (strpos("bar",$foo)) { // found? } else { // not found? } In this example, the condition should be true, but strpos returns the position of the found needle. Since it is at the beginning, it will return 0. Since 0 is a falsey type, the condition will evaluate false, even though it was found. So you need to do something like this: $foo = "bar"; if (strpos("bar",$foo)!==false) { // <-- !== not != for strict comparison // found! } else { // not found! } Quote Link to comment https://forums.phpfreaks.com/topic/285694-strpos-not-functioning-as-expected/#findComment-1466672 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.