documentation help needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • William Gill

    documentation help needed

    I have used and tested the following:

    $x = myFunction($som eValue)? "true":"fal se";

    When myFunction() returns an empty array, it results in false, and when
    it returns a non-empty array it results in true. This is what I want
    and expect, but I can't find any documentation on it. Being naturally
    cautious (spelled paranoid) I don't want to rely on this until I'm
    certain. Can anyone point me to the documentation that supports what
    I'm seeing?
  • Erwin Moller

    #2
    Re: documentation help needed

    William Gill schreef:
    I have used and tested the following:
    >
    $x = myFunction($som eValue)? "true":"fal se";
    >
    When myFunction() returns an empty array, it results in false, and when
    it returns a non-empty array it results in true. This is what I want
    and expect, but I can't find any documentation on it. Being naturally
    cautious (spelled paranoid) I don't want to rely on this until I'm
    certain. Can anyone point me to the documentation that supports what
    I'm seeing?
    Hi,

    Why would an empty array evaluate to false?
    I don't even know if it does, nor do I care.
    Do you want your scripts to rely on such effects?
    I wouldn't.

    Why not simply write cleanly what you want?

    // assuming myFunction always returns an array
    $xTemp = myFunction($som eValue);
    $x = (count($xTemp) == 0) ? "false" : "true";

    Or in case you need true/false as booleans and not as strings:
    $xTemp = myFunction($som eValue);
    $x = (count($xTemp) 0);

    That way somebody else reading your code has also a clue what you are
    trying to accomplish.
    If you write it in your way, the next person seeing your code will ask
    the same questions you are asking yourself now.
    That next person could even be yourself in a few years. :P


    Regards,
    Erwin Moller (clean-code-police)

    Comment

    • Guillaume

      #3
      Re: documentation help needed

      Erwin Moller a écrit :
      // assuming myFunction always returns an array
      $xTemp = myFunction($som eValue);
      $x = (count($xTemp) == 0) ? "false" : "true";
      You may check empty() which directly returns a boolean ^^

      Regards,
      --
      Guillaume

      Comment

      • William Gill

        #4
        Re: documentation help needed

        Erwin Moller wrote:
        >
        Why would an empty array evaluate to false?
        I don't even know if it does, nor do I care.
        It's been a couple of weeks since i tested, but that's what I found.
        Do you want your scripts to rely on such effects?
        I wouldn't.
        >
        Why not simply write cleanly what you want?
        >
        // assuming myFunction always returns an array
        $xTemp = myFunction($som eValue);
        $x = (count($xTemp) == 0) ? "false" : "true";
        >
        Or in case you need true/false as booleans and not as strings:
        $xTemp = myFunction($som eValue);
        $x = (count($xTemp) 0);
        >
        That way somebody else reading your code has also a clue what you are
        trying to accomplish.
        If you write it in your way, the next person seeing your code will ask
        the same questions you are asking yourself now.
        That next person could even be yourself in a few years. :P
        Actually I am emulating someone else's code. My example was rewritten
        to focus on the true/false value of the assignment operation. In
        practice it reads more like:

        if($errors=chec kForErrors(){
        /* there are errors, do something about it */
        else{
        /* no errors so do something else*/
        }

        }

        Comment

        • William Gill

          #5
          Re: documentation help needed

          Guillaume wrote:
          You may check empty() which directly returns a boolean
          That insures a reliable true/false test.

          It also helps with a previous concern regarding the $_POST array.

          Thanks.

          Comment

          • William Gill

            #6
            Re: documentation help needed

            Erwin Moller wrote:
            Why would an empty array evaluate to false?
            I don't even know if it does, nor do I care.
            I retested and both an empty array and an assignment of an empty array
            return false.

            As stated elsewhere, the code I'm emulating uses the true/false result
            of the assignment to control flow, but as Guillaume points out, empty()
            is more explicit (and probably more reliable).

            Comment

            • Michael Fesser

              #7
              Re: documentation help needed

              ..oO(Erwin Moller)
              >William Gill schreef:
              >I have used and tested the following:
              >>
              >$x = myFunction($som eValue)? "true":"fal se";
              >>
              >When myFunction() returns an empty array, it results in false, and when
              >it returns a non-empty array it results in true. This is what I want
              >and expect, but I can't find any documentation on it. Being naturally
              >cautious (spelled paranoid) I don't want to rely on this until I'm
              >certain. Can anyone point me to the documentation that supports what
              >I'm seeing?
              >
              >Why would an empty array evaluate to false?
              >I don't even know if it does, nor do I care.
              It does by definition, see PHP's type juggling. All these values
              evaluate to FALSE if casted to boolean:

              0, '0', array(), '', NULL
              >Do you want your scripts to rely on such effects?
              >I wouldn't.
              I wouldn't either usually, but for another reason. An explicit test is
              cleaner and more readable code.

              Micha

              Comment

              • William Gill

                #8
                Re: documentation help needed

                Michael Fesser wrote:
                >
                It does by definition, see PHP's type juggling. All these values
                evaluate to FALSE if casted to boolean:
                >
                0, '0', array(), '', NULL
                Thanks, that's the type of documentation I was looking for.
                >
                >Do you want your scripts to rely on such effects?
                >I wouldn't.
                >
                I wouldn't either usually, but for another reason. An explicit test is
                cleaner and more readable code.
                Agreed. Using an explicit call to empty() is clearer, but sometimes
                using a multi step to feed a conditional may be counter productive.

                function setClass($field name){
                global $form_errors;
                if(isset($form_ errors)) {
                return array_key_exist s('$fieldname', $form_errors) ? 'error' :
                'normal'; /*set css class */
                }
                else{return 'normal';}
                }
                ....
                ....
                ....

                <input name="First_Nam e" type="text"
                class="<?php echo setClass('First _Name') ?>"
                value="<?php echo getVal('First_N ame') ?>" size="20">




                Comment

                • William Gill

                  #9
                  Re: documentation help needed

                  William Gill wrote:
                  >
                  function setClass($field name){
                  global $form_errors;
                  if(isset($form_ errors)) {
                  return array_key_exist s('$fieldname', $form_errors) ? 'error' :
                  'normal'; /*set css class */
                  }
                  else{return 'normal';}
                  }
                  ...
                  ...
                  ...
                  >
                  <input name="First_Nam e" type="text"
                  class="<?php echo setClass('First _Name') ?>"
                  value="<?php echo getVal('First_N ame') ?>" size="20">
                  >
                  Disregard my example, it is completely off base (I was distracted / had
                  my head up my... well, you know what I mean).

                  Comment

                  • Gordon

                    #10
                    Re: documentation help needed

                    On May 26, 2:50 pm, William Gill <nore...@exampl e.comwrote:
                    I have used and tested the following:
                    >
                    $x = myFunction($som eValue)? "true":"fal se";
                    >
                    When myFunction() returns an empty array, it results in false, and when
                    it returns a non-empty array it results in true.  This is what I want
                    and expect, but I can't find any documentation on it.  Being naturally
                    cautious (spelled paranoid) I don't want to rely on this until I'm
                    certain.  Can anyone point me to the documentation that supports what
                    I'm seeing?
                    That's very odd behavior you're getting there, I was under the
                    impression that empty arrays evaluated to true! For that reason, I'd
                    consider being a bit more thorough in checking what you get back from
                    your function to make sure it really is what you think it is. I could
                    be mistaken on that front of course, I'd have to check the
                    documentation on what evaluates to true and what doesn't, but
                    personally I'd be running count() on your function's return value and
                    checking it was greater than 0.

                    Comment

                    • Lars Eighner

                      #11
                      Re: documentation help needed

                      In our last episode, <Do6dna5hJ5OLXa fVnZ2dnUVZ_qvin Z2d@wideopenwes t.com>,
                      the lovely and talented William Gill broadcast on comp.lang.php:
                      I have used and tested the following:
                      $x = myFunction($som eValue)? "true":"fal se";
                      When myFunction() returns an empty array, it results in false, and when
                      it returns a non-empty array it results in true. This is what I want
                      and expect, but I can't find any documentation on it.
                      'Comparison Operators' in the "Operators" section of the manual -- Chapter
                      15 in my version. Or simply grep the manual for 'ternary operator' --- I
                      don't think there is another one.
                      Being naturally cautious (spelled paranoid) I don't want to rely on this
                      until I'm certain. Can anyone point me to the documentation that supports
                      what I'm seeing?

                      --
                      Lars Eighner <http://larseighner.com/usenet@larseigh ner.com
                      Countdown: 238 days to go.

                      Comment

                      • Michael Fesser

                        #12
                        Re: documentation help needed

                        ..oO(Gordon)
                        >On May 26, 2:50 pm, William Gill <nore...@exampl e.comwrote:
                        >I have used and tested the following:
                        >>
                        >$x = myFunction($som eValue)? "true":"fal se";
                        >>
                        >When myFunction() returns an empty array, it results in false, and when
                        >it returns a non-empty array it results in true.  This is what I want
                        >and expect, but I can't find any documentation on it.  Being naturally
                        >cautious (spelled paranoid) I don't want to rely on this until I'm
                        >certain.  Can anyone point me to the documentation that supports what
                        >I'm seeing?
                        >
                        >That's very odd behavior you're getting there, I was under the
                        >impression that empty arrays evaluated to true!
                        It's correct behaviour, empty always arrays evaluate to FALSE.

                        Converting to boolean


                        Micha

                        Comment

                        • Michael Fesser

                          #13
                          Re: documentation help needed

                          ..oO(Gordon)
                          >On May 26, 2:50 pm, William Gill <nore...@exampl e.comwrote:
                          >I have used and tested the following:
                          >>
                          >$x = myFunction($som eValue)? "true":"fal se";
                          >>
                          >When myFunction() returns an empty array, it results in false, and when
                          >it returns a non-empty array it results in true.  This is what I want
                          >and expect, but I can't find any documentation on it.  Being naturally
                          >cautious (spelled paranoid) I don't want to rely on this until I'm
                          >certain.  Can anyone point me to the documentation that supports what
                          >I'm seeing?
                          >
                          >That's very odd behavior you're getting there, I was under the
                          >impression that empty arrays evaluated to true!
                          It's correct behaviour, empty arrays always evaluate to FALSE.

                          Converting to boolean


                          Micha

                          Comment

                          • AnrDaemon

                            #14
                            Re: documentation help needed

                            Greetings, William Gill.
                            In reply to Your message dated Monday, May 26, 2008, 19:16:51,
                            Erwin Moller wrote:
                            >Why would an empty array evaluate to false?
                            >I don't even know if it does, nor do I care.
                            I retested and both an empty array and an assignment of an empty array
                            return false.
                            They are values that assumed as "empty" (no data) for comparison.
                            These are - "false" value, numerical zero, empty string, the NULL constant,
                            empty array.
                            To make sure you are using true/false test, use strict type comparison (the
                            === or !== operators).
                            Then the

                            $arr = array();
                            if(false !== $arr)
                            {
                            print('it is not \'false\' value');
                            }

                            will show you the truth. Empty array is not the same as 'false' value.
                            As stated elsewhere, the code I'm emulating uses the true/false result
                            of the assignment to control flow, but as Guillaume points out, empty()
                            is more explicit (and probably more reliable).
                            For your specific needs probably, but make sure your functions returning
                            true/false results, before using them in flow control.
                            In some cases, you may want to return zero as valid result, and that will
                            ruine your empty($result) check.


                            --
                            Sincerely Yours, AnrDaemon <anrdaemon@free mail.ru>

                            Comment

                            Working...