I don’t unserstand why PHP doesn’t make replacements in strings containing dollar signs.
Look at the following example:
<?php
error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
$var = 1024;
$str = '$var';
echo $str, '<br>', "$str";
Output is $var $var. Why is it so? Everything is clear with first echo parameter, but I expected that the last parameter will give a different result (1024), because it contains dollar sign encapsulated by double quotes, so it should be interpreted as variable and replaced to 1024. Where am I going wrong?
$strcontains a string with the content of"$var"(no variable replacement, just these very characters). It was created using single quotes, so no variable replacement there.When echoing it using
echo "$str", the variable$strgets replaced with its content, namely the string"$var", thus resulting in your output.The string replacement in double quotes strings does not work recursively! So in order to have
$strreplaced by1024in the second appearance, you have to create$strusing double quotes in the first place.