PHP/String/String Interpolation
Содержание
Adding "s" to the end of the variable name
<source lang="html4strict">
<?php
$food = "grapefruit"; print "These ${food}s aren"t ripe yet."; print "These {$food}s aren"t ripe yet."; ?> </source>
Embedded Conditionals
<source lang="html4strict">
<?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"; } } }
?>
</source>
Embedding numbers and strings into other strings
<source lang="html4strict">
<?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"; ?>
</source>
Embed variables directly into strings
<source lang="html4strict">
<?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"; ?>
</source>
Interpolating with curly braces
<source lang="html4strict">
<? $preparation = "Braise"; $meat = "Beef"; print "{$preparation} and $meat"; ?>
</source>