Ternary Operator Problem

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

    Ternary Operator Problem

    [PHP]
    $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
    $_GET['sortBy'] : 1;
    [/PHP]

    This script should set $sortBy to the $_POST or the $_GET or the
    default of 1 in one line.

    I do not want to have to do this:

    [PHP]
    if ($_POST['sortBy']) {
    $sortBy = $_POST['sortBy'];
    } elseif ($_GET['sortBy']) {
    $sortBy = $_GET['sortBy'];
    } else {
    $sortBy = 1;
    }
    [/PHP]

    This is too bloated for the 2000-line script I already have, and I
    want to master ternary operators as I have never done so in 7 years.

    I simply want the ternary operator equivalent of:

    if - elseif - else
    NOT

    If - else
    as the default..

    Thanx
    Phil
  • Pedro Graca

    #2
    Re: Ternary Operator Problem

    ["Followup-To:" header set to comp.lang.php.]
    Phil Powell wrote:[color=blue]
    > [PHP]
    > $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
    > $_GET['sortBy'] : 1;
    > [/PHP][/color]

    Try more parenthesis:

    $c1 = isset($_POST['sortBy']);
    $c2 = isset($_GET['sortBy']);

    $sortBy = ($c1) ? ($_POST['sortBy']) : ( ($c2) ? ($_GET['sortBy']) : (1) );

    --
    USENET would be a better place if everybody read: : mail address :
    http://www.catb.org/~esr/faqs/smart-questions.html : is valid for :
    http://www.netmeister.org/news/learn2quote2.html : "text/plain" :
    http://www.expita.com/nomime.html : to 10K bytes :

    Comment

    • Tim Van Wassenhove

      #3
      Re: Ternary Operator Problem

      In article <1cdca2a7.04050 70809.63b60d4b@ posting.google. com>, Phil Powell wrote:[color=blue]
      > [PHP]
      > $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
      > $_GET['sortBy'] : 1;
      > [/PHP][/color]

      $val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
      [color=blue]
      > I do not want to have to do this:
      >
      > [PHP]
      > if ($_POST['sortBy']) {
      > $sortBy = $_POST['sortBy'];
      > } elseif ($_GET['sortBy']) {
      > $sortBy = $_GET['sortBy'];
      > } else {
      > $sortBy = 1;
      > }
      > [/PHP][/color]

      Who cares about lines anyway? Imho the if - elseif - else structure is
      more readable, because more people will understand what is going on...
      (As you said yourself that you hadn't heard about ternary operator..)

      --

      Comment

      • Five Cats

        #4
        Re: Ternary Operator Problem

        In message <1cdca2a7.04050 70809.63b60d4b@ posting.google. com>, Phil
        Powell <soazine@erols. com> writes[color=blue]
        >[PHP]
        >$sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
        >$_GET['sortBy'] : 1;
        >[/PHP]
        >
        >This script should set $sortBy to the $_POST or the $_GET or the
        >default of 1 in one line.
        >
        >I do not want to have to do this:
        >
        >[PHP]
        >if ($_POST['sortBy']) {
        > $sortBy = $_POST['sortBy'];
        >} elseif ($_GET['sortBy']) {
        > $sortBy = $_GET['sortBy'];
        >} else {
        > $sortBy = 1;
        >}
        >[/PHP][/color]

        This might be useful for the _POST & _GET parts of your query::


        [color=blue]
        >
        >This is too bloated for the 2000-line script I already have, and I want
        >to master ternary operators as I have never done so in 7 years.[/color]

        Maybe it's time to split that script into some component parts?
        [color=blue]
        >
        >I simply want the ternary operator equivalent of:
        >
        >
        if - elseif - else
        >
        >NOT
        >
        >
        If - else
        as the default..[/color]

        --
        Five Cats
        Email to: cats_spam at uk2 dot net

        Comment

        • Chung Leong

          #5
          Re: Ternary Operator Problem

          "Phil Powell" <soazine@erols. com> wrote in message
          news:1cdca2a7.0 405070809.63b60 d4b@posting.goo gle.com...[color=blue]
          > [PHP]
          > $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
          > $_GET['sortBy'] : 1;
          > [/PHP]
          >
          > This script should set $sortBy to the $_POST or the $_GET or the
          > default of 1 in one line.[/color]

          Use $_REQUEST--it holds the content of $_POST, $_GET, and $_COOKIE.


          Comment

          • Phil Powell

            #6
            Re: Ternary Operator Problem

            Tim Van Wassenhove <euki@pi.be> wrote in message news:<2g1s9dF3i r5eU1@uni-berlin.de>...[color=blue]
            > In article <1cdca2a7.04050 70809.63b60d4b@ posting.google. com>, Phil Powell wrote:[color=green]
            > > [PHP]
            > > $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
            > > $_GET['sortBy'] : 1;
            > > [/PHP][/color]
            >
            > $val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
            >[color=green]
            > > I do not want to have to do this:
            > >
            > > [PHP]
            > > if ($_POST['sortBy']) {
            > > $sortBy = $_POST['sortBy'];
            > > } elseif ($_GET['sortBy']) {[/color]
            > $sortBy = $_GET['sortBy'];[color=green]
            > > } else {[/color]
            > $sortBy = 1;[color=green]
            > > }
            > > [/PHP][/color]
            >
            > Who cares about lines anyway?[/color]

            [snip]

            The person who inherits your 50 PHP scripts each averaging 1700 lines
            each, might care.

            Phil

            Comment

            • Tim Van Wassenhove

              #7
              Re: Ternary Operator Problem

              In article <1cdca2a7.04051 00524.736d5cb7@ posting.google. com>, Phil Powell wrote:[color=blue]
              > Tim Van Wassenhove <euki@pi.be> wrote in message news:<2g1s9dF3i r5eU1@uni-berlin.de>...[color=green]
              >> In article <1cdca2a7.04050 70809.63b60d4b@ posting.google. com>, Phil Powell wrote:[color=darkred]
              >> > [PHP]
              >> > $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
              >> > $_GET['sortBy'] : 1;
              >> > [/PHP][/color]
              >>
              >> $val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
              >>[color=darkred]
              >> > I do not want to have to do this:
              >> >
              >> > [PHP]
              >> > if ($_POST['sortBy']) {
              >> > $sortBy = $_POST['sortBy'];
              >> > } elseif ($_GET['sortBy']) {[/color]
              >> $sortBy = $_GET['sortBy'];[color=darkred]
              >> > } else {[/color]
              >> $sortBy = 1;[color=darkred]
              >> > }
              >> > [/PHP][/color]
              >>
              >> Who cares about lines anyway?[/color]
              >
              > [snip]
              >
              > The person who inherits your 50 PHP scripts each averaging 1700 lines
              > each, might care.[/color]

              Then that person should get himself a decent editor, so he can collapse
              code blocks etc...

              All that matters is the readability, and i think the if - else structure
              is more readable.

              --

              Comment

              • Phil Powell

                #8
                Re: Ternary Operator Problem

                It's one code block, 1700 lines each, 20 files.

                They're files consisting of 20 - 30 classes each. All in one code
                block. Total lines: avg 1700.

                Phil

                Tim Van Wassenhove <euki@pi.be> wrote in message news:<2g9hkpF6l hoU1@uni-berlin.de>...[color=blue]
                > In article <1cdca2a7.04051 00524.736d5cb7@ posting.google. com>, Phil Powell wrote:[color=green]
                > > Tim Van Wassenhove <euki@pi.be> wrote in message news:<2g1s9dF3i r5eU1@uni-berlin.de>...[color=darkred]
                > >> In article <1cdca2a7.04050 70809.63b60d4b@ posting.google. com>, Phil Powell wrote:
                > >> > [PHP]
                > >> > $sortBy = ($_POST['sortBy']) ? $_POST['sortBy'] : ($_GET['sortBy']) ?
                > >> > $_GET['sortBy'] : 1;
                > >> > [/PHP]
                > >>
                > >> $val = isset($_POST['val']) ? $_POST['val'] : (isset($_GET['val']) ? $_GET['val'] : 1);
                > >>
                > >> > I do not want to have to do this:
                > >> >
                > >> > [PHP]
                > >> > if ($_POST['sortBy']) {
                > >> > $sortBy = $_POST['sortBy'];
                > >> > } elseif ($_GET['sortBy']) {[/color][/color]
                > $sortBy = $_GET['sortBy'];[color=green][color=darkred]
                > >> > } else {[/color][/color]
                > $sortBy = 1;[color=green][color=darkred]
                > >> > }
                > >> > [/PHP]
                > >>
                > >> Who cares about lines anyway?[/color]
                > >
                > > [snip]
                > >
                > > The person who inherits your 50 PHP scripts each averaging 1700 lines
                > > each, might care.[/color]
                >
                > Then that person should get himself a decent editor, so he can collapse
                > code blocks etc...
                >
                > All that matters is the readability, and i think the if - else structure
                > is more readable.[/color]

                Comment

                Working...