PHP – associative array value in double quoted string

To print associative array value in double quoted string in php, the following syntax using curly braces can be used.

{$arr['key1']}

Note that curly braces can be used for a regular variable also when it is followed by a alphanumeric character.

Here is an example.

<?php
$arr = array("key1" => "value1");
echo "value={$arr['key1']}\n";
?>
value=value1
Env: PHP 5.5.9 (Linux)

Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments