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

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

Embed Size (px)

DESCRIPTION

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; }

Citation preview

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

Creating Functions tMyn 1

Creating Functions

• Function can be divided into two groups:– Internal (built in) functions– User-defined functions

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

Creating Functions tMyn 2

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.

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

Creating Functions tMyn 3

User-defined functions

• A function may be defined using syntax such as the following:

function funktionName($formalParameter1, formalParameter2, …){

echo ” Example function.<br>”;…return $retValue;

}

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

Creating Functions tMyn 4

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

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

Creating Functions tMyn 5

• 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:

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

Creating Functions tMyn 6

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

Creating Functions tMyn 7

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

Creating Functions tMyn 8

• Functions need not be defined before they are referenced:

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

Creating Functions tMyn 9

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

Creating Functions tMyn 10

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

Creating Functions tMyn 11

• 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:

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

Creating Functions tMyn 12

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

Creating Functions tMyn 13

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

Creating Functions tMyn 14

• The following function has two parameters:

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

Creating Functions tMyn 15

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

Creating Functions tMyn 16

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

Creating Functions tMyn 17

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

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

Creating Functions tMyn 18

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

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

Creating Functions tMyn 19

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

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

Creating Functions tMyn 20

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

Creating Functions tMyn 21

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

Creating Functions tMyn 22

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

Creating Functions tMyn 23

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.

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

Creating Functions tMyn 24

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

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

Creating Functions tMyn 25

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

Creating Functions tMyn 26

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

Creating Functions tMyn 27

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

Creating Functions tMyn 28

• After a minor change a reference parameter is in use:

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

Creating Functions tMyn 29

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

Creating Functions tMyn 30

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

Creating Functions tMyn 31

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

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

Creating Functions tMyn 32

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

Creating Functions tMyn 33

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

Creating Functions tMyn 34

• 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:

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

Creating Functions tMyn 35

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

Creating Functions tMyn 36

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

Creating Functions tMyn 37

• 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:

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

Creating Functions tMyn 38

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

Creating Functions tMyn 39

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

Creating Functions tMyn 40

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

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

Creating Functions tMyn 41

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

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

Creating Functions tMyn 42

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

Creating Functions tMyn 43

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

Creating Functions tMyn 44

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

Creating Functions tMyn 45

• Next example utilizes the returned value in the calling part:

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

Creating Functions tMyn 46

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

Creating Functions tMyn 47

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

Creating Functions tMyn 48

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

Creating Functions tMyn 49

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

Creating Functions tMyn 50

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

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

Creating Functions tMyn 51

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

Creating Functions tMyn 52