使用回显创建 amp-html 代码时无法发现 PHP 语法错误
Trouble finding PHP syntax error using echo to create amp-html code
我正在尝试使用 PHP
回显一些 amp-html 代码
<?php
echo "<section>";
echo "<h3>$title[$i]</h3>";
echo "<amp-video controls width="560" height="315" layout="responsive">";
echo "<source src="https: //example.com/videos/";
echo $filename[$i];
echo "_HD.mp4 type="video / mp4 / > ";
echo " < sourcesrc = "https://example.com/videos/";
echo $filename[$i];
echo '"__HD.webm" type="video/webm"/>';
echo "<div fallback><p>This browser does not support the video element.</p></div>";
echo "</amp-video>";
echo "</section>";
?>
我一直找不到语法错误,我错过了什么?有没有比使用 echo 更简单的方法?
更新:
我已经根据建议编辑了代码,但它仍然抛出一个空白页面:这是完整的代码:
<?php
// Create connection
$con=mysqli_connect("localhost","xx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT id,title,filename FROM videos";
$result=mysqli_query($con, $sql);
$i=1;
while($row=mysqli_fetch_assoc($result))
{
$id[$i] = $row['id'];
$title[$i] = $row['title'];
$filename[$i] = $row['filename'];
$i++;
}
// Loop through the results from the database
for ($i = 1; $i <=count($id); $i++)
{
?>
<section>
<h3><?php echo $title[$i];?></h3>
<amp-video controls width="560" height="315" layout="responsive">
<source src="https: //example.com/videos/<?php echo $filename[$i];?>_HD.mp4" type="video/mp4" />
<source src="https://example.com/videos/<?php echo $filename[$i];?>__HD.webm" type="video/webm" />
<div fallback><p>This browser does not support the video element.</p></div>
</amp-video>
</section>
}
</amp-accordion>
确实是语法的使用问题。您应该尝试做的是在回显 html 时使用单引号,而您可能在 html 中添加的任何其他内容都使用双引号。
<?php
echo '<section>';
echo '<h3>$title[$i]</h3>';
echo '<amp-video controls width="560" height="315" layout="responsive">';
echo '<source src="https://example.com/videos/'.$filename[$i].'_HD.mp4" type="video / mp4 / >"';
echo '<source src="https://example.com/videos/'.$filename[$i].'__HD.webm" type="video/webm"/>"';
echo '<div fallback><p>This browser does not support the video element.</p></div>';
echo '</amp-video>';
echo '</section>';
?>
这样更容易发现语法错误,因为页面要么显示它必须显示的内容,要么显示代码。
由于您的代码中的实际 PHP 输出如此之少,因此退出 PHP 以输出 HTML.
可能会更容易且更具可读性
// Create connection
$con=mysqli_connect("localhost","xx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT id,title,filename FROM videos";
$result=mysqli_query($con, $sql);
while($row=mysqli_fetch_assoc($result)) {
?>
<section>
<h3><?php echo $row['title']; ?></h3>
<amp-video controls width="560" height="315" layout="responsive">
<source src="https: //example.com/videos/<?php echo $row['filename']; ?>_HD.mp4" type="video/mp4" />
<source src="https://example.com/videos/<?php echo $row['filename']; ?>__HD.webm" type="video/webm" />
<div fallback><p>This browser does not support the video element.</p></div>
</amp-video>
</section>
<?php
} // end of while
?>
</amp-accordion>
<?php
我正在尝试使用 PHP
回显一些 amp-html 代码<?php
echo "<section>";
echo "<h3>$title[$i]</h3>";
echo "<amp-video controls width="560" height="315" layout="responsive">";
echo "<source src="https: //example.com/videos/";
echo $filename[$i];
echo "_HD.mp4 type="video / mp4 / > ";
echo " < sourcesrc = "https://example.com/videos/";
echo $filename[$i];
echo '"__HD.webm" type="video/webm"/>';
echo "<div fallback><p>This browser does not support the video element.</p></div>";
echo "</amp-video>";
echo "</section>";
?>
我一直找不到语法错误,我错过了什么?有没有比使用 echo 更简单的方法?
更新: 我已经根据建议编辑了代码,但它仍然抛出一个空白页面:这是完整的代码:
<?php
// Create connection
$con=mysqli_connect("localhost","xx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT id,title,filename FROM videos";
$result=mysqli_query($con, $sql);
$i=1;
while($row=mysqli_fetch_assoc($result))
{
$id[$i] = $row['id'];
$title[$i] = $row['title'];
$filename[$i] = $row['filename'];
$i++;
}
// Loop through the results from the database
for ($i = 1; $i <=count($id); $i++)
{
?>
<section>
<h3><?php echo $title[$i];?></h3>
<amp-video controls width="560" height="315" layout="responsive">
<source src="https: //example.com/videos/<?php echo $filename[$i];?>_HD.mp4" type="video/mp4" />
<source src="https://example.com/videos/<?php echo $filename[$i];?>__HD.webm" type="video/webm" />
<div fallback><p>This browser does not support the video element.</p></div>
</amp-video>
</section>
}
</amp-accordion>
确实是语法的使用问题。您应该尝试做的是在回显 html 时使用单引号,而您可能在 html 中添加的任何其他内容都使用双引号。
<?php
echo '<section>';
echo '<h3>$title[$i]</h3>';
echo '<amp-video controls width="560" height="315" layout="responsive">';
echo '<source src="https://example.com/videos/'.$filename[$i].'_HD.mp4" type="video / mp4 / >"';
echo '<source src="https://example.com/videos/'.$filename[$i].'__HD.webm" type="video/webm"/>"';
echo '<div fallback><p>This browser does not support the video element.</p></div>';
echo '</amp-video>';
echo '</section>';
?>
这样更容易发现语法错误,因为页面要么显示它必须显示的内容,要么显示代码。
由于您的代码中的实际 PHP 输出如此之少,因此退出 PHP 以输出 HTML.
可能会更容易且更具可读性 // Create connection
$con=mysqli_connect("localhost","xx","xxxx","xxxx");
// Check connection
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//Get number of rows
$sql="SELECT id,title,filename FROM videos";
$result=mysqli_query($con, $sql);
while($row=mysqli_fetch_assoc($result)) {
?>
<section>
<h3><?php echo $row['title']; ?></h3>
<amp-video controls width="560" height="315" layout="responsive">
<source src="https: //example.com/videos/<?php echo $row['filename']; ?>_HD.mp4" type="video/mp4" />
<source src="https://example.com/videos/<?php echo $row['filename']; ?>__HD.webm" type="video/webm" />
<div fallback><p>This browser does not support the video element.</p></div>
</amp-video>
</section>
<?php
} // end of while
?>
</amp-accordion>
<?php