DCR DCR - 5 months ago 26
HTML Question

php array syntax ${ is confusing me

I create a $values array and then extract the elements into local scope.

$values['status'.$i] = $newStatus[$i];

When I render an html page. I'm using the following

<?php if(${'status'.$i} == 'OUT'){ ?>

but am confused by what the ${ is doing and why $status.$i won't resolve


$status.$i means

take value of $status variable and concatenate it with value of $i variable.

${'status'.$i} means

take value of $i variable, append id to 'status' string and take value of a variable 'status'.$i


With $i equals '2' and $status equals 'someStatus':

  • $status.$i evaluated to 'someStatus' . '2', which is 'someStatus2'

  • ${'status'.$i} evaluated to ${'status'.'2'} which is $status2. And if $status2 is defined variable - you will get some value.