php variable in string 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

Here is what the above code is Doing:
1. The first echo statement is using braces to tell PHP that the variable name is $a.
2. The second echo statement is using concatenation to tell PHP that the variable name is $a.
3. The third echo statement is using single quotes, which means that PHP will not parse the variable name.
4. The fourth echo statement is using double quotes, but the variable name is not wrapped in braces. This means that PHP will try to find a variable named $arty, which does not exist.

Similar Posts