Documente Academic
Documente Profesional
Documente Cultură
1. User-defined functions:
Syntax:
<?php
function foo ($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
?>
Any valid PHP code may appear inside a function, even other
functions and class definitions.
<?php
function foo()
{
function bar()
{
echo "I don't exist until foo() is called.\n";
}
}
/* We can't call bar() yet
since it doesn't exist. */
foo();
/* Now we can call bar(),
foo()'s processesing has
made it accessible. */
bar();
?>
func_num_args
func_num_args -- Returns the number of arguments passed to the
function
Description
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs\n";
}
func_get_arg
(PHP 4 )
Description
func_get_args
(PHP 4 )
func_get_args -- Returns an array comprising a function's argument list
Description
foo(1, 2, 3);
?>
3.Returning values
Values are returned by using the optional return statement. Any type
may be returned, including lists and objects. This causes the function
to end its execution immediately and pass control back to the line from
which it was called.
Use of return()
<?php
function square ($num)
{
return $num * $num;
}
echo square (4); // outputs '16'.
?>
You can't return multiple values from a function, but similar results can
be obtained by returning a list.
$func = 'foo';
$func(); // This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
?>
You can also call an object's method by using the variable functions feature.
function Bar()
{
echo "This is Bar";
}
}
?>
function_exists
(PHP 3>= 3.0.7, PHP 4 )
function_exists -- Return TRUE if the given function has been defined
Description
Checks the list of defined functions, both built-in (internal) and user-
defined, for function_name. Returns TRUE on success or FALSE on failure.
<?php
if (function_exists('imap_open')) {
echo "IMAP functions are available.<br />\n";
} else {
echo "IMAP functions are not available.<br />\n";
}
?>