Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Member amessinger's Avatar
    Join Date
    Apr 2005
    Location
    Kansas, USA
    Posts
    46
    Member #
    9682
    I'm mostly a front-end developer who dabbles in PHP as needed. I recently learned of a bug in a CMS plugin that I use, caused by the way the plugin author used strpos() to check for the presence of a string within a URI.

    According to the PHP manual strpos() may return a value of 0, FALSE, or "" (an empty string). The first two results I understand: a value of zero means that the substring was found at the beginning of the string, while a value of FALSE means the substring wasn't found at all. It's the "" result that has me scratching my head. Under what conditions does the function return an empty string, and why?

    Thanks in advance for your help.
    Zenscope Studio
    Quality Website Development and Graphic Design
    Specializing in Independently-Owned Businesses and Non-Profits
    www.zenscope.com

  2.  

  3. #2
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    AFAIK, 0, FALSE, and "" are all the same thing. You're supposed to use === 0 if you're checking if the substring is at the very first. Although I could, of course, be clouded by the dark side of the force.
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  4. #3
    Member amessinger's Avatar
    Join Date
    Apr 2005
    Location
    Kansas, USA
    Posts
    46
    Member #
    9682
    Oh, so the difference isn't actually in the value the function returns (which is how the manual puts it) but in how you test that value?

    That would make this is a mildly counter-intuitive side effect of PHP's dynamic typing. Like the Heisenberg uncertainty principle, but applied computer programming. Not sure why I didn't think of that... silly me, trying to read a technical manual literally.
    Zenscope Studio
    Quality Website Development and Graphic Design
    Specializing in Independently-Owned Businesses and Non-Profits
    www.zenscope.com

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    1 times
    Actually, it's a side-effect not of the dynamic typing, but of the type coercion that PHP does. Other dynamically typed languages aren't necessarily susceptible to this issue because "" and false are not equivalent in those.

    Anyway, I suspect [minicode]strpos[/minicode] cannot, in fact, return "", but the notice they included in there was a generic warning about type checking for distinguishing between return values, and was probably included as a template, so it mentions all the possible issues with using == instead of ===. i.e., whenever they have a function that may return two values that evaluate to false under == (or to true, for that matter), they include a templated warning about the dangers of using == there, even if the function may not return ALL the false-evaluating values.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 04:23 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com