Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— 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 Simple Function Call <?php function bold($string){ echo " ". $string. " \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); ?>

7 Functions can also return values by using the return statement: <?php function heading($text, $headingLevel){ switch ($headingLevel) { case 1: $result = " ". ucwords($text). " "; break; case 2: $result = " ". ucwords($text). " "; break; case 3: $result = " ". ucfirst($text). “ "; break; default: $result = " ". ucfirst($text). " "; } 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 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. How Variables Are Passed to Functions

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)." "; echo "Cost is: $". $cost." "; ?>

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


Download ppt "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."

Similar presentations


Ads by Google