1 / 13

Chapter 2 User_defined Function

Learn all about PHP functions in this chapter, including how to create and invoke them, pass input, return values, and include function libraries.

letcher
Download Presentation

Chapter 2 User_defined Function

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2 User_defined Function

  2. 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

  3. 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.

  4. 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.

  5. 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

  6. <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>

  7. 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); ?>

  8. 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"; ?>

  9. 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.

  10. 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 />"; ?>

  11. 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); ?>

  12. 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”)

  13. The end of Chapter 2 Thanks for your paying attention

More Related