Eval, Variable Variables problem

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

    Eval, Variable Variables problem

    I have a small pre-determined number of possible values for a variable, and
    I would like to set a value for the corresponding 'switch type' variable.

    For example, if $x is 14, I want to execute the following statement:
    $switch_14 = "on";

    In general, I want something like this:
    $switch_.$x = "on";

    I know I could build a switch/case structure since the values for $x are
    known and there are only a dozen or so of them, but it would seem more
    elegant and/or compact to do it another way.

    I've looked at eval() and variable variables and can't seem to get anything
    to work.
    I've also read the quote "If eval is the answer, you're asking the wrong
    question".

    Thanks for any help you can provide.

    T


  • Michael Fesser

    #2
    Re: Eval, Variable Variables problem

    ..oO(Ted)
    >I have a small pre-determined number of possible values for a variable, and
    >I would like to set a value for the corresponding 'switch type' variable.
    >
    >For example, if $x is 14, I want to execute the following statement:
    $switch_14 = "on";
    >
    >In general, I want something like this:
    $switch_.$x = "on";
    You're looking for an array:

    $switch[$x] = TRUE;

    Micha

    Comment

    • Rik

      #3
      Re: Eval, Variable Variables problem

      Ted wrote:
      I have a small pre-determined number of possible values for a
      variable, and I would like to set a value for the corresponding
      'switch type' variable.
      >
      For example, if $x is 14, I want to execute the following statement:
      $switch_14 = "on";
      >
      In general, I want something like this:
      $switch_.$x = "on";
      >
      I know I could build a switch/case structure since the values for $x
      are known and there are only a dozen or so of them, but it would seem
      more elegant and/or compact to do it another way.
      >
      I've looked at eval() and variable variables and can't seem to get
      anything to work.
      I've also read the quote "If eval is the answer, you're asking the
      wrong question".
      >
      Thanks for any help you can provide.

      As said, you probably want an array. However, if you want to stick with
      your way, I think this will work (untested):

      ${'switch_'.$i} = 'on';
      --
      Rik Wasmus


      Comment

      • Hendri Kurniawan

        #4
        Re: Eval, Variable Variables problem

        Just a suggestion.. Why not use array?

        $switches = array();
        $switches[$x] = 'on';

        That way it's cleaner, and no need to change any
        switches or cases values later on when you
        decided to change the $x to char or word for example.

        Hendri Kurniawan


        Ted wrote:
        I have a small pre-determined number of possible values for a variable, and
        I would like to set a value for the corresponding 'switch type' variable.
        >
        For example, if $x is 14, I want to execute the following statement:
        $switch_14 = "on";
        >
        In general, I want something like this:
        $switch_.$x = "on";
        >
        I know I could build a switch/case structure since the values for $x are
        known and there are only a dozen or so of them, but it would seem more
        elegant and/or compact to do it another way.
        >
        I've looked at eval() and variable variables and can't seem to get anything
        to work.
        I've also read the quote "If eval is the answer, you're asking the wrong
        question".
        >
        Thanks for any help you can provide.
        >
        T
        >
        >

        Comment

        • BlueNosE

          #5
          Re: Eval, Variable Variables problem

          you may do:

          $x = 1;
          $name = "var_{$x}";
          $$name = "On";

          echo $var_1;// On

          Comment

          Working...