Presentation is loading. Please wait.

Presentation is loading. Please wait.

Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions.

Similar presentations


Presentation on theme: "Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions."— Presentation transcript:

1 Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions

2 Creating FunctionstMyn2 Built-in functions PHP comes standard with many functions and constructs. There are also functions that require specific PHP extensions compiled in, otherwise fatal "undefined function" errors will appear. For example, to use image functions such as imagecreatetruecolor(), PHP must be compiled with GD support. Or, to use mysql_connect(), PHP must be compiled with MySQL support. There are many core functions that are included in every version of PHP, such as the string and variable functions. A call to phpinfo() or get_loaded_extensions() will show which extensions are loaded into PHP. Also note that many extensions are enabled by default and that the PHP manual is split up by extension.

3 Creating FunctionstMyn3 User-defined functions A function may be defined using syntax such as the following: function funktionName($formalParameter1, formalParameter2, …) { echo ” Example function. ”; … return $retValue; }

4 Creating FunctionstMyn4 Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. All functions and classes in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa. PHP does not support function overloading. Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.

5 Creating FunctionstMyn5 Give the function a name that reflects what the function does. The function name can start with a letter or underscore (not a number). Example of a simple function:

6 Creating FunctionstMyn6

7 Creating FunctionstMyn7

8 Creating FunctionstMyn8 Functions need not be defined before they are referenced:

9 Creating FunctionstMyn9

10 Creating FunctionstMyn10

11 Creating FunctionstMyn11 To add more functionality to a function, we can add parameters. A parameter is just like a variable. Parameters are specified after the function name, inside the parentheses. Information may be passed to functions via the argument list, which is a comma-delimited list of expressions. PHP supports passing arguments by value (the default), passing by reference, and default argument values. Passing argument by value:

12 Creating FunctionstMyn12

13 Creating FunctionstMyn13

14 Creating FunctionstMyn14 The following function has two parameters:

15 Creating FunctionstMyn15

16 Creating FunctionstMyn16

17 Creating FunctionstMyn17 The include() statement includes and evaluates the specified file. The description below also applies to require(). The two constructs are identical in every way except how they handle failure. They both produce a warning ( E_WARNING ), but require() results in a fatal error ( E_ERROR ). In other words, use require() if you want a missing file to halt processing of the page. include() does not behave this way, the script will continue regardless. Be sure to have an appropriate include path setting as well. Files for including are first looked for in each include_path entry relative to the current working directory, and then in the directory of current script.

18 Creating FunctionstMyn18 When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope. If the include occurs inside a function within the calling file, then all of the code contained in the called file will behave as though it had been defined inside that function. So, it will follow the variable scope of that function.

19 Creating FunctionstMyn19 When a file is included, parsing drops out of PHP mode and into HTML mode at the beginning of the target file, and resumes again at the end. For this reason, any code inside the target file which should be executed as PHP code must be enclosed within valid PHP start and end tags.

20 Creating FunctionstMyn20

21 Creating FunctionstMyn21

22 Creating FunctionstMyn22

23 Creating FunctionstMyn23 Passing by reference By default, function arguments are passed by value (so that if the value of the argument within the function is changed, it does not get changed outside of the function). To allow a function to modify its arguments, they must be passed by reference. To have an argument to a function always passed by reference, prepend an ampersand (&) to the argument name in the function definition.

24 Creating FunctionstMyn24 Note that there's no reference sign on a function call - only on function definitions. Function definitions alone are enough to correctly pass the argument by reference.

25 Creating FunctionstMyn25

26 Creating FunctionstMyn26

27 Creating FunctionstMyn27

28 Creating FunctionstMyn28 After a minor change a reference parameter is in use:

29 Creating FunctionstMyn29

30 Creating FunctionstMyn30

31 Creating FunctionstMyn31 A function may define C++-style default values for scalar arguments as follows:

32 Creating FunctionstMyn32

33 Creating FunctionstMyn33

34 Creating FunctionstMyn34 Another important feature of variable scoping is the static variable. A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope. Consider the following example:

35 Creating FunctionstMyn35

36 Creating FunctionstMyn36

37 Creating FunctionstMyn37 This function is quite useless since every time it is called it sets $notGood to 0 and prints 0. The $notGood++ which increments the variable serves no purpose since as soon as the function exits the $notGood variable disappears. To make a useful counting function which will not lose track of the current count, the local variable is declared static :

38 Creating FunctionstMyn38

39 Creating FunctionstMyn39

40 Creating FunctionstMyn40 Now, $thisIsGood is initialized only in first call of function and every time the thisWorks() function is called it will print the value of $thisIsGood and increment it.

41 Creating FunctionstMyn41 Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. Note that since return() is a language construct and not a function, the parentheses surrounding its arguments are not required. It is common to leave them out, and you actually should do so.

42 Creating FunctionstMyn42

43 Creating FunctionstMyn43

44 Creating FunctionstMyn44

45 Creating FunctionstMyn45 Next example utilizes the returned value in the calling part:

46 Creating FunctionstMyn46

47 Creating FunctionstMyn47

48 Creating FunctionstMyn48

49 Creating FunctionstMyn49

50 Creating FunctionstMyn50 A function can not return multiple values, but similar results can be obtained by returning an array.

51 Creating FunctionstMyn51

52 Creating FunctionstMyn52


Download ppt "Creating FunctionstMyn1 Creating Functions Function can be divided into two groups: –Internal (built in) functions –User-defined functions."

Similar presentations


Ads by Google