在PHP网站中包含代码的正确方法

时间:2017-05-28 22:45:00

标签: php html

我正在开发一个网站,150个文件,那里有很多代码;至少对我来说,是一个初学者。

在很多地方我重复输出HTML的PHP​​回声,每个回声都有很多PHP变量。我正在考虑不重复这些部分的最佳方法。

鉴于此HTML:

    <main>
        <section>
            <?php
            $query = "SELECT a, b, c, d, e FROM table1";
            $result = mysqli_query($connection,$query);
            while ($row = mysqli_fetch_assoc($result)) {
                $table1_a = $row['a'];
                $table1_b = $row['b'];
                $table1_c = $row['c'];
                $table1_d = $row['d'];
                $table1_e = $row['e'];
                echo 'This code is thirty lines long and appears 
                identical in many places in the website. 
                It uses many variables, like '.$table1_a.','.$table1_b.',
                '.$table1_c.','.$table1_d.','.$table1_e.', and others';
            }
            ?>
        </section>
        <section>
            <?php
            $query = "SELECT a, b, c, d, e FROM table2";
            $result = mysqli_query($connection,$query);
            while ($row = mysqli_fetch_assoc($result)) {
                $table2_a = $row['a'];
                $table2_b = $row['b'];
                $table2_c = $row['c'];
                $table2_d = $row['d'];
                $table2_e = $row['e'];
                echo 'This second code is different to the other, 
                but is thirty lines long as well and is repeated
                in many other places of the website. 
                It uses many variables, like'.$table2_a.',
                '.$table2_b.','.$table2_c.','.$table2_d.',
                '.$table2_e.' and others';
            }
            ?>
        </section>
    </main>

我想象两种方式:包括和功能

使用包含我会写回声 - 没有查询,因为它们都是不同的 - 在文件夹/ includes中的分隔文件中,然后我在页面中调用它们。

包含/ echo_1.php的文件包括:

<?php
    echo 'This second code is different to the other, 
    but is thirty lines long as well and appears identical in 
    many other places of the website. 
    It uses many variables, like '.$table2_a.','.$table2_b.','.$table2_c.',
    '.$table2_d.','.$table2_e.' and others';
?>

函数/ echo_1.php中的函数是:

<?php
    function echo_1($table1_a,$table1_b,$table1_c,$table1_d,$table1_e){
        echo 'This second code is different to the other, but is thirty lines
        long as well and appears identical in many other places of the website.                    
        It uses many variables, like 
        '.$table1_a.','.$table1_b.','.$table1_c.','.$table1_d.',
        '.$table1_e.' and others';
    }
?>

和电话:

    <main>
        <section>
            <?php
            $query = "SELECT a, b, c, d, e FROM table1";
            $result = mysqli_query($connection,$query);
            while ($row = mysqli_fetch_assoc($result)) {
                $table1_a = $row['a'];
                $table1_b = $row['b'];
                $table1_c = $row['c'];
                $table1_d = $row['d'];
                $table1_e = $row['e'];
                //Calling the include from the file echo_1.php
                include 'includes/echo_1.php';
            }
            ?>
        </section>
        <section>
            <?php
            $query = "SELECT a, b, c, d, e FROM table2";
            $result = mysqli_query($connection,$query);
            while ($row = mysqli_fetch_assoc($result)) {
                $table2_a = $row['a'];
                $table2_b = $row['b'];
                $table2_c = $row['c'];
                $table2_d = $row['d'];
                $table2_e = $row['e'];
                //Using the function echo_1 to create the echo
                echo_1($table1_a,$table1_b,$table1_c,$table1_d,$table1_e);

            }
            ?>
        </section>
    </main>

此外,回声有可能是代码的一部分 - 有查询 - 在其他不同的回声之间重复,我认为不重复它们可能很好,可能还有其他包含或其他功能。

使用包含我将对其他文件进行大量调用,这可能会使网站变慢。有了函数,我只打一个电话,但我不知道这是否会更好。哪种选择是最有效的方式,包括或包含功能?任何有关编写可维护代码的建议都将受到欢迎!

<磷>氮

1 个答案:

答案 0 :(得分:1)

有更好的方法可以做到这一点,但为了防止自己重复一些,你可以做出这样的改变:

<main>
    <section>
        <?php
        section('table1');
        ?>
    </section>
    <section>
        <?php
        section('table2');
        ?>
    </section>
</main>

你的功能:

function section( $table ) {
            $query = "SELECT a, b, c, d, e FROM $table";
            $result = mysqli_query($connection, $query);
            while ($row = mysqli_fetch_assoc($result)) {
                $table_a = $row['a'];
                $table_b = $row['b'];
                $table_c = $row['c'];
                $table_d = $row['d'];
                $table_e = $row['e'];
                echo 'This code is thirty lines long and appears 
                identical in many places in the website. 
                It uses many variables, like '.$table_a.','.$table_b.',
                '.$table_c.','.$table_d.','.$table_e.', and others';
            }
}