Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687381
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687383
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687393
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687403
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687417
Share on other sites

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;
}

?>

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687420
Share on other sites

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. =)

Link to comment
https://forums.phpfreaks.com/topic/132232-solved-strpos/#findComment-687422
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.