--------------------------------------------------------------------------------------------------------------------------
Prepared By : Uday Shah (HOD - IT)
E-Mail : rupareleducation@gmail.com
Contact No : 7600044051
PHP Function : Unit 2
Question : Function
Naming rules:
A valid function name starts with a
letter or underscore, followed by any number of letters, numbers or
underscores.
Types of
Function:
There are different types of function as
under.
1.
Argument
function:
·
Information
may be passed to function via the argument list which is comma delimited list
of expression.
·
PHP
supports passing argument by value (the default), passing by reference and
default argument values. Variable length argument lists are supported only in
PHP 4 and later.
<?php
echo
“Welcome to PHPMYSQL”;
function
sum($a,$b)
{
$ans=$a+$b;
echo
“<br>Sum:=$ans”;
}
sum(10,100);
?>
=> Making arguments be passed by
reference:
·
By
default function arguments are passed by value.(so that if you change the value
of the argument within the function, it does not get changed outside of the
function). If you wish to allow a function to modify its arguments, you must
pass them by reference.
Ex:
<?php
function
add_some_extra(&$string)
{
$string.="and
something extra.";
}
$str="This
is a string,";
add_some_extra($str);
echo
$str;
?>
Output:
“This is a string, and something extra.”
2.
Default Argument
value:
·
Function
may define c++ style default values for scalar arguments.
·
You
can write your function to have default values. You can define an argument as
having a default value with the parameter.
·
For
that argument will adopt when function call, provided you don’t pass value for
the arguments.
<?php
function
makecoffee($type=”Cold Coffee”)
{
return
“Making a cup of $type.”;
}
echo
makecoffee();
echo
makecoffee(“Hot Coffee”);
?>
Output:
Making
a cup of Cold Coffee.
Making
a cup of Hot Coffee.
3.
variable
function:
·
PHP
supports the concept of variable function. This means that if the variable name
has parentheses appended to it, PHP will look for a function with the same name
as whatever the variable execute it.
·
Variable
function will not work with language construct, such as echo(), print(),
unset(), isset(), empty(), include() & require().
4.
return function:
·
Values
are returned by using optional return statement.
·
Any
type may be returned including list an object, these causes the function and
it’s execution immediately and pass control back to the line from it was
called.
Question: Explain variable length argument list.
·
Php 4 and above has support for variable length argument list in user
defined function.
·
This is quit easy using the func_num_args(), func_get_arg() and
func_get_args() functions.
·
No special syntax is required and argument list may be provided with
function definitions and will behave as normal.
func_num_args():
·
It returns the number of argument pass to the function.
o
Syntax: int func_num_args(value)
·
It returns the no. of argument passed in to the current user define
function.
·
It will generate a warning if called from outside of a user define
function.
·
This function can’t use directly use as a function parameter,
instead it’s result may be assign to a variable, which can be passed to the
function.
<?php
function
a()
{
$num=func_num_args();
$temp=func_get_args();
for($i=0;$i<$num;$i++)
{
echo
$temp[$i].”<br>”;
}
}
a(“MMG”, “A5B”,56);
?>
func_get_arg():
·
It return an item from the argument list.
o
Syntax: mixed func_get_arg()
·
It returns the argument which is at the argument number into a UDF’s
argument list.
·
Function arguments are counted starting from 0(zero).
·
It will generate a warning if called outside of a function definition.
<?php
function
fun()
{
$numargs=func_num_args();
echo
“Number of arguments:$numargs<br>”;
if($numargs>=2)
{
echo “Second argument
is:”.func_get_arg(1).”<br>”;
}
}
Fun(1,’b’,3);
?>
func_get_args():
·
It gets an array of the function’s argument list.
·
This function may be used in conjunction with func_get_arg and
func_num_args to allow user define function to accept variable length argument
list.
o
Syntax:func_get_args(value)
·
It returns an array in which each element is a copy of the current
corresponding member of the user define function argument list.
<?php
function fun()
{
$numargs=func_num_args();
echo
“Number of arguments: $numargs”;
}
fun(11,2,”abc”);
?>
Explain Variable Handling Function.
Gettype() :
·
It is used to get the
type of a variable.
·
Syntax: gettype(variable)
·
Possible values for the
return strings are Boolean,integer,double
·
string,array,NUL etc.
Settype() :
·
It is used to set the
type of variable.
·
settype(variable,type0
)
)
·
Possible values for the
return strings are Boolean, integer, double, string, array, NULL etc.
Strval()
:
·
It is used to get string
value of a variable.
·
strval(variable)
Floatval() :
·
It is used to get float
value of a variable.
·
floatval(variable)
Intval() :
·
It is used to get
integer value of a variable.
·
intval
(variable)
(variable)
Isset() :
·
The isset () function is used to check whether a variable is set or not.
·
If a variable is already unset with unset() function, it will no longer
be set.
·
The isset() function return false if testing variable contains a NULL
value.
Unset() :
·
unset() destroys the specified variables.
·
The behavior of unset() inside
of a function can vary depending on what type of variable you are attempting to
destroy.
:: Best of Luck ::
Please share and comment it... Thank you.
https://myshopprime.com/uday.shah3/shop