[英]Printing php variables in html
I've been tying myself in knots a bit trying to figure out how to print a couple of PHP variables into some HTML. 我一直在努力使自己陷入困境,试图弄清楚如何将一些PHP变量打印到一些HTML中。 I just can't seem to get the single or double quotes correct. 我似乎无法正确理解单引号或双引号。
I am pulling images from the 500px API and I want to render them into a bootstrap carousel. 我正在从500px API中提取图像,并且希望将其渲染到引导轮播中。 The variables I want to use are $photo->name
(which I have put in between the heading tags below and $photo->url
which I want to put in instead of the placehold.it url. Everything I have tried gives me an Parse error: syntax error, unexpected T_VARIABLE error however. 我要使用的变量是$photo->name
(我将其放在下面的标题标签和$photo->url
,而不是placehold.it url中。我尝试的所有操作都给了我一个解析错误:语法错误,但是意外的T_VARIABLE错误 。
Can anyone help? 有人可以帮忙吗?
<?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++;
}
?>
There are several ways, using "echo", including: 有多种使用“ 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.';
So, you might consider changing your code to: 因此,您可以考虑将代码更改为:
<?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++;
}
?> ?>
Try this 尝试这个
<?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\')"...
使用\\逃逸
Use echo
and paste html5 code between double quotes. 使用echo
并在双引号之间粘贴html5代码。 Example: 例:
echo "<div id='mydiv'>$phpvar</div>";
or concatenate vars with dots: 或用点将var连接起来:
echo "<div id='mydiv'>".$phpvar."</div>";
Sometimes the error depends on single or double quotes. 有时错误取决于单引号或双引号。
Have fun! 玩得开心! (: (:
You are having an escaping issue with strings, please observe the following examples: 您遇到字符串转义的问题,请注意以下示例:
$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++;
}
?>
If you want to add a variable to print or echo, you must close the quotes and add a point at the start and the end. 如果要添加变量以进行打印或回显,则必须关闭引号,并在起点和终点处添加一个点。 For example: 例如:
<?php
$and = "and";
echo "Hello ".$and." how are you?"; //prints Hello and how are you
?>
edit: not I noticed that you have a quotes between url()
. 编辑:不是我没有注意到您在url()
之间加了引号。 The PHP thinks that the string stops there. PHP认为字符串在那里停止。 You must add a left-slash: \\
before it if you want that the next letter after it will be a part of the string. 如果希望后面的下一个字母成为字符串的一部分,则必须在其前面添加一个左斜杠: \\
。 for example: 例如:
<?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!".
?>
in your case: 在您的情况下:
<?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.