gish Posted November 11, 2008 Share Posted November 11, 2008 hi I lifted some code from a site and I am trying to understand it. Below is the code but have I got the theory right. $isMobile = strpos($ua, 'sony') !== false strpos is trying to find the word sony in the string $ua if it does $isMobile will return a 1. I have no idea "!== false" does??? Is this right and what does !== false do? gish Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/ Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 the !== false will determine if the function really returned false since 0 is a valid response from strpos as sony could be at the very start of the string. So this checks if it truly is false and not that sony was just at the very beginning. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687381 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 Yes it is write An example is <?php $string= strpos("monday it rained", 'rained');//missed out a bit. if($string == 1) { echo "there is one instance of the word rained"; } else { echo "the word doesn't exist in the string"; } ?> I have never seen !== false used before, it looks like only part of a statement you have. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687383 Share on other sites More sharing options...
.josh Posted November 11, 2008 Share Posted November 11, 2008 I have never seen !== false used before, it looks like only part of a statement you have. Sure you have. Or at least, you've seen the principle. $result = mysql_query($sql) or die(); Same principle. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687388 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 Yes it is write An example is <?php $string= strpos("monday it rained", 'rained'; if($string == 1) { echo "there is one instance of the word rained"; } else { echo "the word doesn't exist in the string"; } ?> I have never seen !== false used before, it looks like only part of a statement you have. Try it with this: <?php $string= strpos("rained it did on monday", 'rained'); if($string == 1) { echo "there is one instance of the word rained"; } else { echo "the word doesn't exist in the string"; } ?> Your code will return a "the word doesnt exist" now try it with this: <?php $string= strpos("rained it did on monday", 'rained'); if($string !== false) { echo "there is one instance of the word rained"; } else { echo "the word doesn't exist in the string"; } ?> That will return the correct result. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687393 Share on other sites More sharing options...
gish Posted November 11, 2008 Author Share Posted November 11, 2008 So if i was to code it the other way $isMobile = strpos($ua, 'sony') or die(); yes/no Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687395 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 That would kill the page if there was no instance of the word in the var. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687396 Share on other sites More sharing options...
.josh Posted November 11, 2008 Share Posted November 11, 2008 So if i was to code it the other way $isMobile = strpos($ua, 'sony') or die(); yes/no haha yah don't do that, I was just showing blade something. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687401 Share on other sites More sharing options...
gish Posted November 11, 2008 Author Share Posted November 11, 2008 no I don't want to kill the page I want the header to move to another part of the site. If a mobile phone hits the site. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687402 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 Sorry I thought the question was answered. Here is the answer. The reason the person uses the !== false is because the function may return 0 if sony is at the very beginning of the string as that is the starting position, since a string is really just an array of characters it is 0-index based. So no, it will not return a 1 if the word is in the string, it may return 0. By implementing the !== false you take away from the 0 as being a false instead it is true since that is where the sony position starts at in the string. If you know that sony will never be at the start of the string, that is fine to check it against 0. The point of strpos is to return the first position of the word. I hope that helps clear things up for you. So this $isMobile = strpos($ua, 'sony') or die(); May kill the page even though sony is in $ua, and yea you do not want to do that. $isMobile = strpos($ua, 'sony') !== false; Will return true, if the word is inside $ua and false if the word is not (or a 1 for true/0 for false). The above representation is like a short if statement it could be presented like so: <?php $isMobile = false; if (strpos($ua, 'sony') !== false) { $isMobile = true; } if ($isMobile) { echo 'This came from a mobile!'; }else { echo 'This came from a PC!!! EEEK!'; } ?> hi I lifted some code from a site and I am trying to understand it. Below is the code but have I got the theory right. $isMobile = strpos($ua, 'sony') !== false strpos is trying to find the word sony in the string $ua if it does $isMobile will return a 1. I have no idea "!== false" does??? Is this right and what does !== false do? gish Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687403 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 @crayon, thanks i was thinking it might be out of something like $var !== false ? something : something Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687408 Share on other sites More sharing options...
gish Posted November 11, 2008 Author Share Posted November 11, 2008 So I have this correct if $au has sony at the start of the string. $isMobile will say that the position of sony is 0 which means that if i create an if statement like the one below. Then I would never get the header to move. So !== false overrides the statement and then tells $isMobile that it does exist in $au. if ($isMobile >= 1) { $location='mobile/index.php'; header ('Location: '.$location); exit; } else { exit; } yes/no Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687417 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 Correct. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687418 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 Erm.. <?php $isMobile = strpos($au,"sony");//get the amount of occurrences of sony in the string(i believe) if ($isMobile >= 1) //if the amount of (sony's) in the string is 1 or more then. { $location='mobile/index.php'; header ('Location: '.$location);// redirect page to here exit; } else {//else exit the page exit; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687420 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 Erm.. <?php $isMobile = strpos($au,"sony");//get the amount of occurrences of sony in the string(i believe) if ($isMobile >= 1) //if the amount of (sony's) in the string is 1 or more then. { $location='mobile/index.php'; header ('Location: '.$location);// redirect page to here exit; } else {//else exit the page exit; } ?> Not to be mean or anything, but that is completely wrong =) http://us2.php.net/strpos strpos — Find position of first occurrence of a string Finds the first position, as in my lengthy post I described that a string is an array of Characters, and thus are 0-index based so sony at the very start of the word would return 0, thus rendering the above code as wrong/bad code. That is why they have the !== false Again I am not meaning to be rude, just enlightening you. =) Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687422 Share on other sites More sharing options...
DeanWhitehouse Posted November 11, 2008 Share Posted November 11, 2008 Ahh, i knew it was something like that, i haven't used it for a long time. Edit:Not completely wrong, only slightly Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687423 Share on other sites More sharing options...
gish Posted November 11, 2008 Author Share Posted November 11, 2008 I am not worried I am trying to learn. You need to learn by correction. Do you mean bad code by me I was using it as an example not the actual code I was going to use. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687429 Share on other sites More sharing options...
premiso Posted November 11, 2008 Share Posted November 11, 2008 No, I was talking with Blade =) Sorry I thought quoting him would show that. Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687430 Share on other sites More sharing options...
gish Posted November 11, 2008 Author Share Posted November 11, 2008 thanks for your help =) Quote Link to comment https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687452 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.