[英]Printing php variables in html
我一直在努力使自己陷入困境,试图弄清楚如何将一些PHP变量打印到一些HTML中。 我似乎无法正确理解单引号或双引号。
我正在从500px API中提取图像,并且希望将其渲染到引导轮播中。 我要使用的变量是$photo->name
(我将其放在下面的标题标签和$photo->url
,而不是placehold.it url中。我尝试的所有操作都给了我一个解析错误:语法错误,但是意外的T_VARIABLE错误 。
有人可以帮忙吗?
<?php
$i = 0;
$len = count($obj);
foreach ($obj->photos as $photo){
if ($i==0) {
print '<div class="item active">
<div class="fill" style="background-image:url('http://placehold.it/1900x1080&text=Slide One');">
</div>
<div class="carousel-caption">
<h1>$photo->name</h1>
</div>
</div>';
} else {
print "hello 2";
}
$i++;
}
?>
有多种使用“ echo”的方法,包括:
$world = "world";
#If you wrap your statement in double-quotes, then:
echo "Hello $world, nice to meet you.";
#If you wrap your echo statement in single-quotes, then:
echo 'Hello '.$world.', nice to meet you.';
因此,您可以考虑将代码更改为:
<?php
$url = 'http://placehold.it/1900x1080&text=Slide One';
$i = 0;
$len = count($obj);
foreach ($obj->photos as $photo){
if ($i==0) {
print '<div class="item active">
<div class="fill" style="background-image:url(\''.$url.'\');">
</div>
<div class="carousel-caption">
<h1>'.$photo->name.'</h1>
</div>
</div>';
} else {
print "hello 2";
}
$i++;
}
?>
尝试这个
<?php
$i = 0;
$len = count($obj);
foreach ($obj->photos as $photo){
if ($i==0) {
print '<div class="item active">
<div class="fill" style="background-image:url(\'http://placehold.it/1900x1080&text=Slide One\');">
</div>
<div class="carousel-caption">
<h1>'.$photo->name.'</h1>
</div>
</div>';
} else {
print "hello 2";
}
$i++;
}
?>
..."background-image:url(\'http://placehold.it/1900x1080&text=Slide One\')"...
使用\\逃逸
使用echo
并在双引号之间粘贴html5代码。 例:
echo "<div id='mydiv'>$phpvar</div>";
或用点将var连接起来:
echo "<div id='mydiv'>".$phpvar."</div>";
有时错误取决于单引号或双引号。
玩得开心! (:
您遇到字符串转义的问题,请注意以下示例:
$variable = 'my string';
echo $variable ; // my string
echo '$variable '; // $variable
echo "$variable "; // my string
$variable = 'my st'ring'; // ERROR
$variable = 'my st\'ring'; // escaped properly
echo $variable ; // my st'ring
echo '$variable '; // $variable
echo "$variable "; // my st'ring
<?php
$i = 0;
$len = count($obj);
foreach ($obj->photos as $photo){
if ($i==0) {
print "<div class='item active'>
<div class='fill' style='background-image:url(\"http://placehold.it/1900x1080&text=Slide One\");'>
</div>
<div class='carousel-caption'>
<h1>$photo->name</h1>
</div>
</div>";
} else {
print "hello 2";
}
$i++;
}
?>
如果要添加变量以进行打印或回显,则必须关闭引号,并在起点和终点处添加一个点。 例如:
<?php
$and = "and";
echo "Hello ".$and." how are you?"; //prints Hello and how are you
?>
编辑:不是我没有注意到您在url()
之间加了引号。 PHP认为字符串在那里停止。 如果希望后面的下一个字母成为字符串的一部分,则必须在其前面添加一个左斜杠: \\
。 例如:
<?php
echo "My teacher said "You are not listening!"."; // Even SO's editor thinks that "You are not listening!" isn't a part of the string and adds a color. It will return an erro.
// the right way is:
echo "My teacher said \"You are not listening!\"."; // Here you can see that the editor doesn't adds color to whatever in the quotes because it know that is is a part of the string. It prints My teacher said "You are not listening!".
?>
在您的情况下:
<?php
$i = 0;
$len = count($obj);
foreach ($obj->photos as $photo){
if ($i==0) {
print '<div class="item active">
<div class="fill" style="background-image:url(\'http://placehold.it/1900x1080&text=Slide One\');">
</div>
<div class="carousel-caption">
<h1>'.$photo->name.'</h1>
</div>
</div>';
} else {
print "hello 2";
}
$i++;
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.