Search code examples
phparraysvariablesquotes

How do I insert a variable into a PHP array?


I have looked for some responses on the web, but none of them are very accurate.

I want to be able to do this:

$id = "" . $result ["id"] . "";
$info = array('$id','Example');

echo $info[0];

Is this possible in any way?


Solution

  • What you need is (not recommended):

    $info = array("$id",'Example'); // variable interpolation happens in ""
    

    or just

    $info = array($id,'Example'); // directly use the variable, no quotes needed
    

    You've enclosed the variable inside single quotes and inside single quotes variable interpolation does not happen and '$id' is treated as a string of length three where the first character is a dollar.