PHP函数echo与HTML字符串

时间:2014-01-15 10:09:04

标签: php

我的HTML正文中有这个功能:

      <ul>
        <!-- Dynamic insertion of projects with PHP function -->
        <?php projectGallery(); ?>
      </ul>

这是PHP函数本身:

// This function shows a list of projects on our home page
function projectGallery(){
    include 'includes/config.php';
    $result = mysqli_query($con,"SELECT * FROM project");

    while($row = mysqli_fetch_array($result)) {

        echo "<li>
                  <a href='#' data-reveal-id='".$row['project_id']."' data-animation='fade'>
                    <div class='project'>
                      <img class='project_image' src='images/upload/".$row['project_afbeelding']. "' />
                        <h2 class='title'>".$row['project_titel']."</h2>
                    </div>
                  </a>
             </li>" ;

    };
};

好吧,如果你还在阅读,尽管丑陋...我的问题是,是否有更清晰的方法来编写这个回声部分?没有无尽的HTML字符串。

7 个答案:

答案 0 :(得分:2)

就个人而言,我总是这样做。当有一个干净的HTML源时,我

function projectGallery(){
 include 'includes/config.php';
 $result = mysqli_query($con,"SELECT * FROM project");

 while($row = mysqli_fetch_array($result)) { ?>

 <li><a href="#" data-reveal-id="<?php echo $row['project_id']; ?>" data-animation="fade">
  <div class="project">
   <img class="project_image" src="images/upload/<?php echo $row['project_afbeelding']; ?>" />
   <h2 class="title"><?php echo $row['project_titel']; ?></h2>
  </div>
 </a>
</li><?php
} ?>

这样,您的IDE将分别识别HTML和PHP内容,从而减少出错的可能性。然而,如果扩展,这是一个混乱的来源。但正如我所说,对我来说,这是很多东西的源代码。

答案 1 :(得分:1)

您有几个选择。第一个是简单地结束PHP标记:

function projectGallery(){
include 'includes/config.php';
$result = mysqli_query($con,"SELECT * FROM project");

    while($row = mysqli_fetch_array($result))
    {
    ?>

    <li>
        <a href='#' data-reveal-id='<?php echo $row['project_id']; ?>' data-animation='fade'>

            <div class='project'>
                <img class='project_image' src='images/upload/<?php echo $row['project_afbeelding']; ?>' />
                <h2 class='title'><?php echo $row['project_titel']; ?></h2>
            </div>
        </a>
    </li>

    <?php
    };
};

另一种方法是创建一个回显的字符串,以便将来进行更多的编辑:

function projectGallery(){
include 'includes/config.php';
$result = mysqli_query($con,"SELECT * FROM project");
$output = '';
    while($row = mysqli_fetch_array($result))
    {
    $output .= "<li>";
    $output .= "<a href='#' data-reveal-id='".$row['project_id']."' data-animation='fade'>";

    $output .= "<div class='project'>";
    $output .= "<img class='project_image' src='images/upload/".$row['project_afbeelding']. "' />";
    $output .= "<h2 class='title'>".$row['project_titel']."</h2>";
    $output .= "</div>";

    $output .= "</a>";
    $output .= "</li>";

    };
echo $output;
};

两者都有其优点,两者都更易于管理和适应日期

答案 2 :(得分:1)

在PHP模式下,你根本不应该回复html代码。 这使得你的代码难以阅读,你疯狂地放置'或'而你的IDE(如果你有的话)无法帮助你完成HTML自动完成。

你应该总是这样:

<?php while($row = mysqli_fetch_array($result)) { ?>
    <li>
        <a href="#" data-reveal-id="<?=$row['project_id']?>" data-animation="fade">
            <div class="project">
                <img class="project_image" src="images/upload/<?=$row['project_afbeelding']?>" />
                <h2 class="title"><?=$row['project_titel']?></h2>
            </div>
        </a>
    </li>
<?php } ?>

因此,只需在需要时再次打开和关闭PHP,并将其关闭一段时间。

与以下内容完全相同:

<?="123"?>

但请注意,某些网络服务器配置为短标签

答案 3 :(得分:0)

试试吧

在html中:

<?php echo projectGallery(); ?>

在功能中:

return "<li><a href='#' data-reveal-id='".$row['project_id']."' data-animation='fade'><div class='project'><img class='project_image' src='images/upload/".$row['project_afbeelding']. "' /><h2 class='title'>".$row['project_titel']."</h2></div></a></li>" ;

答案 4 :(得分:0)

function projectGallery(){
include 'includes/config.php';
$result = mysqli_query($con,"SELECT * FROM project");

while($row = mysqli_fetch_array($result)) {
?>
   <li>
              <a href='#' data-reveal-id='<?=$row['project_id']?>' data-animation='fade'>
                <div class='project'>
                  <img class='project_image' src='images/upload/<?=$row['project_afbeelding']?>' />
                    <h2 class='title'><?=$row['project_titel']?></h2>
                </div>
              </a>
         </li>

答案 5 :(得分:0)

您可以创建一个函数来获取文件和值数组,并将这些值提取到受限上下文中的变量中,包含文件,捕获结果输出,并以字符串形式返回:

function render_with_values($file, array $variables = null) {

     if (!file_exists($file)) { 
         throw new Exception('Template cant be found.'); 
     }

     if ($variables !== null)
         extract($variables); //Extract array values to variables; array('foo' => 'bar') will result to $foo = 'bar'; and will be avaiable in the template you are including below but will not leak outside a function call.

     ob_start(); //Supress output

     include $file; //File 

     $content = ob_get_contents(); //Retrieve supressed output.
     ob_end_clean(); //End output supression.

     return $content;

}

将您的代码更改为:

function projectGallery(){
    include 'includes/config.php';
    $result = mysqli_query($con,"SELECT * FROM project");

    while($row = mysqli_fetch_array($result)) {

        echo render_with_values('path/to/gallery_item_template.php', $result)

    };
};

path/to/gallery_item_template.php

<li>
    <a href="#" data-reveal-id="<?php echo $project_id; ?>" data-animation="fade">
        <div class="project">
            <img class="project_image" src="images/upload/<?php echo $project_afbeelding; ?>" />
            <h2 class="title"><?php echo $project_title; ?></h2>
        </div>
    </a>
</li>

但严重的是,请阅读MVC以及关注点分离的概念。您可以查看Twig之类的模板引擎。

答案 6 :(得分:0)

不一定是最漂亮但尚未提及,最快的方式(性能明智)在PHP中连接一些字符串的方法是加入一个数组。

function projectGallery(){

    // You should probably require that, not merely include.
    require_once 'includes/config.php';

    $result = mysqli_query($con, 'SELECT * FROM project');

    $buffer = array();

    while($row = mysqli_fetch_array($result)) {
        $buffer[] = '<li>';
        // You're using double quotes, so you can put variables directly
        // in your string, no need to concatenate with a dot. The missing
        // quotes in the array index key (project_id) are intentionnal
        // and valid PHP.
        $buffer[] = "<a href=\"#\" data-reveal-id=\"$row[project_id]\" data-animation=\"fade\">";
        $buffer[] = '<div class="project">';
        ...
        $buffer[] = '<li>';
    };

    echo implode('', $bufffer);
}

您还应该记住单引号和双引号之间的区别。单引号处理得更快,因为它们不能包含变量。所以,如果你选择使用双引号,你最好在其中加入一些变量,而不是用点运算符连接它们。