[英]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.