#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,558
    Rep Power
    221

    Is this valid to build variables like this ${$element_name . "_bgcolor_db"}


    Hi;

    Making a big form. Instead of wrting thousands of lines, I wanna loop it.

    In order to do that I need to make dynamic variable names.

    Is this the right way?

    PHP Code:
    <?php 
    $pre_headline_bgcolor 
    "red";
    $element_name="pre_headline";
    echo ${
    $element_name "_bgcolor_db"};?>
    Code:
    <input type = "hidden" 
    name = "<?php echo $element_name . "_bgcolor_db";?>" 
    id = "<?php echo $element_name;?>_bgcolor_db" 
    value="<?php echo html_escape(${$element_name . "_bgcolor_db"});?>">
    Last edited by English Breakfast Tea; May 28th, 2018 at 07:59 PM.
  2. #2
  3. Backwards Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,904
    Rep Power
    9646
    No. It may be valid syntax but there is no good reason to do it that way.

    Use an array. Perhaps
    PHP Code:
    $colors[$element_name] = ["bgcolor_db" => "red"]; 
    Code:
    <input type="hidden"
    name="colors[<?=$element_name?>][bgcolor_db]"
    id="colors_<?=$element_name?>_bgcolor_db"
    value="<?=html_escape($colors[$element_name]["bgcolor_db"])?>">

IMN logo majestic logo threadwatch logo seochat tools logo