ternary operator or if() statement for default value?

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

    #1

    ternary operator or if() statement for default value?

    I'm trying to write some simple code, but I might be simplifying it
    too much.

    I have a function that either returns a string that I want, or FALSE.
    In the code that I'm working on, I would like to default to another
    string if the function returns false.

    Here's the long way to do it:

    $string = myFunction(2);
    if ( $string === FALSE ) {
    $string = "default value";

    }

    I would like to use the value from the function, if it's not false,
    else the default value.

    if ( $string = myFunction(2) === FALSE ) {
    $string = "default value";

    }

    If I were using the ternary operator to do this, I would do

    $string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;

    but it looks like I'm calling the function twice, which would be
    inefficient.

    What's the most efficient way to do this?
  • Michael Fesser

    #2
    Re: ternary operator or if() statement for default value?

    ..oO(lawpoop@gm ail.com)
    >I'm trying to write some simple code, but I might be simplifying it
    >too much.
    >
    >I have a function that either returns a string that I want, or FALSE.
    >In the code that I'm working on, I would like to default to another
    >string if the function returns false.
    >
    >Here's the long way to do it:
    >
    >$string = myFunction(2);
    >if ( $string === FALSE ) {
    $string = "default value";
    >
    >}
    >
    >I would like to use the value from the function, if it's not false,
    >else the default value.
    >
    >if ( $string = myFunction(2) === FALSE ) {
    $string = "default value";
    >
    >}
    >
    >If I were using the ternary operator to do this, I would do
    >
    >$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
    >
    >but it looks like I'm calling the function twice, which would be
    >inefficient.
    >
    >What's the most efficient way to do this?
    Efficiency is not really an issue. Question is what's more convenient
    and what you feel more comfortable with. You could do something like

    $string = ($value = myFunction(2)) === FALSE
    ? 'default value'
    : $value;

    But I would probably use a little helper function with two arguments:

    function strInit($value, $default)
    return ($value !== FALSE) ? $value : $default;
    }

    And then:

    $string = strInit(myFunct ion(2), 'default value');

    Micha

    Comment

    • lawpoop@gmail.com

      #3
      Re: ternary operator or if() statement for default value?

      On Oct 9, 3:58 pm, Michael Fesser <neti...@gmx.de wrote:
      Efficiency is not really an issue. Question is what's more convenient
      and what you feel more comfortable with. You could do something like
      >
      $string = ($value = myFunction(2)) === FALSE
        ? 'default value'
        : $value;
      >
      Thanks, Micha! That's exactly what I was looking for!


      Comment

      • Michael Fesser

        #4
        Re: ternary operator or if() statement for default value?

        ..oO(lawpoop@gm ail.com)
        >On Oct 9, 3:58 pm, Michael Fesser <neti...@gmx.de wrote:
        >
        >Efficiency is not really an issue. Question is what's more convenient
        >and what you feel more comfortable with. You could do something like
        >>
        >$string = ($value = myFunction(2)) === FALSE
        >  ? 'default value'
        >  : $value;
        >>
        >
        >Thanks, Micha! That's exactly what I was looking for!
        You're welcome.

        Micha

        Comment

        • Stewart Robert Hinsley

          #5
          Re: ternary operator or if() statement for default value?

          In message
          <9744b18b-8016-447b-9b81-a982f1a22edb@u4 6g2000hsc.googl egroups.com>,
          lawpoop@gmail.c om writes
          >I'm trying to write some simple code, but I might be simplifying it
          >too much.
          >
          >I have a function that either returns a string that I want, or FALSE.
          >In the code that I'm working on, I would like to default to another
          >string if the function returns false.
          >
          >Here's the long way to do it:
          >
          >$string = myFunction(2);
          >if ( $string === FALSE ) {
          $string = "default value";
          >
          >}
          >
          >I would like to use the value from the function, if it's not false,
          >else the default value.
          >
          >if ( $string = myFunction(2) === FALSE ) {
          $string = "default value";
          >
          >}
          >
          >If I were using the ternary operator to do this, I would do
          >
          >$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
          >
          >but it looks like I'm calling the function twice, which would be
          >inefficient.
          It's also problematic if the function has side effects.
          >
          >What's the most efficient way to do this?
          Checking the manual, it appears that PHP has inherited C-like assignment
          operator semantics, so

          $string = ($temp = myFunction(2)) === FALSE ? "default value" : $temp;

          --
          Stewart Robert Hinsley

          Comment

          • lawpoop@gmail.com

            #6
            Re: ternary operator or if() statement for default value?

            On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden .demon.co.uk>
            wrote:
            In message
            <9744b18b-8016-447b-9b81-a982f1a22...@u4 6g2000hsc.googl egroups.com>,
            lawp...@gmail.c om writes
            >
            If I were using the ternary operator to do this, I would do
            >
            $string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
            >
            but it looks like I'm calling the function twice, which would be
            inefficient.
            >
            It's also problematic if the function has side effects.
            "Side effects" being if the function returns anything other than a
            valid string or false?

            Comment

            • Stewart Robert Hinsley

              #7
              Re: ternary operator or if() statement for default value?

              In message
              <b03b781b-c5da-4a94-9ffb-5633b2dc21fd@l3 3g2000pri.googl egroups.com>,
              lawpoop@gmail.c om writes
              >On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden .demon.co.uk>
              >wrote:
              >In message
              ><9744b18b-8016-447b-9b81-a982f1a22...@u4 6g2000hsc.googl egroups.com>,
              >lawp...@gmail. com writes
              >
              >>
              >If I were using the ternary operator to do this, I would do
              >>
              >$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
              >>
              >but it looks like I'm calling the function twice, which would be
              >inefficient.
              >>
              >It's also problematic if the function has side effects.
              >
              >"Side effects" being if the function returns anything other than a
              >valid string or false?
              >
              Side effects being if the function changes any non-local or static
              variable during its execution.

              So it the function has internal state, and this changes when it is
              called, that's a side effect. (For example a random number generator
              function.)

              So if the function changes any global variable (or class variables, if
              it's a member function of a class) that's a side effect.

              So if a function changes a variable passed to it as a reference, that's
              a side effect.

              Pascal distinguishes between procedures (which can have side effects)
              and functions (which can't), but most languages only have a single
              construct, which covers both concepts. (Some languages call them
              functions, some procedures, some subroutines, and I wouldn't be
              surprised if there were other alternate names.)
              --
              Stewart Robert Hinsley

              Comment

              • Michael Fesser

                #8
                Re: ternary operator or if() statement for default value?

                ..oO(Stewart Robert Hinsley)
                >Side effects being if the function changes any non-local or static
                >variable during its execution.
                >
                >So it the function has internal state, and this changes when it is
                >called, that's a side effect. (For example a random number generator
                >function.)
                >
                >So if the function changes any global variable (or class variables, if
                >it's a member function of a class) that's a side effect.
                >
                >So if a function changes a variable passed to it as a reference, that's
                >a side effect.
                >
                >Pascal distinguishes between procedures (which can have side effects)
                >and functions (which can't),
                Functions in Pascal can have side effects as well. The only difference
                is that they have a return value, while procedures have not.

                Micha

                Comment

                • Jerry Stuckle

                  #9
                  Re: ternary operator or if() statement for default value?

                  Stewart Robert Hinsley wrote:
                  In message
                  <b03b781b-c5da-4a94-9ffb-5633b2dc21fd@l3 3g2000pri.googl egroups.com>,
                  lawpoop@gmail.c om writes
                  >On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden .demon.co.uk>
                  >wrote:
                  >>In message
                  >><9744b18b-8016-447b-9b81-a982f1a22...@u4 6g2000hsc.googl egroups.com>,
                  >>lawp...@gmail .com writes
                  >>
                  >>>
                  >>If I were using the ternary operator to do this, I would do
                  >>>
                  >>$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
                  >>>
                  >>but it looks like I'm calling the function twice, which would be
                  >>inefficient .
                  >>>
                  >>It's also problematic if the function has side effects.
                  >>
                  >"Side effects" being if the function returns anything other than a
                  >valid string or false?
                  >>
                  >
                  Side effects being if the function changes any non-local or static
                  variable during its execution.
                  >
                  So it the function has internal state, and this changes when it is
                  called, that's a side effect. (For example a random number generator
                  function.)
                  >
                  So if the function changes any global variable (or class variables, if
                  it's a member function of a class) that's a side effect.
                  >
                  So if a function changes a variable passed to it as a reference, that's
                  a side effect.
                  >
                  Pascal distinguishes between procedures (which can have side effects)
                  and functions (which can't), but most languages only have a single
                  construct, which covers both concepts. (Some languages call them
                  functions, some procedures, some subroutines, and I wouldn't be
                  surprised if there were other alternate names.)
                  Not quite true. In Pascal, a procedure cannot return a value, while a
                  function must return a value.

                  Either one can have side effects.

                  As for "most languages only have a single construct" - true of many
                  languages which are based at least in part on C. Other languages such
                  as FORTRAN, PL/1 and others have both concepts. In those which don't
                  distinguish between the two, the presence of a return value is optional.

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

                  Comment

                  • Stewart Robert Hinsley

                    #10
                    Re: ternary operator or if() statement for default value?

                    In message <gco9br$e9o$1@r egistered.motza rella.org>, Jerry Stuckle
                    <jstucklex@attg lobal.netwrites
                    >Stewart Robert Hinsley wrote:
                    >In message
                    >><b03b781b-c5da-4a94-9ffb-5633b2dc21fd@l3 3g2000pri.googl egroups.com>,
                    >>lawpoop@gmail .com writes
                    >>On Oct 9, 4:33 pm, Stewart Robert Hinsley <{$new...@meden .demon.co.uk>
                    >>wrote:
                    >>>In message
                    >>><9744b18b-8016-447b-9b81-a982f1a22...@u4 6g2000hsc.googl egroups.com>,
                    >>>lawp...@gmai l.com writes
                    >>>
                    >>>>
                    >>>If I were using the ternary operator to do this, I would do
                    >>>>
                    >>>$string = myFunction(2) === FALSE ? "default value" : myFunction(2) ;
                    >>>>
                    >>>but it looks like I'm calling the function twice, which would be
                    >>>inefficien t.
                    >>>>
                    >>>It's also problematic if the function has side effects.
                    >>>
                    >>"Side effects" being if the function returns anything other than a
                    >>valid string or false?
                    >>>
                    > Side effects being if the function changes any non-local or static
                    >>variable during its execution.
                    > So it the function has internal state, and this changes when it is
                    >>called, that's a side effect. (For example a random number generator
                    >>function.)
                    > So if the function changes any global variable (or class variables,
                    >>if it's a member function of a class) that's a side effect.
                    > So if a function changes a variable passed to it as a reference,
                    >>that's a side effect.
                    > Pascal distinguishes between procedures (which can have side
                    >>effects) and functions (which can't), but most languages only have a
                    >>single construct, which covers both concepts. (Some languages call
                    >>them functions, some procedures, some subroutines, and I wouldn't be
                    >>surprised if there were other alternate names.)
                    >
                    >Not quite true. In Pascal, a procedure cannot return a value, while a
                    >function must return a value.
                    Yes - I shouldn't have relied on my memory from the 25 years or more
                    since I used Pascal. Jensen and Wirth wrote "strongly discouraged".
                    >
                    >Either one can have side effects.
                    >
                    >As for "most languages only have a single construct" - true of many
                    >languages which are based at least in part on C. Other languages such
                    >as FORTRAN, PL/1 and others have both concepts. In those which don't
                    >distinguish between the two, the presence of a return value is optional.
                    >
                    --
                    Stewart Robert Hinsley

                    Comment

                    Working...