使用 PHP & HTML 的正确方法是什么
What's the correct method using PHP & HTML
我创建了许多需要 php、html 和 css 的网站。他们都工作得很好,但我现在在想,如果我一直以错误的方式做这件事。
通常,我首先构建站点模板和样式,然后编写 php。
然后我只在 HTML 中包含 php 函数等。
例如,查看 table 列出了一些帖子 (index.php):
<div class="posts">
<h1 class="content-title">New Posts</h1>
<?php
$rows = list_posts();
while ($row=mysql_fetch_array($rows)) {
echo "
<div>
<img class='post-avatar' src='img/icon_newsletter.jpg'>
<h2 class='post-title'>".$row['post_title']."</h2>
<p class='post-meta'>
Από <a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
</p>
<p>
".substr($row['post_body'], 0, 180)."...
</p>
<img class='post-icon-read' src='img/read.png' /> <a href='viewpost.php?id=".$row['post_id']."'>Συνέχεια άρθρου..</a>
</div>
";
}
?>
</div>
还有改进的空间。
将 HTML 保持在 PHP 之内。这不是个人意见,有技术原因。
当你这样做时:
html
<?php PHP ?>
html
<?php ?>
html
<?php PHP ?>
html
当您在 HTML 模式和 PHP 模式之间来回切换时,PHP 会产生开销。它让你看起来像一个 Word Press Hack。
一些事情:
- Heredoc 语法
- 数值数组
- 转义引号
从基本模板开始:
echo <<<EOT
// Top of page
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
}
echo <<<EOT
// bottom of page
EOT;
使用 MYSQL_NUM
的原因是列值可以在不连接的情况下使用。
而不是
<a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
你可以这样做:
$comment = total_comments($row[2]) ;
echo "<a href=\"#\">$row[0]</a>$row[1]<a class=\"post-views\" href=\"viewpost.php?id=$row[2]#comments\">Comments: $comment</a>";
反斜杠会转义双引号内的双引号。 Heredoc 中不需要转义引号。
单维数值数组可以在双引号和 Heredoc 中使用。
页面顶部:
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Edit Rotation Diet</title>
<style type="text/css">
</style></head>
<body><div id="page">
EOT;
页面底部:
echo <<<EOT
<script type="text/javascript">//<![CDATA[
//]]>
</script></body></html>
EOT;
假设页面内容是 table
<body><div id="page"><table>
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
echo <<<EOT
<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
EOT;
}
echo <<<EOT
</table>
<script type="text/javascript">//<![CDATA[
HTML 和 PHP 的缩进是错误的。您应该考虑当有人查看源代码时 HTML 会是什么样子。在大多数 Word Press 页面上查看源代码。看起来很垃圾。
echo 和第一个 html 之间的空行给你换行。
当你回显 HTML 时,当你查看源代码(如 Word Press)时,它最终看起来真的很糟糕。
就个人而言,我不喜欢四个 space 缩进。在您不知不觉中,您的代码已隐藏在右边界之外。我用了两个 spaces.
我觉得这很丑而且浪费垂直 space:
if($x = 1)
{
echo '<p>one</p>';
}
else
}
echo '<p>two</p>'
}
我认为这很清楚。没有过多的垂直滚动/
if($x = 1){
echo '<p>one</p>';
}
else{
echo '<p>two</p>
}
我创建了许多需要 php、html 和 css 的网站。他们都工作得很好,但我现在在想,如果我一直以错误的方式做这件事。 通常,我首先构建站点模板和样式,然后编写 php。 然后我只在 HTML 中包含 php 函数等。 例如,查看 table 列出了一些帖子 (index.php):
<div class="posts">
<h1 class="content-title">New Posts</h1>
<?php
$rows = list_posts();
while ($row=mysql_fetch_array($rows)) {
echo "
<div>
<img class='post-avatar' src='img/icon_newsletter.jpg'>
<h2 class='post-title'>".$row['post_title']."</h2>
<p class='post-meta'>
Από <a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
</p>
<p>
".substr($row['post_body'], 0, 180)."...
</p>
<img class='post-icon-read' src='img/read.png' /> <a href='viewpost.php?id=".$row['post_id']."'>Συνέχεια άρθρου..</a>
</div>
";
}
?>
</div>
还有改进的空间。
将 HTML 保持在 PHP 之内。这不是个人意见,有技术原因。
当你这样做时:
html
<?php PHP ?>
html
<?php ?>
html
<?php PHP ?>
html
当您在 HTML 模式和 PHP 模式之间来回切换时,PHP 会产生开销。它让你看起来像一个 Word Press Hack。
一些事情: - Heredoc 语法 - 数值数组 - 转义引号
从基本模板开始:
echo <<<EOT
// Top of page
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
}
echo <<<EOT
// bottom of page
EOT;
使用 MYSQL_NUM
的原因是列值可以在不连接的情况下使用。
而不是
<a href='#'>".$row['post_user']."</a> | ".$row['post_date']." | <a class='post-views' href='viewpost.php?id=".$row['post_id']."#comments'>Comments: ".total_comments($row['post_id'])."</a>
你可以这样做:
$comment = total_comments($row[2]) ;
echo "<a href=\"#\">$row[0]</a>$row[1]<a class=\"post-views\" href=\"viewpost.php?id=$row[2]#comments\">Comments: $comment</a>";
反斜杠会转义双引号内的双引号。 Heredoc 中不需要转义引号。
单维数值数组可以在双引号和 Heredoc 中使用。
页面顶部:
echo <<<EOT
<!DOCTYPE html>
<html lang="en"><head><title>Page Title</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><title>Edit Rotation Diet</title>
<style type="text/css">
</style></head>
<body><div id="page">
EOT;
页面底部:
echo <<<EOT
<script type="text/javascript">//<![CDATA[
//]]>
</script></body></html>
EOT;
假设页面内容是 table
<body><div id="page"><table>
EOT;
while ($row=mysql_fetch_array($rows), MYSQL_NUM) {
echo <<<EOT
<tr>
<td>$row[0]</td>
<td>$row[1]</td>
<td>$row[2]</td>
<td>$row[3]</td>
</tr>
EOT;
}
echo <<<EOT
</table>
<script type="text/javascript">//<![CDATA[
HTML 和 PHP 的缩进是错误的。您应该考虑当有人查看源代码时 HTML 会是什么样子。在大多数 Word Press 页面上查看源代码。看起来很垃圾。
echo 和第一个 html 之间的空行给你换行。
当你回显 HTML 时,当你查看源代码(如 Word Press)时,它最终看起来真的很糟糕。
就个人而言,我不喜欢四个 space 缩进。在您不知不觉中,您的代码已隐藏在右边界之外。我用了两个 spaces.
我觉得这很丑而且浪费垂直 space:
if($x = 1)
{
echo '<p>one</p>';
}
else
}
echo '<p>two</p>'
}
我认为这很清楚。没有过多的垂直滚动/
if($x = 1){
echo '<p>one</p>';
}
else{
echo '<p>two</p>
}