Download presentation
Presentation is loading. Please wait.
Published byMarsha Welch Modified over 9 years ago
1
PHP Reusing Code and Writing Functions 1
2
Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task (should be a single, well-defined, repeatedly-used task) Optionally returns a result Calling a function function_name (possibly_empty_comma_sep_list_of_actual_pars); or The function call is operand in an expression if the value returned by the function is of interest Actual parameters – can be data (literals), names of variables holding data (scalar vars, arrays, objects) 2 Using Functions
3
To use a function, you need to know the function’s prototype: How many parameters a function takes; What each parameter represents, of what data type it needs to be; What is the result the function returns, its type. Example: resource fopen (string filename, string mode [, bool use_include_path [, resource context]]) [] indicate optional parameters If values are not provided for them, default values are used Optional parameters can only be left out from the right Calling an undefined function error: script name, line #, function name are reported 3 Using Functions
4
A function declaration creates (= declares) a new function: function function_name (possibly_empty_comma_sep_list_of_formal_pars) { // code that performs the task you require… // can contain everything that is legal elsewhere in a PHP script: // variable declarations, function calls, language constructs, require/include, // php statements, class declarations, HTML, other functions } function – keyword! 4 Defining Functions
5
Example of a trivial function with no return value and no parameters: <?php function my_function() { echo ‘ My function was called ’; } ?> OR <?php function my_function() { // exit PHP if needed ?> My function was called <?php } ?> Function call: my_function(); Variable function: $name=‘ my_function’; $name (); 5 Defining Functions
6
Function1.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/function1.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/function1.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/function1_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/function1_php.pdf
7
Function names are: For built-in functions: “superglobal” = available to all PHP scripts For user-defined functions: global in scope = available to the entire script in which they were declared create function libraries, included in scripts when needed Function names are: Case insensitive !!!! PHP is a mixed-case language (function names are case insensitive while everything else is not, e.g. var names) However, aim to be consistent; convention is to use all lowercase… 7 Defining Functions
8
Function names – restrictions: Function names have to be valid PHP identifiers (letters, digits, underscores, do not begin with a digit) Your function cannot have the same name as an existing function (built-in or user defined)! Which means: PHP does not support function overloading! Overloading = the practice of having multiple function signatures with same name that are accessed according to the position and type of actual parameters that are passed to a function at run time. 8 Defining Functions
9
Forward referencing = function definitions must precede function calls – it’s not required in PHP, due to how programs are parsed: All PHP blocks are read, then the included files Function definitions are first read into memory, which defines them in the page => global scope; Then the rest of the page is processed fatal “undefined function call” runtime errors are avoided Nested functions – provide complications Are not defined in the global memory space of the page until the enclosing function is called Avoid writing function definitions within functions! 9 Defining Functions
10
Parameters = to pass data created outside the function into a function Example - function with one parameter: Prototype: function create_table (array data); Displays the one-dimensional argument array as a table, one element on a row Declaration: There is no type specified in the definition of function parameters! 10 Defining Functions - Parameters
11
Create_table.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table_php.pdf
12
When defining functions, you can define parameters as mandatory or optional: Mandatory parameters must be listed before optional parameters in the parameter list Optional parameters have a default value Arguments don’t have to be provided for optional parameters when function is called: default values are used instead. Arguments are matched to parameters from left to right, based on position, not on type! 12 Defining Functions - Parameters
13
Create_table2.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table2.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table2.php http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table2_php.pdf http://www.nku.edu/~frank/csc301/Examples/PHP_Functi ons/create_table2_php.pdf
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.