Re: Help with Javascript: naming variables using other variables



On 06/11/2005 17:54, X l e c t r i c wrote:

sagejoshua wrote:

[snip]

<?php
$var1 = "foo";
${$var1.bar} = "great!";
print $foobar;
// Outputs "great!"
?>

[snip]

Are you sure that outputs "great!" ?

[snip]

To elaborate on what Leif wrote, the variable variable constructed above is made possible through string concatenation, similar to

  var variable = 'foo';

  this[variable + 'bar'] = 'Great!';  /* foobar = 'Great!'; */

in ECMAScript. However, rather than using a string literal, the OP has written in bar what would normally considered a constant. Indeed, if STRICT notices are enabled in PHP, this will be reported in the error log.

It would be better written as:

  <?php
  $variable = 'foo';
  ${$variable . 'bar'} = 'Great!';
  print $foobar;  /* Great! */
  ?>

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
.