Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2007
    Location
    Canada
    Posts
    49
    Thanks
    1
    Thanked 1 Time in 1 Post

    strpos() troubles

    Minor problem with strpos()... If $value is present at the very start of my string $row->mfg_types, the strpos() fails to match. I worked around this by adding the underscore as the first character but what am I doing incorrectly? I understand that it is returning a zero (string matched starting at character 0) which is also boolean false.


    PHP Code:
    $checkboxes explode('|'$MFG_TYPES);
    echo 
    "\n<tr><td>Mfg. Types:</td><td>";
    foreach (
    $checkboxes as $value) {
        echo 
    "<input type=\"checkbox\" value=\"$value\" name=\"mfg[]\"";
        if (!
    strpos('_'.$row->mfg_types$value) === false) {
            
    //  the '_' was added at front because the function wouldn't
            //  pickup if the $value started as the first character of the string
            
    echo " checked=\"on\"";
        }
        echo 
    ">$value<br />\n";
    }
    echo 
    "</td></tr>\n"
    Of all the things I've lost in my life time, my mind is the one I miss the most!

  • #2
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    You are using !strpos(....) === false when you should be using strpos(...) !== false. When strpos() returns 0 as a "found" value, you are effectively saying !0 or !false. Obviously that means !false = true. The final comparison ends up being !false === false or true === false, which is always false. Remove the ! in fornt of strpos(...) and change === to !==

    That should help.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello


  •  

    Posting Permissions

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