Variable assignment using OR operator?

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

    Variable assignment using OR operator?

    Hello,

    I would like to set a variable to a default if 2 other vars are undef.
    Obviously there are several ways to do this but I would like to do it as I
    would in Perl:

    $memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';

    Is there acorrect way to do this in 1 line in PHP?

    TIA,

    jg


  • Michael Fesser

    #2
    Re: Variable assignment using OR operator?

    .oO(jerrygarciu h)
    [color=blue]
    >I would like to set a variable to a default if 2 other vars are undef.
    >Obviously there are several ways to do this but I would like to do it as I
    >would in Perl:
    >
    >$memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
    >
    >Is there acorrect way to do this in 1 line in PHP?[/color]

    $memberState = isset($_REQUEST['ms']) ? $_REQUEST['ms'] : 'Unapproved';

    Try if that fits your needs ($_REQUEST contains both POST and GET
    variables).

    Micha

    Comment

    • jerrygarciuh

      #3
      Re: Variable assignment using OR operator?

      Thanks!

      jg


      "Michael Fesser" <netizen@gmx.ne t> wrote in message
      news:8v6n11d2eu 33jicglqugge5ng ejrrrsiqn@4ax.c om...[color=blue]
      > .oO(jerrygarciu h)
      >[color=green]
      >>I would like to set a variable to a default if 2 other vars are undef.
      >>Obviously there are several ways to do this but I would like to do it as I
      >>would in Perl:
      >>
      >>$memberStat e = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
      >>
      >>Is there acorrect way to do this in 1 line in PHP?[/color]
      >
      > $memberState = isset($_REQUEST['ms']) ? $_REQUEST['ms'] : 'Unapproved';
      >
      > Try if that fits your needs ($_REQUEST contains both POST and GET
      > variables).
      >
      > Micha[/color]


      Comment

      • Matt Mitchell

        #4
        Re: Variable assignment using OR operator?

        "jerrygarci uh" <designs@no.spa m.nolaflash.com > wrote in message
        news:QPMSd.4029 $SF.1615@lakere ad08...
        : Hello,
        :
        : I would like to set a variable to a default if 2 other vars are undef.
        : Obviously there are several ways to do this but I would like to do it as I
        : would in Perl:
        :
        : $memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
        :
        : Is there acorrect way to do this in 1 line in PHP?
        :

        I believe it would work just like that, you might want brackets though:

        $memberState = ($_POST['ms'] || $_GET['ms'] || 'Unapproved');

        otherwise, you'll get

        if (!($memberState = $_POST['ms']))
        {
        $GET['ms'] or 'Unapproved';
        }
        which is probably not what you want


        Comment

        • jerrygarciuh

          #5
          Re: Variable assignment using OR operator?

          Actually that performs as though it is a conditional:

          echo $memberState = ($x || $y || 'Unapproved');

          outputs 1

          Surely there is a construct for this in PHP? Michaels solution, while
          helpful in that case does not solve the over all problem of an elegant 1
          liner which does this lovely trick.

          Thanks for the reply!

          jg



          "Matt Mitchell" <m_a_t_t_remove _the_underscore s@metalsponge.n et> wrote in
          message news:b1qTd.1753 04$B8.158991@fe 3.news.blueyond er.co.uk...[color=blue]
          > "jerrygarci uh" <designs@no.spa m.nolaflash.com > wrote in message
          > news:QPMSd.4029 $SF.1615@lakere ad08...
          > : Hello,
          > :
          > : I would like to set a variable to a default if 2 other vars are undef.
          > : Obviously there are several ways to do this but I would like to do it as
          > I
          > : would in Perl:
          > :
          > : $memberState = $_POST['ms'] || $_GET['ms'] || 'Unapproved';
          > :
          > : Is there acorrect way to do this in 1 line in PHP?
          > :
          >
          > I believe it would work just like that, you might want brackets though:
          >
          > $memberState = ($_POST['ms'] || $_GET['ms'] || 'Unapproved');
          >
          > otherwise, you'll get
          >
          > if (!($memberState = $_POST['ms']))
          > {
          > $GET['ms'] or 'Unapproved';
          > }
          > which is probably not what you want
          >
          >[/color]


          Comment

          • Jan Pieter Kunst

            #6
            Re: Variable assignment using OR operator?

            jerrygarciuh wrote:
            [color=blue]
            > an elegant 1
            > liner which does this lovely trick.[/color]

            This is just my personal opinion, but since I started writing large
            programs that have to be maintainable and understandable to myself and
            others, I have really come to dislike 'elegant one liners'.

            If I understood your problem correctly, I would simply write this:
            (combining $_POST and $_GET in S_REQUEST):


            if (isset($_REQUES T['ms']))
            $memberState = $_REQUEST['ms'];
            else
            $memberState = 'Unapproved';


            I'd rather have four lines which are immediately obvious than one line
            which is a puzzle, however elegant it may be.

            But to each his own, YMMV, etc.

            JP

            --
            Sorry, <devnull@cauce. org> is a spam trap.
            Real e-mail address unavailable. 5000+ spams per month.

            Comment

            Working...