php equivalent to perl's || behavior?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Coder Droid

    php equivalent to perl's || behavior?

    one feature of perl I'm desparately missing in php is using || to assign
    the first non-empty value in a list to a variable. For example,

    # perl example 1
    $a = 1;
    $b = 2;
    $c = $a || $b;
    print $c; # displays 1

    # perl example 2
    $a = 1;
    $b = 2;
    $c = $d || $b;
    print $c; # displays 2, since $d is empty

    # perl example 3
    $a = 'apple';
    $b = 'banana';
    $c = 'cherry';
    $d = $a || $b || $c;
    print $d; # displays 'apple';

    # perl example 4
    $a = '';
    $b = '';
    $c = 'cherry';
    $d = $a || $b || $c;
    print $d; # displays 'cherry';

    This is such a handy language construct. Is there any such php
    equivalent that accomplishes this? I've already written a function to do
    it. (Like this: $d = value($a, $b, $c). But making value() visible
    everywhere is something I'd rather avoid if there's something built-in
    to php.)

    --cd


  • Chung Leong

    #2
    Re: php equivalent to perl's || behavior?


    "Coder Droid" <coderdroid@lik ethiswouldstops pam.hotmail.com > wrote in
    message news:PsJad.1959 5$iC4.16000@fe2 .texas.rr.com.. .[color=blue]
    > one feature of perl I'm desparately missing in php is using || to assign
    > the first non-empty value in a list to a variable. For example,
    >
    > # perl example 1
    > $a = 1;
    > $b = 2;
    > $c = $a || $b;
    > print $c; # displays 1
    >
    > # perl example 2
    > $a = 1;
    > $b = 2;
    > $c = $d || $b;
    > print $c; # displays 2, since $d is empty
    >
    > # perl example 3
    > $a = 'apple';
    > $b = 'banana';
    > $c = 'cherry';
    > $d = $a || $b || $c;
    > print $d; # displays 'apple';
    >
    > # perl example 4
    > $a = '';
    > $b = '';
    > $c = 'cherry';
    > $d = $a || $b || $c;
    > print $d; # displays 'cherry';
    >
    > This is such a handy language construct. Is there any such php
    > equivalent that accomplishes this? I've already written a function to do
    > it. (Like this: $d = value($a, $b, $c). But making value() visible
    > everywhere is something I'd rather avoid if there's something built-in
    > to php.)
    >
    > --cd
    >[/color]

    No, unfortunately.[color=blue]
    >[/color]


    Comment

    • CJ Llewellyn

      #3
      Re: php equivalent to perl's || behavior?

      "Coder Droid" <coderdroid@lik ethiswouldstops pam.hotmail.com > wrote in
      message news:PsJad.1959 5$iC4.16000@fe2 .texas.rr.com.. .
      -snip-[color=blue]
      > # perl example 4
      > $a = '';
      > $b = '';
      > $c = 'cherry';
      > $d = $a || $b || $c;
      > print $d; # displays 'cherry';
      >
      > This is such a handy language construct. Is there any such php
      > equivalent that accomplishes this? I've already written a function to do
      > it. (Like this: $d = value($a, $b, $c). But making value() visible
      > everywhere is something I'd rather avoid if there's something built-in
      > to php.)[/color]

      The closest that php comes is the Ternary Operator





      Comment

      • Coder Droid

        #4
        Re: php equivalent to perl's || behavior?

        > > This is such a handy language construct. Is there any such php[color=blue][color=green]
        > > equivalent that accomplishes this? I've already written a function[/color][/color]
        to do[color=blue][color=green]
        > > it. (Like this: $d = value($a, $b, $c). But making value() visible
        > > everywhere is something I'd rather avoid if there's something[/color][/color]
        built-in[color=blue][color=green]
        > > to php.)
        > >
        > > --cd
        > >[/color]
        >
        > No, unfortunately.[/color]

        Hmmm... that isn't the answer I wanted. ;) But, it's the one I
        expected. I thought I'd pretty much exhausted my search for something.

        The function I wrote to do this is:

        function value()
        {
        $numargs = func_num_args() ;
        for ($i = 0; $i < $numargs; $i++) {
        $value = func_get_arg($i );
        if ($value != '') {
        return $value;
        }
        }
        }

        Which obviously loops though all values and returns the first non-empty
        one. I use this to determine the value for something when the source
        value might come from one of a number of places. e.g., let's say I'm
        trying to set the default language, I might use it like this:

        $a = value($_REQUEST['lang'], $config['lang'], $_ENV['lang'], 'en');

        This allows a number of fallback values. While it works, it is about 200
        bytes, which is a hundred times larger than "||". And since it's a
        function, I've got to worry about its visibility everywhere. Right now,
        it's a method of an object.

        Hmmmm.... well, okay. I'm done thinking out loud. Guess I'll ponder the
        best way to do this some more.

        --cd


        Comment

        • Berislav Lopac

          #5
          Re: php equivalent to perl's || behavior?

          On Tue, 12 Oct 2004 13:09:13 GMT, Coder Droid wrote:
          [color=blue]
          > The function I wrote to do this is:
          >
          > function value()
          > {
          > $numargs = func_num_args() ;
          > for ($i = 0; $i < $numargs; $i++) {
          > $value = func_get_arg($i );
          > if ($value != '') {
          > return $value;
          > }
          > }
          > }
          >
          > Which obviously loops though all values and returns the first non-empty
          > one. I use this to determine the value for something when the source
          > value might come from one of a number of places. e.g., let's say I'm
          > trying to set the default language, I might use it like this:
          >
          > $a = value($_REQUEST['lang'], $config['lang'], $_ENV['lang'], 'en');
          >
          > This allows a number of fallback values. While it works, it is about 200
          > bytes, which is a hundred times larger than "||". And since it's a
          > function, I've got to worry about its visibility everywhere. Right now,
          > it's a method of an object.[/color]

          This might be a bit smaller:

          function value() {
          foreach(func_ge t_args() as $a) {
          if($a != '') return $a;
          }
          }

          Berislav

          Comment

          • Coder Droid

            #6
            Re: php equivalent to perl's || behavior?

            > function value() {[color=blue]
            > foreach(func_ge t_args() as $a) {
            > if($a != '') return $a;
            > }
            > }[/color]

            True. And that does look cleaner, so I'll probably use that. But it's
            still a function, so I still have my visibility problems. I just hate
            having to use "global $whatever" every time I want to declare this
            object to access this method.

            So I'm thinking about (just this once) having one function defined
            outside of all my objects (a "supergloba l function" of sorts) and
            declaring it at the beginning. Hmmmm... I might give it a shot and see
            how it turns out. Sometimes it's hard to tell without a test drive.

            --cd


            Comment

            • Berislav Lopac

              #7
              Re: php equivalent to perl's || behavior?

              On Tue, 12 Oct 2004 20:37:25 GMT, Coder Droid wrote:
              [color=blue]
              > True. And that does look cleaner, so I'll probably use that. But it's
              > still a function, so I still have my visibility problems. I just hate
              > having to use "global $whatever" every time I want to declare this
              > object to access this method.
              >
              > So I'm thinking about (just this once) having one function defined
              > outside of all my objects (a "supergloba l function" of sorts) and
              > declaring it at the beginning.[/color]

              Which is a completely legal approach in PHP.

              Berislav

              Comment

              • Markus Ernst

                #8
                Re: php equivalent to perl's || behavior?

                Coder Droid wrote:[color=blue][color=green]
                >> function value() {
                >> foreach(func_ge t_args() as $a) {
                >> if($a != '') return $a;
                >> }
                >> }[/color]
                >
                > True. And that does look cleaner, so I'll probably use that. But it's
                > still a function, so I still have my visibility problems. I just hate
                > having to use "global $whatever" every time I want to declare this
                > object to access this method.
                >
                > So I'm thinking about (just this once) having one function defined
                > outside of all my objects (a "supergloba l function" of sorts) and
                > declaring it at the beginning. Hmmmm... I might give it a shot and see
                > how it turns out. Sometimes it's hard to tell without a test drive.[/color]

                I use a class which works as a container for all my functions I want to see
                globally, and let all other classes inherit it; so that kind of function is
                visible everywhere.

                class functions
                {
                function value()
                {
                [...]
                }
                }

                class otherclass extends functions
                {
                function use_value($a, $b, $c)
                {
                return $this->value($a, $b, $c);
                }
                }

                And also in the calling script you can use $otherclass->value() as soon as
                you have an instance of otherclass.

                HTH
                Markus


                Comment

                • Coder Droid

                  #9
                  Re: php equivalent to perl's || behavior?

                  > > So I'm thinking about (just this once) having one function defined[color=blue][color=green]
                  > > outside of all my objects (a "supergloba l function" of sorts) and
                  > > declaring it at the beginning.[/color]
                  >
                  > Which is a completely legal approach in PHP.[/color]

                  And which is what I ended up doing. It's not too bad, actually.

                  --cd


                  Comment

                  • Coder Droid

                    #10
                    Re: php equivalent to perl's || behavior?

                    > I use a class which works as a container for all my functions I want
                    to see[color=blue]
                    > globally, and let all other classes inherit it; so that kind of[/color]
                    function is[color=blue]
                    > visible everywhere.[/color]

                    Hmmm... hadn't thought of trying that either. Thanks for the tip.

                    --cd


                    Comment

                    Working...