php (complex) dynamic variable name construction
<p><strong>I've got this problem when trying to construct a variable name</strong> 
(which should output a corresponding array element.)</p>

<p>I start with setting the variables for testing purposes, normally the piece variable would hold the first element of an array $piece.</p>

<pre><code>$wordnumber = 0;
$piece[0] = "forever";
</code></pre>

<p>I later echo them right before my problem to see if they're still ok.</p>

<pre><code>echo "$piece[0]";
echo "$wordnumber";
</code></pre>

<p>The output is ok.</p>

<pre><code>forever0
</code></pre>

<p>But then comes the problem, as I'm trying to make a function that automatically handles every single array element, so I want it to construct the next corresponding variable every time. <strong>However somehow it has no value</strong> after construction.</p>

<pre><code>$name = ${'piece[' . $wordnumber . ']'};
echo "$name";
</code></pre>

<p>outputs nothing...</p>

<p>I've tried a lot of different formatting, I really don't know why I'm failing so hard here.
The code isn't part of any function right now btw.</p>

<p><strong>Update:
$name = $piece[$wordnumber] solves the problem
I'm curious though why my previous formatting didn't work as expected.</strong></p>

<p><strong>Update: Question solved by André, the problem was that $piece[0] wasn't actually part of an array. So $piece was the actual variable. After storing an actual array $piece = array("Redish", "Yellow", "Green"); at start and using global $piece; in my function everything started working like a charm.</strong></p>
