DCR DCR - 1 month ago 6
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];
extract($values);


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

Answer

$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

Example:

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.

Comments