我知道我不能在回声中使用回声。我的PHP代码是:
echo '<img src="img/image1.jpg"
我想使用php变量作为源代码。有些想法:
echo '<img src="php-code"
答案 0 :(得分:1)
使用。(点)你可以在echo语句中连接php变量。
echo '<img src="'.$src.'" />';
答案 1 :(得分:1)
您有四种选择:
$url = '...';
//1
echo '<img src="' . $url . '">';
//2
echo "<img src='{$url}'>"; //Note that you have to use double quotes
//3
echo '<img src="';
echo $url;
echo '">';
//4
echo '<img src="', $url, '">'; //I would not recommend this one though
答案 2 :(得分:0)
只是跳过第一个回声并在中间用回声写出html
<img src="<?=$src?>">
答案 3 :(得分:0)
所有这些工作
echo '<img src="', $url, '">'; # This works by sending 3 different parameters to echo
echo '<img src="' . $url . '">'; # This works by concatenating 3 strings before echoing
echo '<img src="'; echo $url; echo '">'; # This works by echoing 3 strings in turn
echo "<img src=\"$url\">"; # All of these works by inserting
echo "<img src=\"${url}\">"; # the value of $url in the string
echo "<img src=\"{$url}\">"; # before echoing. " needs to be escaped.
# And finally, this (called HEREDOC) does the same thing as above
# only without ", so that sign is not needed to be escaped
echo <<<FOO
<img src="$url">
<img src="{$url}">
<img src="${url}">
FOO;
答案 4 :(得分:0)
有很多解决方案。
我更喜欢这个:<?php echo '<img src="'.$url.'">'; ?>
$ url代表Image-Url,我猜你知道ist。
你也可以这样做:
例如:<img src="<?php echo $url; ?>">
但我喜欢第一种方法,这是一种简单的方法来输出字符串。