Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. Linux, Apache, MySQL, PHP
  4. Help with this expression

Help with this expression

Scheduled Pinned Locked Moved Linux, Apache, MySQL, PHP
helpquestionlearning
13 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C cjoki

    It returns false because you are comparing a string literal to a number. If your string was if("0"==0) it should work. You may want to try to write the statement like this.

    <?php
    $test_array = array(1,"a",3,"b",5,"c",7,"d","e",0,"f","g","h","i","j","k");
    $i = 0
    foreach($test_array as $x)
    {
    if($x==0)
    {
    echo "found You at index ".$i."!<br>";
    exit;
    }
    else
    {
    echo "still looking<br>";
    }
    }
    $i++;
    ?>

    This should help you...

    X Offline
    X Offline
    xx77abs
    wrote on last edited by
    #3

    I've tried that, and it returned: still looking found You at index 1! So PHP thought that "a" was equal to 0 ... again, string is equal to zero, like in my first post ... what's going on ? is it maybe just error in my php installation ?? Right now, I'm very confused :D BTW, I've just tried that script on some other web server, result is the same ... is this kind of bug in PHP or what :D ? Thanks

    C 1 Reply Last reply
    0
    • X xx77abs

      Hello ! I have very simple expression in if statement: "d"==0 So in my code it looks like this: if ("d"==0) { //do something } Of course, booth sides aren't always like that, they are variables, but I saw that I had bug somewhere and it turned out that error occurs always when on one side is string and on other zero ... Can anyone explain to me why does this return true ?? I can't see why string would be equal to zero ... Thanks :)

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      xx77abs wrote:

      Can anyone explain to me

      Read the manual. http://www.php.net/manual/en/language.types.php[^]

      C W 2 Replies Last reply
      0
      • X xx77abs

        I've tried that, and it returned: still looking found You at index 1! So PHP thought that "a" was equal to 0 ... again, string is equal to zero, like in my first post ... what's going on ? is it maybe just error in my php installation ?? Right now, I'm very confused :D BTW, I've just tried that script on some other web server, result is the same ... is this kind of bug in PHP or what :D ? Thanks

        C Offline
        C Offline
        cjoki
        wrote on last edited by
        #5

        Sorry made a mistake in the code. This is what it should look like. See if you can spot the change. Understanding the difference between this code and the first one I posted will answer your question. You can then test it further by moving the 0 (zero) to a different location within the array.

        <?php
        $test_array = array(1,"a",3,"b",5,"c",7,"d","e",0,"f","g","h","i","j","k");
        $i = 0;
        foreach($test_array as $x)
        {
        if($x==0)
        {
        echo "found You at index ".$i."!<br>";
        exit;
        }
        else
        {
        echo "still looking<br>";
        }
        $i++;
        }
        ?>

        X 1 Reply Last reply
        0
        • L Lost User

          xx77abs wrote:

          Can anyone explain to me

          Read the manual. http://www.php.net/manual/en/language.types.php[^]

          C Offline
          C Offline
          cjoki
          wrote on last edited by
          #6

          Sometimes people learn more and better by example or a quick question, no need to discourage him.

          X 1 Reply Last reply
          0
          • C cjoki

            Sometimes people learn more and better by example or a quick question, no need to discourage him.

            X Offline
            X Offline
            xx77abs
            wrote on last edited by
            #7

            Yeah, I know a little about that, and I know that operator === will work correctly as it will check for type and content of variable, but I just wanted one-line explanation ;)

            1 Reply Last reply
            0
            • C cjoki

              Sorry made a mistake in the code. This is what it should look like. See if you can spot the change. Understanding the difference between this code and the first one I posted will answer your question. You can then test it further by moving the 0 (zero) to a different location within the array.

              <?php
              $test_array = array(1,"a",3,"b",5,"c",7,"d","e",0,"f","g","h","i","j","k");
              $i = 0;
              foreach($test_array as $x)
              {
              if($x==0)
              {
              echo "found You at index ".$i."!<br>";
              exit;
              }
              else
              {
              echo "still looking<br>";
              }
              $i++;
              }
              ?>

              X Offline
              X Offline
              xx77abs
              wrote on last edited by
              #8

              Thank you for your help, but if you mean that error was this line: $i++; outside of foreach loop, I've already fixed it before trying the sample and it still don't work ... http://x.xx77abs.com/example.php[^] As you can see, it tells me that if found zero at index 1, and you can see source code that was executed (it is yours :) ) ;)

              C 1 Reply Last reply
              0
              • X xx77abs

                Thank you for your help, but if you mean that error was this line: $i++; outside of foreach loop, I've already fixed it before trying the sample and it still don't work ... http://x.xx77abs.com/example.php[^] As you can see, it tells me that if found zero at index 1, and you can see source code that was executed (it is yours :) ) ;)

                C Offline
                C Offline
                cjoki
                wrote on last edited by
                #9

                I made a change to the code and got it to give the expected results.

                foreach($test\_array as $x)
                { 
                	echo $x."<br>";
                     if($x==0 && is\_numeric($x))
                

                The echo of $x shows it is (without the is_numeric function) stopping on "a", not what I would expect. Running the code with the is_numeric gives the correct result of index 9. I also tested === with the same results as the is_numeric. Without spending more time researching this I can only assume that php is parsing "a" as being equal to 0. I suspect this maybe due to something the parser is doing to allow the testing of a string and an integer. OK, I just did a little research and found this in the PHP manual...

                String conversion to numbers
                When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

                If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer . In all other cases it will be evaluated as a float .

                The value is given by the initial portion of the string . If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

                For more information on this conversion, see the Unix manual page for strtod(3).

                A quick google on strtod shows me this...

                Return Value
                On success, the function returns the converted floating point number as a double value.
                If no valid conversion could be performed, a zero value (0.0) is returned.
                If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE.
                If the correct value would cause underflow, zero is returned and errno is set to ERANGE.

                This looks to be the issue...but this is just a guess.

                C X 2 Replies Last reply
                0
                • C cjoki

                  I made a change to the code and got it to give the expected results.

                  foreach($test\_array as $x)
                  { 
                  	echo $x."<br>";
                       if($x==0 && is\_numeric($x))
                  

                  The echo of $x shows it is (without the is_numeric function) stopping on "a", not what I would expect. Running the code with the is_numeric gives the correct result of index 9. I also tested === with the same results as the is_numeric. Without spending more time researching this I can only assume that php is parsing "a" as being equal to 0. I suspect this maybe due to something the parser is doing to allow the testing of a string and an integer. OK, I just did a little research and found this in the PHP manual...

                  String conversion to numbers
                  When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

                  If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer . In all other cases it will be evaluated as a float .

                  The value is given by the initial portion of the string . If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

                  For more information on this conversion, see the Unix manual page for strtod(3).

                  A quick google on strtod shows me this...

                  Return Value
                  On success, the function returns the converted floating point number as a double value.
                  If no valid conversion could be performed, a zero value (0.0) is returned.
                  If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE.
                  If the correct value would cause underflow, zero is returned and errno is set to ERANGE.

                  This looks to be the issue...but this is just a guess.

                  C Offline
                  C Offline
                  cjoki
                  wrote on last edited by
                  #10

                  and php does force a conversion of strings when they appear in a comparisons

                  If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. These rules also apply to the switch statement. The type conversion does not take place when the comparison is === or !== as this involves comparing the type as well as the value.

                  source: http://www.php.net/manual/en/language.operators.comparison.php[^] I also noted that on this page that speaks to comparisons they have the following sample code

                  <?php
                  var_dump(0 == "a"); // 0 == 0 -> true
                  var_dump("1" == "01"); // 1 == 1 -> true
                  var_dump("10" == "1e1"); // 10 == 10 -> true
                  var_dump(100 == "1e2"); // 100 == 100 -> true

                  switch ("a") {
                  case 0:
                  echo "0";
                  break;
                  case "a": // never reached because "a" is already matched with 0
                  echo "a";
                  break;
                  }
                  ?>

                  So even the manual supports the "a" == 0, but I can not find out exactly why it does this. The questions I now have is does the function used in the conversion return a zero as a result or is this an error from the function which also returns a zero?

                  1 Reply Last reply
                  0
                  • C cjoki

                    I made a change to the code and got it to give the expected results.

                    foreach($test\_array as $x)
                    { 
                    	echo $x."<br>";
                         if($x==0 && is\_numeric($x))
                    

                    The echo of $x shows it is (without the is_numeric function) stopping on "a", not what I would expect. Running the code with the is_numeric gives the correct result of index 9. I also tested === with the same results as the is_numeric. Without spending more time researching this I can only assume that php is parsing "a" as being equal to 0. I suspect this maybe due to something the parser is doing to allow the testing of a string and an integer. OK, I just did a little research and found this in the PHP manual...

                    String conversion to numbers
                    When a string is evaluated in a numeric context, the resulting value and type are determined as follows.

                    If the string does not contain any of the characters '.', 'e', or 'E' and the numeric value fits into integer type limits (as defined by PHP_INT_MAX), the string will be evaluated as an integer . In all other cases it will be evaluated as a float .

                    The value is given by the initial portion of the string . If the string starts with valid numeric data, this will be the value used. Otherwise, the value will be 0 (zero). Valid numeric data is an optional sign, followed by one or more digits (optionally containing a decimal point), followed by an optional exponent. The exponent is an 'e' or 'E' followed by one or more digits.

                    For more information on this conversion, see the Unix manual page for strtod(3).

                    A quick google on strtod shows me this...

                    Return Value
                    On success, the function returns the converted floating point number as a double value.
                    If no valid conversion could be performed, a zero value (0.0) is returned.
                    If the correct value is out of the range of representable values, a positive or negative HUGE_VAL is returned, and the global variable errno is set to ERANGE.
                    If the correct value would cause underflow, zero is returned and errno is set to ERANGE.

                    This looks to be the issue...but this is just a guess.

                    X Offline
                    X Offline
                    xx77abs
                    wrote on last edited by
                    #11

                    So that's it :) Thanks for explaining ;)

                    1 Reply Last reply
                    0
                    • L Lost User

                      xx77abs wrote:

                      Can anyone explain to me

                      Read the manual. http://www.php.net/manual/en/language.types.php[^]

                      W Offline
                      W Offline
                      whatrevolution
                      wrote on last edited by
                      #12

                      <Pretentious> Raid tha manyuhl. :E <Pretentious> Aw raid eh own mah meaxbile. :E

                      Honestly Illustrated

                      1 Reply Last reply
                      0
                      • X xx77abs

                        Hello ! I have very simple expression in if statement: "d"==0 So in my code it looks like this: if ("d"==0) { //do something } Of course, booth sides aren't always like that, they are variables, but I saw that I had bug somewhere and it turned out that error occurs always when on one side is string and on other zero ... Can anyone explain to me why does this return true ?? I can't see why string would be equal to zero ... Thanks :)

                        W Offline
                        W Offline
                        whatrevolution
                        wrote on last edited by
                        #13

                        This happens because strings, not just "a", are type cast to integer type when compared against an integer, and the integer that they are equivalent to is zero, which equals zero. :) [Honestly Illustrated](http://honestlyillustrated.com)

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups