Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | Links | Search | Bio |

Guide to PHP and MySQL (University at Buffalo Version)


Chapter 6: Working With Functions

PHP Functions

PHP functions provide a way to create a group of statements that perform a certain task and then "call" that function from PHP code.

<?php
function generate_footer() {
  echo ("<footer>\n<p>Copyright &copy; " . date ("Y") . " Jim Gerland</p>\n</footer>\n");
}
generate_footer();
?>

Figure 6-1: A PHP Function

Copyright © 2024 Jim Gerland


Figure 6-2: A PHP Function Output

The PHP include Statement

PHP allows you to have a function in a separate file, such as functions.php and, using the PHP include or include_once statement access that function in any PHP program.

<?php include("generateFooter.php"); ?>
<!doctype html>
<html>
  <head>
    <title>The name of this page</title>
  <head>
  <body>
    <h2>Hello!</h2>
    <p><?php echo("Hello World!"); ?></p>
    <?php generateFooter(); ?>
  </body>
</html>
Figure 6-3: Using include

The rendered web page would look like this:

Hello!

Hello World!


Copyright © 2024 Jim Gerland


Figure 6-4: PHP include Output

Function Parameters

PHP allows you to pass arguments to a function by declaring them as the parameters of the function.

<?php
function calcSalesTax($price, $tax) {
  $total = $price + ($price * $tax);
  printf("<p>Total cost: $%.2f</p>\n", $total");
}

calcSalesTax(42.00, 0.0875);
?>

Figure 6-5: Passing Arguments to a Function

Total cost: $45.67


Figure 6-6: Passing Arguments to a Function Output

Using Default Function Parameters

PHP also allows you to have default values parameters if that parameter is not in the arguments passed to a function by assigning a default value to them as the parameters of the function.

<?php
function calcSalesTax2($price, $tax = .0875) {
  $total = $price + ($price * $tax);
  printf("<p>Total cost: $%.2f</p>\n", $total");
}

calcSalesTax2(42.00);
?>

Figure 6-7: Using Default Arguments with a Function

Total cost: $45.67


Figure 6-8: Using Default Arguments with a Function Output

Using Optional Function Parameters

PHP also allows you to have optional parameters if that parameter is not in the arguments passed to a function.

<?php
function calcSalesTax3($price, $tax = "") {
  $total = $price + ($price * $tax);
  printf("<p>Total cost: $%.2f</p>\n", $total");
}

calcSalesTax3(42.00);
?>

Figure 6-9: Using Optional Arguments with a Function

Total cost: $42.00


Figure 6-10: Using Optional Arguments with a Function Output

Task - Create a PHP Loop/Array/Function Page

For this task you will copy your index.php file to a new file called lfa.php. You will also create a file called that will hold your PHP functions. You will need to include your functions.php in the code for your index.php file.

You will modify the PHP array function for this item in your index.php page to add a link for this assignment that allows me to view your work.

You *MUST* use the W3C Unicorn Validator to validate your HTML5/CSS3 code.

Then upload your files (images, CSS, js, php, etc.) to your localhost Web Space and add <a> (anchor) tags that utilize the 'download.php' for all the files for this assignment that allow me to download your files to my web server.

Let's Get Started with a PHP function Page!
Let's Get Started with a PHP include Page!

Help contribute to my OER Resources. Donate with PayPal button via my PayPal account.
Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. Copyright © 2016-2024 Jim Gerland