我正在尝试自学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>
Grade - A
</li>
<li>
<a href="url" target="_blank">Week 2</a>
Not yet graded
</li>
<!--
<li>
<a href="url" target="_blank">Week 3</a>
Grade - A
</li>
<li>
<a href="url" target="_blank">Week 5</a>
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> Grade - ', $grade_letter, '</li>';
}
?>
我尝试调用函数并在其中包含变量,以便填充相应的部分。
assignment($skydoc = 'url', $week_no = '2', $grade_letter = '?')
我在同一个文件中尝试用于测试目的,但是当我在服务器上尝试时,我得到的是HTML
<li>
<a href="" target="_blank">Week </a>
Grade -
</li>
提前谢谢你。如有任何疑问或需要澄清,请告知我们。
答案 0 :(得分:1)
...接近
function assignment($skydoc = '', $week_no = '', $grade_letter = ''){
echo '<li>
<a href="'.$skydoc.'" target="_blank">Week '.$week_no.'</a>
Grade - '.$grade_letter.'
</li>';
}
assignment('url', '2', '?');
答案 1 :(得分:1)
你的功能有点不正确。函数声明应该包含您传递给它的变量的占位符。
function assignment($skydoc = 'url', $week_no = '2', $grade_letter = '?')
{
echo '<li>
<a href="',$skydoc,'" target="_blank">Week ',$week_no,'</a>
Grade - ',$grade_letter,'
</li>';
}
然后,当您想要调用该函数时,您只需调用以下内容
assignment('value', 1, 'A');
如果您按如下方式调用赋值,那么您将在变量中放置默认值。
assignment(); // url, 2, ?