0 vs false

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • esingley@gmail.com

    0 vs false

    So, this was driving me insane today, and I'm sure there's an easy
    answer that I'm just missing.

    Say I have an expression where 0 is acceptable as a TRUE response. But
    how, in PHP, do I say that 0 = true since it treats 0 = FALSE = "".
    For example, if I look for "foo" in a string using strpos:

    if (strpos("foo bar", "foo")) {
    echo "foo found at the beginning of the phrase.";
    }

    This should evaluate to TRUE, but it doesn't since it finds "food" at
    position 0, which then evaluates to FALSE.

    I tried casting, isset, is_null, and a few other siller options. I
    eventually worked around it by using substr_count, but mostly I'm just
    curious about the general answer to the question.

    Thanks.

  • Jerry Stuckle

    #2
    Re: 0 vs false

    esingley@gmail. com wrote:[color=blue]
    > So, this was driving me insane today, and I'm sure there's an easy
    > answer that I'm just missing.
    >
    > Say I have an expression where 0 is acceptable as a TRUE response. But
    > how, in PHP, do I say that 0 = true since it treats 0 = FALSE = "".
    > For example, if I look for "foo" in a string using strpos:
    >
    > if (strpos("foo bar", "foo")) {
    > echo "foo found at the beginning of the phrase.";
    > }
    >
    > This should evaluate to TRUE, but it doesn't since it finds "food" at
    > position 0, which then evaluates to FALSE.
    >
    > I tried casting, isset, is_null, and a few other siller options. I
    > eventually worked around it by using substr_count, but mostly I'm just
    > curious about the general answer to the question.
    >
    > Thanks.
    >[/color]

    if (strpos("foo bar", "foo") !== false) {
    echo "foo found";
    }

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • Geoff Berrow

      #3
      Re: 0 vs false

      Message-ID: <1145846522.785 908.162290@u72g 2000cwu.googleg roups.com> from
      esingley@gmail. com contained the following:
      [color=blue]
      >
      >Say I have an expression where 0 is acceptable as a TRUE response. But
      >how, in PHP, do I say that 0 = true since it treats 0 = FALSE = "".[/color]

      You'll be embarrassed to learn that there is a section on is in the
      manual. ;-)

      Find the position of the first occurrence of a substring in a string


      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • esingley@gmail.com

        #4
        Re: 0 vs false

        Argh, a RTFM would have saved me an hour. Thanks, though! That, of
        course, solved my problem right away.

        Comment

        • esingley@gmail.com

          #5
          Re: 0 vs false

          Argh, a RTFM would have saved me an hour. Thanks, though! That, of
          course, solved my problem right away.

          Comment

          Working...