PHP Exercise 7

User-defined Functions

 

  1. You can make your own functions in PHP by simply typing the following:

 

<?php
function foo ($arg_1, $arg_2,…$arg_n)
{
    echo "Example function.\n";
    return $retval;
}
?>

 

  1. To call the function in your php code, simply type the name of the function followed by parenthesis.  If you pass any parameters to the function, place those inside the parenthesis.

 

foo(arg_1,arg_2,...arg_n);

 

  1. To access the variables passed to a subroutine, simply refer to them like a standard C user-defined function.  For example, if $arg_1 is an argument defined in my function, then I can access the variable by $arg_1 in my function.
  2. You can put user-defined functions in a separate file to organize your functions.  This is especially helpful when more than 1 .php files need to use the same function.  Examine the following example of a user-defined function located in a separate file (called html_functions.inc:

 

<?php

function html_headers($title)

{

echo "<HTML><HEAD><TITLE>$title</TITLE>";

echo "</HEAD><BODY>";

           // Return 1 just in case you ever use this function

// in a conditional statement

return 1;

}

?>

 

  1. To use the functions defined in a separate file, use the include statement in your php code.  For example, this file (named user_function_test.php) will use the html_headers() function defined in html_functions.inc.

 

<?php

include 'html_functions.inc';

 

// The title on my page should be

// My New Website

html_headers("My New Website");

 

echo "The HTML header tags were printed by a subroutine";

echo "</body></html>";

?>

 

  1. This seems simple enough.  One problem with user-defined functions that you may run into is variable scope.  By this, I mean that a variable defined inside a function is only available inside that function.  By default, predefined variables and autoglobals (i.e. $_POST, $_GET, etc) are already global variables and available to all functions.  To declare a variable that is available to other subroutines or parent processes (user_function_test.php was a parent process to html_headers()), use the GLOBAL modifier.

 

function my_new_func()

{

GLOBAL $my_global

...Php code

}

 

  1. Now after I call my_new_func(), I will be able to access $my_global in my php code just as if I had declared it in my current function.

 

 

On Your Own Exercise 7

 

Write a new function called display_month_name() that takes one argument.  The argument should be an integer between 1 & 12 that represents you were born in. The function will return a string value that is the name of the month you were born (i.e. January for an input of 1).  Place this function in a separate file called php_exercise_7.inc.  Create another file called php_exercise_7.php that calls the function and passes it an integer from 1-12.  It should also display "My Name is your_name and I was born in your_birth_month" (replace red items with appropriate information).

 

Hint:  You can use a switch statement instead of many if/elseif/else statements for your display_month_name() function.  Documentation for switch statements can found online at http://www.php.net/manual/en/control-structures.switch.php