note 75669 modified in language.variables.variable by danbrown
| From: | danbrown@php.net | Date: | Wed, 24 Jun 2009 16:52:00 +0000 |
| Subject: | note 75669 modified in language.variables.variable by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-157390@lists.php.net to get a copy of this message | ||
One interesting thing I found out: You can concatenate variables and use spaces. Concatenating
constants and function calls are also possible.
<?php
define('ONE', 1);
function one() {
return 1;
}
$one = 1;
${"foo$one"} = 'foo';
echo $foo1; // foo
${'foo' . ONE} = 'bar';
echo $foo1; // bar
${'foo' . one()} = 'baz';
echo $foo1; // baz
?>
This syntax doesn't work for functions:
<?php
$foo = 'info';
{"php$foo"}(); // Parse error
// You'll have to do:
$func = "php$foo";
$func();
?>
Note: Don't leave out the quotes on strings inside the curly braces, PHP won't handle that
graciously.
--was--
One interesting thing I found out: You can use "blank" strings in the curly braces, and
even strings together with variables (as long as you use double quotes or concatenation).
Concatenating constants and function calls are also possible.
<?php
define('ONE', 1);
function one() {
return 1;
}
$one = 1;
${"foo$one"} = 'foo';
echo $foo1; // foo
${'foo' . ONE} = 'bar';
echo $foo1; // bar
${'foo' . one()} = 'baz';
echo $foo1; // baz
?>
This syntax doesn't work for functions:
<?php
$foo = 'info';
{"php$foo"}(); // Parse error
// You'll have to do:
$func = "php$foo";
$func();
?>
Note: Don't leave out the quotes on strings inside the curly braces, PHP won't handle that
graciously.
http://php.net/manual/en/language.variables.variable.php