php string with variable code example
Example 1: php variable in string
$a = '12345';
// This works:
echo "qwe{$a}rty"; // qwe12345rty, using braces
echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used
// Does not work:
echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
echo "qwe$arty"; // qwe, because $a became $arty, which is undefined
Example 2: add more data to variable php
$a = "Hello ";
$a .= "World!";
Example 3: php concatenate variables to function
public function getAvg(string $var) {
$min = $this->{$var._min};
$max = $this->{$var._max};
$avg = (($min + $max) / 2);
return round($avg);
}
Example 4: PHP Variable in String
phpCopy
<?php
$taste = "ie";
echo sweet.$taste;
?>