130 likes | 142 Views
Learn all about PHP functions in this chapter, including how to create and invoke them, pass input, return values, and include function libraries.
E N D
Chapter 2 User_defined Function
Chapter Goals • In this chapter, you’ll learn all about PHP functions, including how : • to create and invoke them, • pass input, • return values to the caller, and • Include function libraries
Function: • Function is a self-contained block of code that can be called by your scripts. • Functions provide a way to group together related statements into a cohesive block. • For reusable code, a function saves duplicating statements and makes maintenance of the code easier.
Defining a Function • You can define a function using the function statement: function function_name( $argument1, $argument2 ) { // function code here } • The name of the function follows the function statement and precedes a set of parentheses. • If your function requires arguments, you must place comma-separated variable names within the parentheses.
Calling a Function • Functions in a PHP program can be either built-in or user-defined. • Regardless of their source, all functions are evaluated in the same way: $some_value = function_name( [ parameter, ... ] ); // strlen( ) is a built-in function that returns the length of a string $length = strlen("PHP"); // $length is now 3
<html><head> <title>Simple Function Call</title> </head><body bgcolor="#ffffff"> <?php function bold($string){ echo "<b>" . $string . "</b>\n"; } // First example function call (with a static string) echo "this is not bold\n"; bold("this is bold"); echo "this is again not bold\n"; // Second example function call (with a variable) $myString = "this is bold"; bold($myString); ?> </body></html>
Functions can also return values by using the return statement: <?php function heading($text, $headingLevel){ switch ($headingLevel) { case 1: $result = "<h1>" . ucwords($text) . "</h1>"; break; case 2: $result = "<h2>" . ucwords($text) . "</h2>"; break; case 3: $result = "<h3>" . ucfirst($text) . “</h3>"; break; default: $result = "<p><b>" . ucfirst($text) . "</b>"; } return($result); } $test = "user defined functions"; echo heading($test, 2); ?>
How Variables Are Passed to Functions 1.) Passing arguments by value • By default, variables are passed to functions by value, not by reference • Example: <?php function doublevalue($var){ $var = $var * 2; } $variable = 5; doublevalue($variable); echo "\$variable is: $variable"; ?>
How Variables Are Passed to Functions 2.) Passing arguments by reference • An alternative to returning a result or using a global variable is to pass a reference to a variable as an argument to the function. • This means that any changes to the variable within the function affect the original variable • Passing an argument by reference is done by appending an ampersand(&) to the front of the argument.
Example: Passing by Reference <?php $cost = 20.00; $tax = 0.05; function calculate_cost(&$cost, $tax) { // Modify the $cost variable $cost = $cost + ($cost * $tax); } calculate_cost($cost,$tax); echo "Tax is: ". ($tax*100)."<br />"; echo "Cost is: $". $cost."<br />"; ?>
Default argument values • PHP allows functions to be defined with default values for arguments. • A default value is simply supplied in the argument list using the = sign <?php function salestax($price,$tax=.0575) { $total = $price + ($price * $tax); echo "Total cost: $total"; } $price = 15.47; salestax($price); ?>
Using built-in functions • PHP’s built-in functions are one reason why PHP is so powerful and useful. isset($varname) empty($varname) die(“message”); • File Inclusion Statement PHP offers four statements for including such files into applications, each of which is introduced in this section. Include(“filename”) Include_once(“filename”) Require(“filename”) Require_once(“filename”)
The end of Chapter 2 Thanks for your paying attention