我正在尝试在PHP HEREDOC段中使用模板文字。这会导致模板中使用的美元符号出现问题:
echo <<<JS
console.error(`Source type ${source} not supported`);
JS;
给我以下错误:
PHP Notice: Undefined variable: source in example.php on line 2
是否有可能避免PHP将美元符号解析为变量?
$myline = 'add this line';
$input = <<<'JS'
console.error(`Source type ${source} not supported`);
console.log(`%s`);
JS;
$output = sprintf($input,$myline);
echo $output
echo <<<JS
console.error(`Source type \${source} not supported`);
JS;