2. Functions
2. Function and String
2.1Defining and calling a function
2.2 Default parameters
2.3 Variable parameters, Missing parameters
2.4 Variable function, Anonymous function
2
3. Functions
Defining a function
function [&] function_name([param[,…]])
{
//code
}
Function name are case insensitive.
If the function is defined with the optional
ampersand then that function returns a reference to
the returned data.
3
4. Revise –
• Function types-
1. Built-in
2. User defined
• calling to function(return or no return)
• Call by value - f($a);
• Call by reference - &f($a);
Ex.function & f($a)
{
}
$v=& f(10);
4
6. Functions :Parameter types
1. Default Parameters
function pival($pi=3.14)
{
echo “The value of PI is $pi”;
}
The default value is constant.
While calling function,
pival(); // The value of PI is 3.14
pival(null); // The value of PI is
pival(3.00); // The value of PI is 3.00
A function may have any number of parameters
with default values, but they must be listed after all
the parameters.
6
8. Functions :Parameter types
2. Variable Parameters
When number of parameters are not fixed then
Variable Parameter function can be used. For e.g. to
find out area of different shapes.
PHP provides three functions:
1. func_get_args(): It returns an array of all
parameter provided to the function.
2. func_num_args(): It returns the number of
parameters provided to the function.
3. func_get_arg(): It returns a specific argument
from the parameters.
8
10. Functions :Parameter types
3. Missing Parameters
When you call a function,
you can pass any number of arguments to the
function.
Any parameters the function expects that are not
passed to it remain unset,
and a warning is issued for each of missing parameter.
10
11. Example-
Missing Parameters
<?php
function display($a, $b)
{ if (isset($a))
{
echo "first argument value is set";
}
if (isset($b))
{
echo "second argument value is set";
}
}display(10,23);
display(20);
display();?>
11
12. Function Type-1. Variable Function
As with variable variables, you can call a function
based on the value of a variable.
For e.g., a variable is used to determine which of
three functions to call:
switch($which)
{ case 'first': first( ); break;
case 'second': second( ); break;
case 'third': third( ); break; }
In this case, we could use a variable function call to
call the appropriate function. To make a variable
function call, include the parameters for a function in
parentheses after the variable. $f = 'echo'; $f('hello,
world'); // does not work
12
13. Variable Function
To rewrite the previous example:
$which(); // if $which is "first" the function first( ) is
called, etc... If no function exists for the variable, a
runtime error occurs when the code is evaluated. So,
you can use the built-in function function_exists( ) to
check whether a function exists for the value of the
variable before calling the function:
$yes_or_no = function_exists(function_name); For
example:
if(function_exists($which))
{ $which(); // if $which is "first" the function first( )
is called, etc... }
13
15. Functions Type- 2. Anonymous Function
These functions are localized and temporary & with
no names.
You can create an anonymous function using
create_function( ).
This function takes two parameters-the first shows the
parameters the anonymous function takes in, and the
second is the actual code or functionality.
A randomly generated name for the function is
returned:
$func_name=create_function(args_string,code_string)
15