variable and string simple question

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

    variable and string simple question

    Hi,

    this is my code:

    <?php
    $i=1;
    $g1R=10;
    $g2R=20;
    $g3R=30;
    switch ('$g$iR'): --> this syntax does not work!!!
    case 10:
    echo "it worked";
    break;
    default:
    echo "it did not work";
    endswitch;;
    ?>

    I define $g{1,2,3}R as variables and I want to use the switch on them
    with $i setting the value of which one of the three will be used. how
    do I write the term inside the switch condition?

    thanks in advance

  • Ewoud Dronkert

    #2
    Re: variable and string simple question

    On 22 May 2005 11:08:46 -0700, inonzuk wrote:[color=blue]
    > I define $g{1,2,3}R as variables and I want to use the switch on them
    > with $i setting the value of which one of the three will be used.[/color]

    $i = 1;
    $gR = array( 1 => 10, 20, 30 );
    switch ( $gR[$i] )
    {
    case 10: break;
    default: break;
    }


    --
    Firefox Web Browser - Rediscover the web - http://getffox.com/
    Thunderbird E-mail and Newsgroups - http://gettbird.com/

    Comment

    • Janwillem Borleffs

      #3
      Re: variable and string simple question

      inonzuk wrote:[color=blue]
      > Hi,
      >
      > this is my code:
      >
      > <?php
      > $i=1;
      > $g1R=10;
      > $g2R=20;
      > $g3R=30;
      > switch ('$g$iR'): --> this syntax does not work!!![/color]

      switch(${"g{$i} R"})...


      JW



      Comment

      • Ewoud Dronkert

        #4
        Re: variable and string simple question

        On Sun, 22 May 2005 22:51:03 +0200, Janwillem Borleffs wrote:[color=blue]
        > switch(${"g{$i} R"})...[/color]

        Yeah, but *really*!


        --
        Firefox Web Browser - Rediscover the web - http://getffox.com/
        Thunderbird E-mail and Newsgroups - http://gettbird.com/

        Comment

        • Janwillem Borleffs

          #5
          Re: variable and string simple question

          Ewoud Dronkert wrote:[color=blue]
          > On Sun, 22 May 2005 22:51:03 +0200, Janwillem Borleffs wrote:[color=green]
          >> switch(${"g{$i} R"})...[/color]
          >
          > Yeah, but *really*![/color]

          Really what? What doesn't look good to you does not mean that it isn't
          possible.

          It's now up to the OP to chose between your suggestion and the original
          approach he wanted to implement.


          JW



          Comment

          • Oli Filth

            #6
            Re: variable and string simple question

            Janwillem Borleffs wrote:[color=blue]
            > Ewoud Dronkert wrote:[color=green]
            > > On Sun, 22 May 2005 22:51:03 +0200, Janwillem Borleffs wrote:[color=darkred]
            > >> switch(${"g{$i} R"})...[/color]
            > >
            > > Yeah, but *really*![/color]
            >
            > Really what? What doesn't look good to you does not mean that it[/color]
            isn't[color=blue]
            > possible.[/color]

            There are *lots* of things that are *possible* in PHP that are very bad
            from a programmatic point of view; that doesn't mean that they should
            be used!!
            [color=blue]
            > It's now up to the OP to chose between your suggestion and the[/color]
            original[color=blue]
            > approach he wanted to implement.[/color]

            The problem is, I'm guessing you're a pretty good programmer, and you
            know how to do things sensibly, as well as how to do clever tricks like
            that.

            Judging by the OP's original code snippet, he probably has the attidue
            that "arrays are more trouble than they're worth, and take more
            typing", or may not even know what arrays are, and will use your
            suggestion because it solves his immediate problem without having to
            change the rest of his code.

            And then he'll go off and write all his PHP scripts using horrible
            convoluted expressions like that, rather than taking the time to learn
            how to do things properly. Which will lead to his needing to use even
            more complicated trickery to fix things later down the line (and
            probably use eval() as well!)

            IMO, it's the "responsibility " of people who know what they're doing to
            encourage good programming practice, or at the very least explain that
            things like the above are to be avoided! But that's just my opinion.

            --
            Oli

            Comment

            • Ewoud Dronkert

              #7
              Re: variable and string simple question

              Janwillem Borleffs wrote:[color=blue]
              > What doesn't look good to you does not mean that it isn't possible.[/color]

              Of course. Just trying to point out to the OP that there was a simpler, more
              general solution, easier to maintain and expand, by using a different
              concept that for forementioned reasons, yes, did look better to me. I just
              *know* it looks better to you too :)
              [color=blue]
              > It's now up to the OP to chose between your suggestion and the original
              > approach he wanted to implement.[/color]

              Let's hope he will choose wisely.

              E.
              --
              Firefox Web Browser - Rediscover the web - http://getffox.com/
              Thunderbird E-mail and Newsgroups - http://gettbird.com/

              Comment

              Working...