简体   繁体   中英

Trying to create a PHP function that only utilizes new instance of variable

I am trying to teach myself PHP. I have looked to find out how to make this work.

I have an online portfolio that I am posting my weekly school assignments and their grades when I receive them.

I currently have something that looks like this:

<?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>

What I would like to do is have a separate PHP file that I include in the header PHP file so it goes everywhere with me just to cut down on the amount of code.

Currently what I am doing is just hiding the html for the weeks that I haven't come up to yet. I am using the original page as a template and just changing it as I go. I am trying to incorporate PHP more so that I can make changes easier and so that I can try to learn as much as I can. I figure that by calling a function an populating it with the variables needed I can cut down on the code needed and time required.

This separate file would have a function like:

<?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>';
}
?>

I try calling the function an include the variables in it so that it will fill in the appropriate sections.

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

I try that in the same file for testing purposes but all I get in HTML when I try it on my server is this

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

Thank you in advance. Any more questions or need clarification, please let me know.

close...

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', '?');

your calling the function a bit incorrectly. function declaration should have the place holders for the variables you are passing into it.

 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>';
 }

Then when you want to call the function you simply invoke the following

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

if you call assignment as follows then the default values you have placed in the variables will be assinged.

assignment(); // url, 2, ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM