0

I need to define a set of variables to a series

$charta ='<img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/aplha.png" alt ="" />';
$chartb ='<img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/bravo.png" alt ="" />';
$chartc ='<img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/charlie.png" alt ="" />';

etc ...

Rather than define them all like this I want to use a loop to define them using an array

$names = array (
'aplha' =>  'a',
'bravo' =>   'b',
'charlie' => 'c'
);

So I tried this, after reading about variable variables in the PHP documentation

foreach($names as $k=>$v){
${'chart' . $v} ='<img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/'.$k.'.png" alt ="" />';
}

And this works.

My simple question is - is this good / acceptable practice? I explained my method to a more experienced programmer and they told me to find another way that didn't include variable variables, because they are bad practice - but I can't think of what's wrong with this, nor how to do it better.

Thoughts?

Gideon
  • 1,878
  • 4
  • 40
  • 71
  • 4
    Why do you even need to create individual variables for each array entry when you have the data in the array itself and can simply access it there? – Mark Baker Nov 26 '14 at 23:06
  • 4
    Hm, why not declare `$charts` as an array & use `$charts[$v] = ..` ? Rule of thumb: variable variables are a bad choice in 99.999% of the cases. – Wrikken Nov 26 '14 at 23:06
  • I agree with both above. Why make your code less readable? If I inherited a project with this kind of variable declaration I would immediately quit and fill out an application at the home depot – Kai Qing Nov 26 '14 at 23:14
  • @Wrikken ok! That seems fine, to my (ignorant) head they seem essentially equivalent, can you expand a bit on why my original method is worse? I've adjusted my code to define a new array $charts now and reference it as you say via $charts[a] etc. Thanks (if you make it into an answer I could accept it) – Gideon Nov 26 '14 at 23:18
  • Yes ok @KaiQing I am just trying to improve, no need to quit your hypothetical job :P – Gideon Nov 26 '14 at 23:19
  • The mean reasons are these: (1) it makes addressing values _harder_ in most cases then accessing array-items (2) it makes tracing back the origin of a variable when trying to fix a bug extremely hard if that variable is never initiated explicitly. – Wrikken Nov 26 '14 at 23:23
  • I'm just looking for an excuse to work at the home depot... but as for why the original is worse, I would say at the most base level it is just unnecessary and makes the code harder to work with - more verbose, and less direct. Can you give a scenario where you have to do it this way instead of either Mark or Wrikken's method? – Kai Qing Nov 26 '14 at 23:26
  • Arrays have many advantages. You can use [`array_map()`](http://php.net/array_map) ([like this](http://codepad.org/eHlMHyil)) and write maintaineable code – kero Nov 26 '14 at 23:31
  • @all thanks for the advice, that all makes good sense to me - I leave a (slightly) better php programmer. If anyone wants to make an answer compiling the advice above for the reputation, I'll accept it! :) – Gideon Nov 26 '14 at 23:32

1 Answers1

0

I would use an array with the $v being the key. It's readable. Even better:

$names = array(
  'a' => 'yourimage1',
  'b' => 'yourimage2',
  'c' => 'yourimage3',
);

function chartImage($img_string = 'your-default-img')
{
    return '<img src="'.$_SERVER["DOCUMENT_ROOT"].'/output/pdf/charts/'.$img_string.'.png" alt ="" />';
}

// somewhere in your code

print chartImage($names['a']); // or
<?= chartImage($names['a']); ?>

I would do it this way because it's easy to see what you are doing. Better readability.

Variable variables are bad practice when you have defined data. There's no reason to use it. Variable variables are used when you are dealing with data that is unknown. Since you know and predefine variables in a sequence, variable variables adds not enhancements and can make it very confusing to a second set of eyes when viewing the code.

A good example of using variable variables is in surveys. You're not sure how many validations there will be or answers and so you would code variables that hold variables data. When I created a survey system, I created variables that held variable data of arrays working on the validation types and input types. It comes down to context and fixing a solution. Using variable variables in your context is not fixing a solution.

Paul Carlton
  • 2,785
  • 2
  • 24
  • 42