语句“echo”并不会“导致”问题,问题出在新手没有足够的信息,所以我会尝试解释一下。
在php中有四种指定字符串的方式:
由于字符串被单引号括起来,php引擎不会将$variable解释为实际变量,并且你看到的内容将被输出。
$variable = 'random text';
$str = "Hello World. This will be interpreted as $variable";
echo $str; // 输出:Hello World。这将被解释为随机文本
在这个例子中,php会尝试查找一个名为$variable的变量,并使用它的内容在字符串中。
Heredoc语法对于像你想做的事情非常有用 - 你有各种变量、单引号、双引号和转义,而混合这些可能会很麻烦。因此,好的php人员为我们实现了以下语法:
$str = <<<EOF
<img src="$directory/images/some_image.gif" alt='this is alt text' />
<p>Hello!</p>
EOF;
What will happen is that PHP engine will try to interpret variables (and functions, but I won't post examples on how to do that since it's available at php.net), however you wrapped the string with <<
$str = <<<'EOF'
<p>Hello! This wants to be a $variable but it won't be interpreted as one!</p>
EOF;
It's the same as using a single-quoted string - no variable replacements occur, and to specify something as nowdoc - simply wrap the delimiter with single quote characters as shown in the example.
If you are able to understand these four principles, problems with quotes in your string should go away :)
echo sprintf()
而不是简单地使用printf()
? - Wiseguyecho "<img src='" . get_bloginfo('template_url') . "img/" . $f['mainImage'] . ".png' />";
- Eliasdxecho "<img src='".get_bloginfo('template_url')."img/{$f['mainImage']}.png' />";
- Anther