我一直试图弄清楚如何将一些PHP变量打印成某些HTML。我似乎无法使单引号或双引号正确。
我从500px API中提取图像,我想将它们渲染成一个bootstrap轮播。我想要使用的变量是$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++;
}
?>
答案 0 :(得分:1)
有几种方法,使用“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++;
}
&GT;
答案 1 :(得分:0)
试试这个
<?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++;
}
?>
答案 2 :(得分:0)
..."background-image:url(\'http://placehold.it/1900x1080&text=Slide One\')"...
使用\来逃避quatition
答案 3 :(得分:0)
使用echo
并在双引号之间粘贴html5代码。例如:
echo "<div id='mydiv'>$phpvar</div>";
或用点连接变量:
echo "<div id='mydiv'>".$phpvar."</div>";
有时错误取决于单引号或双引号。
玩得开心! (:
答案 4 :(得分:0)
您遇到字符串转义问题,请注意以下示例:
$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
答案 5 :(得分:0)
<?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++;
}
?>
答案 6 :(得分:0)
如果要添加变量以进行打印或回显,则必须关闭引号并在开头和结尾添加一个点。例如:
<?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++;
}
?>