0

我正在尝试自学 PHP。我一直在寻找如何使这项工作。

我有一个在线作品集,当我收到它们时,我会发布我每周的学校作业和他们的成绩。

我目前有一些看起来像这样的东西:

<?php
$page_title = "MATH203 | Journey to my Oasis";
$grade = "A = 185/185";
$course_title = "Applications of Discrete Mathematics";
$course_description = "Math. A fun four letter word. A lot of people have issues with this subject. I like math. I am not proficient by any stretch of the imagination, but I like it. It is challenging. Having this course after programming, I believe, will allow me to think of it in the manner in which it is intended. The professor is also teaches computer science. This should be interesting.";
?>

<?php include("../includes/header.php");?>
<?php include("../includes/navigation.php");?>
<?php include("../includes/classannounce.php");?>

<div id="content">
    <div id="content_container">
        <div class="one_half">
            <h3>Discussion Board Posts</h3>
            <h4><ul>
                <li>
                    <a href="url" target="_blank">Week 1</a>
                    &nbsp; &nbsp; Grade - A
                </li>
                <li>
                    <a href="url" target="_blank">Week 2</a>
                    &nbsp; &nbsp; Not yet graded
                </li>
                <!--
                    <li>
                    <a href="url" target="_blank">Week 3</a>
                      &nbsp; &nbsp; Grade - A
                    </li>
                    <li>
                    <a href="url" target="_blank">Week 5</a>
                      &nbsp; &nbsp; Grade - B-
                    </li>
                -->
                <li>All links open a new window</li>
            </ul></h4>
        </div>
    </div><!--end content_container-->
</div><!--end content-->
'<?php include("../includes/footer.php");?>
</body>

我想做的是有一个单独的 PHP 文件,我将它包含在头 PHP 文件中,这样它就可以随处可见,只是为了减少代码量。

目前我正在做的只是在我还没有想到的几周内隐藏 html。我使用原始页面作为模板,并随时更改它。我正在尝试更多地合并 PHP,以便我可以更轻松地进行更改,以便我可以尝试尽可能多地学习。我认为通过调用一个函数并用所需的变量填充它,我可以减少所需的代码和所需的时间。

这个单独的文件将具有如下功能:

<?php
function assignment()
{
    $skydoc = '';
    $week_no = '';
    $grade_letter = '';
    echo '<li><a href="', $skydoc, '" target="_blank">Week ', $week_no,
        '</a>&nbsp; &nbsp; Grade - ', $grade_letter, '</li>';
}
?>

我尝试调用该函数并在其中包含变量,以便它将填充适当的部分。

assignment($skydoc = 'url', $week_no = '2', $grade_letter = '?')

为了测试目的,我在同一个文件中尝试了它,但是当我在我的服务器上尝试它时,我在 HTML 中得到的只是这个

<li>
    <a href="" target="_blank">Week </a>
    &nbsp; &nbsp; Grade - 
</li>        

先感谢您。如有更多问题或需要澄清,请告诉我。

4

2 回答 2

1

关闭...

function assignment($skydoc = '', $week_no = '', $grade_letter = ''){
    echo '<li>
                <a href="'.$skydoc.'" target="_blank">Week '.$week_no.'</a>
                &nbsp; &nbsp; Grade - '.$grade_letter.'
              </li>';
}

assignment('url', '2', '?');
于 2013-09-01T02:56:27.240 回答
1

你调用函数有点不正确。函数声明应该有你传递给它的变量的占位符。

 function assignment($skydoc = 'url', $week_no = '2', $grade_letter = '?')
 {
            echo '<li>
                <a href="',$skydoc,'" target="_blank">Week ',$week_no,'</a>
                &nbsp; &nbsp; Grade - ',$grade_letter,'
              </li>';
 }

然后,当您要调用该函数时,只需调用以下内容

assignment('value', 1, 'A');

如果您按如下方式调用分配,那么您放置在变量中的默认值将被分配。

assignment(); // url, 2, ?
于 2013-09-01T03:00:16.260 回答