140 likes | 156 Views
PHP Reusing Code and Writing Functions. Using Functions. 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
E N D
PHP Reusing Code and Writing Functions
Using Functions • 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)
Using Functions • 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
Defining Functions • 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!
Defining Functions • Example of a trivial function with no return value and no parameters: <?php function my_function() { echo ‘<p>My function was called</p>’; } ?> OR <?php function my_function() { // exit PHP if needed ?> <p>My function was called</p> <?php } ?> • Function call: my_function(); • Variable function: $name=‘my_function’; $name();
Function1.php • http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/function1.php • http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/function1_php.pdf
Defining Functions • 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…
Defining Functions • 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.
Defining Functions • 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!
Defining Functions - Parameters • 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!
Create_table.php • http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/create_table.php • http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/create_table_php.pdf
Defining Functions - Parameters • 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!
Create_table2.php • http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/create_table2.php • http://www.nku.edu/~frank/csc301/Examples/PHP_Functions/create_table2_php.pdf