Embedding numbers and strings into other strings : String Interpolation « String « PHP






Embedding numbers and strings into other strings

 
<?php 
$arr = array(1 => "abc","abc" => 123.5,array(1,2,3)); 
$key = "abc"; 

echo "First value = $arr[1]\n"; 
echo "Second value = $arr[abc]\n"; 
echo "Third value = $arr[2]\n"; 
echo "Third value = $arr[2][2]\n"; 

echo "Second value = ${arr['abc']}\n"; 
echo "Second value = ${arr["abc"]}\n"; 
echo "Second value = ${arr[$key]}\n"; 
?> 


<?php 
$arr = array( 
    "abc" => "abc", 
    "def" => 123.5, 
    "ghi" => array(1,2,3) 
); 
$key = "abc"; 
$obj = (object) $arr; 

echo "First value = $obj->abc\n"; 
echo "Second value = $obj->def\n"; 
echo "Third value = $obj->ghi\n"; 
?>
  
  








Related examples in the same category

1.Adding "s" to the end of the variable name
2.Embed variables directly into strings
3.Embedded Conditionals
4.Interpolating with curly braces