PHP/String/String Interpolation
Содержание
Adding "s" to the end of the variable name
<?php
$food = "grapefruit";
print "These ${food}s aren"t ripe yet.";
print "These {$food}s aren"t ripe yet.";
?>
Embedded Conditionals
<?php
$value = 2;
if($value > 0) {
if($value <= 10) {
echo "The $value variable is between 1 and 10.";
} else {
if($value <= 20) {
echo "The $value variable is between 1 and 20.";
} else {
echo "The $value variable is greater than 20";
}
}
}
?>
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";
?>
Embed variables directly into strings
<?php
$a = 10;
$b = 15.7;
echo "The value of \$a is $a and the value of \$b is $b\n";
echo "The value of \$a is $a and the value of \$b is $b\n";
?>
Interpolating with curly braces
<?
$preparation = "Braise";
$meat = "Beef";
print "{$preparation} and $meat";
?>