PHP - How Can I Create Variable Names Within a Loop?

后端 未结 5 1087
抹茶落季
抹茶落季 2020-12-04 18:51

My goal is to have a loop where variable names are created. Such as:

$variable1
$variable2
$variable3

and ass

相关标签:
5条回答
  • 2020-12-04 19:06

    Just to expand on what others have said about why this is a technique best avoided: variable names exist solely for use by the programmer; they have no relationships or properties according to the language's runtime. As such, a collection of related variables should be put into a variable representing some appropriate container - in the case of PHP, its incredibly flexible "array" type.

    Whenever the issue of dynamically named variables is raised, it is usually because somebody thinks it is a solution to a particular problem, but it generally leads to more problems than it solves - for instance, once part of a program starts dynamically naming variables, everywhere that wants to use those variables now also has to dynamically name them; finding out which items are actually set becomes unnecessarily complex, and so on.

    There are a very few contexts where variable-variables are useful, for certain kinds of debugging and meta-programming, for example, but like goto, their presence in the language should not mean you don't try every other possible solution first.

    999 times out of 1000, though, if you find yourself wondering how to dynamically create variable names, you should reframe the question: what are you actually trying to achieve, and what is the best solution to that problem. If some existing / 3rd-party code has been built using this pattern (and presumably also global variables), it may be best to patch or wrap it rather than propogating the mis-design into your own code. (Admittedly, a wrapper will still need to know how to declare the dynamic variables.)

    0 讨论(0)
  • 2020-12-04 19:07

    Thats no possible since variable cannot be create in run time

    0 讨论(0)
  • 2020-12-04 19:17

    If you had an associative array i'ld prefer using the extract() method. Check it out here

    for example

    <?php
    
    /* Suppose that $var_array is an array*/
    
    $var_array = array("color" => "blue",
                   "size"  => "medium",
                   "shape" => "sphere");
    extract($var_array, EXTR_PREFIX_SAME, "wddx");
    
    echo "$color, $size, $shape";
    
    ?>
    

    the above code would output, blue, large, sphere, medium

    0 讨论(0)
  • 2020-12-04 19:18

    There are several ways. One is via a string and a double $, but I don't like this method. A programmer could easily remove the double $ (seeing it as a typeo).

    for($i = 0; $i <= 7; $i++) {
         $name = "variable{$i}";
         $$name = "foo";
    }
    

    The best approach is the explicit statement.

    for($i = 0; $i <= 7; $i++) {
         ${"variable$i"} = "foo";
    }
    

    With the explicit inline string there is no doubt about the intent of what you're doing. Another PHP programmer will not alter the code by mistake.

    0 讨论(0)
  • 2020-12-04 19:18

    You should use an array for this:

    for($i = 1; $i <= 7; $i++) {
      $variable[$i] = $i;
    }
    

    Edit: to clarify, you should use an array because there's no code (to my knowledge) which will accept $variable1 but not $variable[1]. Dynamically generating variable names is all kinds of wrong.

    0 讨论(0)
提交回复
热议问题