Results 1 to 4 of 4

Thread: [RESOLVED] strpos being strange and/or annoying.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] strpos being strange and/or annoying.

    Was doing some example code for a friend and I came across something very strange.

    If I do:
    echo "<b>" . strpos($hayStack, $num1YXX) . "=" . $hayStack . "." . $num1YXX . ",</b>";
    strpos returns no value. (result is just "=000.0,").

    But if I go:
    Code:
    echo "<b>" . strpos("000", "0") . "=" . $hayStack . "." . $num1YXX . ",</b>";
    strpos returns: "0=000.0,".

    Basically, it's reading the variables as empty, but they are fine when they are echoed on the same line. Anyone have any insights into this strange behavior? There's no errors.

    Full code below.

    Code:
    <?php
    error_reporting( E_ALL );
    ini_set('display_errors', true); 
    $num1YXX=0;   //  }
    $num1XYX=0;   //   > I could have made this one array (And make able to count higher then 10 with more then 3 iterations), but did separate variables for simplicity.
    $num1XXY=0;   //  }
    
    $numberArray[]=""; //Store our awesome numbers in.
    $numberCounter=0; //Keep track of how many cycles we've done (This can be calculated by the num1XXX variables).
    $numPos1=false; // This is so once we've found that at least one instance of the array already exists, exit (or ignore) the rest of the loop.
    $numPos2=false;
    $numPos3=false;
    
    $alreadyExists=true; //Remember if it exists or not.
    
    for ($num1YXX=0; $num1YXX<=9; $num1YXX++)
    {
      for ($num1XYX=0; $num1XYX<=9; $num1XYX++)
      {
        for ($num1XXY=0; $num1XXY<=9; $num1XXY++) // 3 indented loops, one for each integer (They are concatinated in the end).
        {
    	  $alreadyExists=true;
    	  $numPos1=false;
    	  $numPos2=false;
    	  $numPos3=false;
    
    	  $result = $num1YXX . $num1XYX . $num1XXY;
    	  foreach ($numberArray as $arrayIndex) //Check array for existing entry loop.
    	  {
    	    $hayStack=$arrayIndex;
    	    $numPos1 = strpos($hayStack, $num1YXX);
    	    $numPos2 = strpos($hayStack, $num1XYX);
    	    $numPos3 = strpos($hayStack, $num1XXY);
    //echo "<b>" . $numPos1 . "=" . $hayStack . "." . $num1YXX . ",</b>";
    echo "<b>" . strpos($hayStack, $num1YXX) . "=" . $hayStack . "." . $num1YXX . ",</b>";
    echo "<b>" . strpos("000", "0") . "=" . $hayStack . "." . $num1YXX . ",</b>";
    		if ($numPos1===false && $numPos2===false && $numPos3===false)
    		{
    		  $alreadyExists=false;
    		}
    		
    	  }
    	  if ($alreadyExists==false)
    	  {
    		$numberArray[] = $result; //Add it in if it's not found.
    		echo $result . "<br />"; //Also echo to screen.
    	  }
    	  $numberCounter++;
        }
    	$numberCounter++;
      }
      $numberCounter++;
    }
    echo "<br /><br /> Total Unique: " . count($numberArray) . "<br />"; // Echo total amount
    //echo "<br /><br /> Total Cycles: " . $numberCounter . "<br />"; // Echo cycles

  2. #2
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: strpos being strange and/or annoying.

    RTFM.
    If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
    Your needle is an int, not a string. Example:
    PHP Code:
    $hayStack '000';
    $num1YXX 0;
    echo 
    strpos($hayStack$num1YXX) . "=" $hayStack "." $num1YXX ","
    //outputs: =000.0,

    $hayStack '000';
    $num1YXX '0';
    echo 
    strpos($hayStack$num1YXX) . "=" $hayStack "." $num1YXX ","
    //outputs: 0=000.0,

    $hayStack 'abc';
    $num1YXX 97;
    echo 
    strpos($hayStack$num1YXX) . "=" $hayStack "." $num1YXX ",";
    //outputs: 0=abc.97, 
    The third example shows how you'd intentionally use an int as the needle (97 is the ordinal value of a).

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: strpos being strange and/or annoying.

    Thanks! That's exactly what it was. I thought I was going a little crazy.

    Code:
    $numPos1 = strpos($hayStack . " ",  trim($num1YXX . " "));
    I added a blank space to the hayStack for strpos, and that seems to do the trick.

  4. #4
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: [RESOLVED] strpos being strange and/or annoying.

    I added a blank space to the hayStack for strpos, and that seems to do the trick.
    It works because you're implicitly casting it as a string; instead, you could just explicitly cast it:
    PHP Code:
    $numPos1 strpos((string)$hayStack,  (string)$num1YXX); 
    This makes the intention of your code a little more straightforward.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width