Does $i = 0 equal nothing?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DavidPr
    New Member
    • Mar 2007
    • 155

    Does $i = 0 equal nothing?

    In $i = 0, is 0 the same as nothing or does it count as 1?

    In a form I have these two fields - file and file_new. Only one (hopefully) should contain a value. I have to determine which one has a value to determine which function to execute.

    [PHP]$file = mysql_real_esca pe_string($_POS T['file']);
    $file_new = mysql_real_esca pe_string($_POS T['file_new']);

    $i = 0;
    if ($file > $i AND $file_new < $i)
    {
    function file();
    }
    else
    {
    funtion file_new();
    }[/PHP]

    I guess I could just leave off the second part of the if statement, unless this would help me to make sure that only one variable contains a value. If file_new also contained a value I could return an error message saying that only one of these fields can be filled out on the form.

    Will this work or no?

    OR is this better: [PHP]
    if ($file != '' AND $file_new == '') {
    run this code
    }

    if ($file == '' AND $file_new != '') {
    run this code
    }

    if ($file != '' AND $file_new != '') {
    run this code
    }

    [/PHP]

    Thanks.
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    #2
    Originally posted by DavidPr
    In $i = 0, is 0 the same as nothing or does it count as 1?

    In a form I have these two fields - file and file_new. Only one (hopefully) should contain a value. I have to determine which one has a value to determine which function to execute.

    [PHP]$file = mysql_real_esca pe_string($_POS T['file']);
    $file_new = mysql_real_esca pe_string($_POS T['file_new']);

    $i = 0;
    if ($file > $i AND $file_new < $i)
    {
    function file();
    }
    else
    {
    funtion file_new();
    }[/PHP]

    I guess I could just leave off the second part of the if statement, unless this would help me to make sure that only one variable contains a value. If file_new also contained a value I could return an error message saying that only one of these fields can be filled out on the form.

    Will this work or no?

    OR is this better: [PHP]
    if ($file != '' AND $file_new == '') {
    run this code
    }

    if ($file == '' AND $file_new != '') {
    run this code
    }

    if ($file != '' AND $file_new != '') {
    run this code
    }

    [/PHP]

    Thanks.
    what does an echo () return give you.

    Comment

    • pbmods
      Recognized Expert Expert
      • Apr 2007
      • 5821

      #3
      Heya, DavidPr.

      Try using empty.

      $i = 0 evaluates to 0, which == false. However:

      [code=php]
      $bool = ($i = 0);
      var_dump($bool) ; // int(0)
      var_dump($bool == false); // boolean(true)
      var_dump($bool === false); // boolean(false)
      var_dump($bool == ''); // boolean(true)
      var_dump($bool === ''); // boolean(false)
      [/code]

      Comment

      • moishy
        New Member
        • Oct 2006
        • 104

        #4
        why don't you try if, elseif

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          The way I would go about this a different way. First I would make a function to determine if a field was empty or not:

          [code=php]
          function hasvalue($var)
          {
          if(isset($var))
          {
          if(trim($var) != '') return true;
          }
          return false;
          }
          [/code]

          Then I would do your tests on the value returning function:

          [code=php]
          if(hasvalue($fi le) xor hasvalue($file_ new))
          {
          if(hasvalue($fi le))
          {
          //Docrap
          }
          else
          {
          //Doothercrap
          }
          }
          else
          {
          echo "Please give a value for one and only one field";
          }
          [/code]

          This will allow you to adapt and change the hasvalue function later down the road, if your data types change, or if you want to perform some data validation in the function as well (highly recommended).

          The other value of writing your code like this is that you KNOW how the function is going to react in later versions of PHP (as well as previous), and it is entirely obvious what the hell you are doing.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by DavidPr
            In $i = 0, is 0 the same as nothing or does it count as 1?

            ....
            Eh .

            Comment

            • DavidPr
              New Member
              • Mar 2007
              • 155

              #7
              I didn't know if whether or not the "0" in $i = 0 would be considered a value or the same as empty or ''.

              I ended up with this:
              [PHP]if(!empty($file ) && empty($file_new )) {

              // Do this

              } elseif(!empty($ file_new) && empty($file)) {

              // Do this other thing

              } else {

              echo "Error - fill in just one field";

              }[/PHP]

              But I'm going to copy down what Motoma suggested and try it. I'll also read about "empty" as pbmods suggests.

              When they come out with a new version of PHP, is it not backwards compatible?

              Thanks you all for the help. I have more questions and will post them in another thread.

              David

              Comment

              • Motoma
                Recognized Expert Specialist
                • Jan 2007
                • 3236

                #8
                Originally posted by DavidPr
                When they come out with a new version of PHP, is it not backwards compatible?
                They try to be as much as is safe HOWEVER:
                Things like number evaluating to booleans and nulls being equivalent to unset and non-set variables have no defined behavior. This means that even though it may act a certain way, it is neither intended nor unintended for it to act that way. Using such hacks detracts from readability and reliability, and I would strongly suggest against relying on undocumented behavior.

                Comment

                • DavidPr
                  New Member
                  • Mar 2007
                  • 155

                  #9
                  I didn't think about the security issues, but it makes sense... unfortunately.

                  Comment

                  • pbmods
                    Recognized Expert Expert
                    • Apr 2007
                    • 5821

                    #10
                    Heya, David.

                    You may also find this page useful:

                    Comment

                    • Motoma
                      Recognized Expert Specialist
                      • Jan 2007
                      • 3236

                      #11
                      Originally posted by DavidPr
                      I didn't think about the security issues, but it makes sense... unfortunately.
                      Well, security wasn't exactly the point I was trying to make.

                      To restate the point I intended to assert:
                      Just because it works one way, does not mean it is supposed to work that way.

                      In your case, you were trying to determine if the value of a string was larger than the value of an integer. In this situation, the greater than operator is not strictly defined by the language with regards to what it evaluates to.

                      The fact that it evaluates to anything may be a mere coincidence caused by the internal state of the PHP interpreter at the moment it was executed, or it may be due to implicit type casting beforehand. The problem is: we don't know.

                      If you wrote your code expecting the greater than operator to handle the situation a particular way, and the reason it does is because of casting, you are all set as long as string comparisons do not change. However, if the result of the operator is in fact due to coincidence, you cannot rely on that behavior between different executions of the script, between different versions of PHP, or between different computers or operating systems.

                      I hope this clarifies the point I was trying to make.
                      Good luck with your code, and come back if you have any other questions.

                      Comment

                      • pbmods
                        Recognized Expert Expert
                        • Apr 2007
                        • 5821

                        #12
                        Are we having fun yet?

                        To expand on Motoma's point, what if one day PHP became truly Object-Oriented and supported operator overloading?

                        Code is PHP/Java/C hybrid

                        [code=php]
                        String.prototyp e['>'] = function($rhs) {
                        if($rhs->constructor != String)
                        return $this->length > String($rhs)->length;
                        return $this->length > $rhs->length;
                        }
                        [/code]

                        Now, if $i = '', $i > 0 will actually return false because (''->length ! > '0'->length).

                        Comment

                        • r035198x
                          MVP
                          • Sep 2006
                          • 13225

                          #13
                          Originally posted by pbmods
                          Are we having fun yet?

                          To expand on Motoma's point, what if one day PHP became truly Object-Oriented .....
                          They should ...

                          Comment

                          • Motoma
                            Recognized Expert Specialist
                            • Jan 2007
                            • 3236

                            #14
                            Originally posted by r035198x
                            They should ...
                            There is a PECL extension that allows one to perform operator overloading.

                            Comment

                            Working...