Beruflich Dokumente
Kultur Dokumente
Function in PHP
Using require() & include()
User defined function
Function arguments
Returning values
Variables functions
Internal functions
File Edit
Address
http://localhost/myweb/test.php
<?php
<?php
Go
echo @"A $color $fruit"; // A
$color = 'green';
include 'vars.php';
$fruit = 'apple'; A A green apple
echo "A $color $fruit"; // A green apple
?>
?>
File
Edit
Address
View
Favorites
http://localhost/myweb/test.php
Help
Go
test.php
<?php
require unknown.php
include
echo "A $color $fruit";
?>
following:
function name
Argument list
<?php
function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
?>
key word
<?php
$makefoo = true;
/* We can't call foo() from here
since it doesn't exist yet,
but we can call bar() */
foo();
File Edit
Address
Go
bar();
if ($makefoo) {
function foo(){
echo "I don't exist until program execution reaches me.<br>";
}
}
/* Now we can safely call foo()
since $makefoo evaluated to true */
if ($makefoo) foo();
function bar() {
echo "I exist immediately upon program start.<br>";
}?>
<?php
function recursion($a)
{
if ($a < 20) {
echo "$a|";
recursion($a + 1);
}
}
recursion(0);
?>
Edit
Address
View
Favorites
http://localhost/myweb/test.php
Tool
Help
Go
0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|
Function arguments
Information may be passed to functions via the
Function arguments
<?php
function takes_array(&$input)
{
echo $input[0].",".$input[1]."<br>";
$input[2] = 4;
}
$a = array(2,3,6);
takes_array($a);
echo $a[2]."<br>";
?>
Pass
Pass
byby
reference
value
2,3
4
6
Go
Function arguments
Microsoft Internet Explorer
File Edit
<?php
function makecoffee($type = "cappuccino")
{
return "Making a cup of $type.<br>";
}
echo makecoffee();
echo makecoffee(null);
echo makecoffee("espresso");
?>
Address
Go
Function arguments
Microsoft
Internet
PHP also allows the use of
- x
arrays
andExplorer
the special
File Edit
<?php
function makecoffee($types = array("cappucc
ino"), $coffeeMaker = NULL)
{
$device = is_null($coffeeMaker) ? "hands"
: $coffeeMaker;
return "Making a cup of ".join(", ", $types).
" with $device.<br>";
}
echo makecoffee();
echo makecoffee(array("cappuccino", "lavazz
a"), "teapot");
?>
http://localhost/myweb/test.php
Go
Function arguments
The default value must be a constant expression, not
Function arguments
Default args must
be right to all args
<?php
function makeyogurt( $type = acidophilus ,
$flavour )
{
return "Making a bowl of $type $flavour.<
br>";
}
echo makeyogurt("raspberry");
// won't work as expected
?>
Go
Function arguments
int func_num_args ( void )
function.
Generates a warning if called from outside of a userdefined function.
Returns the number of arguments passed into the
current user-defined function.
Function arguments
Microsoft Internet Explorer
File Edit
<?php
function foo()
{
$numargs = func_num_args();
echo "Number of arguments: $numargs<br>";
}
foo(1, 2, 3);
?>
Address
Number of arguments: 3
Go
Returning values
Values are returned by using
the
optional
Microsoft
Internet
Explorer return
- x
Returning values
A function can not return
multiple
values, -but x
Microsoft
Internet Explorer
<?php
function small_numbers()
{
return array (0, 1, 2);
}
list ($zero, $one, $two) = small_numbers();
echo $zero."|".$one."|".$two;
?>
Address
0|1|2
http://localhost/myweb/test.php
Go
Variables functions
PHP supports the concept of variable functions. This
Variables functions
<?php
function foo() {
echo "In foo()<br />\n";
}
function bar($arg = ''){
echo "In bar(); argument was '$arg'.<br/>\n";
}
// This is a wrapper function around echo
function echoit($string){
echo $string;
}
$func = 'foo';
$func();
// This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
?>
In foo()
In bar(); argument was 'test'.
test
Go
Internal functions
function_exists Return TRUE
the
Microsoftif
Internet Explorer
File Edit
Address
given
- x
<?php
IMAP functions are not available.
if (function_exists('imap_open')) {
echo "IMAP functions are available.<br />";
The function name, as a string.
} else { $function_name
echo "IMAP functions are not available.<br />";
}
?>
Go
Summary
You have studied knowledge about function in PHP.
You knew how to declare a function, using built in