Sie sind auf Seite 1von 23

Fundamental PHP

AUTHOR: TRUONG TOAN THINH

Function in PHP
Using require() & include()
User defined function
Function arguments
Returning values
Variables functions
Internal functions

Using require() & include()


require() is identical to include() except upon failure it

will also produce a fatal E_COMPILE_ERROR level


error. In other words, it will halt the script whereas
include() only emits a warning (E_WARNING) which
allows the script to continue.
Microsoft Internet Explorer
vars.php

File Edit

View Favorites Tool Help


test.php

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
?>
?>

Using require() & include()

File

Edit

Address

View

Favorites

http://localhost/myweb/test.php

Microsoft Internet Explorer


Tool

Help

Go

test.php

<?php
require unknown.php
include
echo "A $color $fruit";
?>

User defined function


A function may be defined using syntax such as the

following:

function name

Argument list

<?php
function foo($arg_1, $arg_2, /* ..., */ $arg_n)
{
echo "Example function.\n";
return $retval;
}
?>

key word

User defined function


Function names follow the same rules as other labels in

PHP. A valid function name starts with a letter or


underscore, followed by any number of letters, numbers,
or underscores. As a regular expression, it would be
expressed thus: [a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.
Functions need not be defined before they are
referenced, except when a function is conditionally
defined as shown in the two examples below.
When a function is defined in a conditional manner such
as the two examples shown. Its definition must be
processed prior to being called.

User defined function


Microsoft Internet Explorer

<?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

View Favorites Tool Help


http://localhost/myweb/test.php

Go

I exist immediately upon program


start
I don't exist until program
execution reaches me.

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>";
}?>

User defined function


Microsoft Internet Explorer

File Edit View Favorites Tool Help


<?php
function foo()
Address
http://localhost/myweb/test.php
Go
{
echo "I am foo.<br>";
I am foo.
function bar()
I don't exist until foo() is called.
{
echo "I don't exist until foo() is called.<br>";
}
}
bar();
/* 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();
?>

User defined function

<?php
function recursion($a)
{
if ($a < 20) {
echo "$a|";
recursion($a + 1);
}
}
recursion(0);
?>

Microsoft Internet Explorer


File

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

argument list, which is a comma-delimited list of


expressions.
PHP supports passing arguments by value (the
default), passing by reference, and default argument
values. Variable-length argument lists are also
supported

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

Microsoft Internet Explorer


File Edit
Address

2,3
4
6

View Favorites Tool Help


http://localhost/myweb/test.php

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

View Favorites Tool Help


http://localhost/myweb/test.php

Making a cup of cappuccino.


Making a cup of .
Making a cup of espresso.

Go

Function arguments
Microsoft
Internet
PHP also allows the use of
- x
arrays
andExplorer
the special
File Edit

View Favorites Tool Help

type NULL as default values,


for example:
Address

<?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

Making a cup of cappuccino with


hands.
Making a cup of cappuccino, lavazza
with teapot.

Function arguments
The default value must be a constant expression, not

(for example) a variable, a class member or a


function call.
Note that when using default arguments, any
defaults should be on the right side of any nondefault arguments; otherwise, things will not work as
expected

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
?>

Microsoft Internet Explorer


File Edit
Address

View Favorites Tool Help


http://localhost/myweb/test.php

Making a bowl of acidophilus


raspberry.

Making a bowl of raspberry .

Go

Function arguments
int func_num_args ( void )

Gets the number of arguments passed to the

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

View Favorites Tool Help


http://localhost/myweb/test.php

Number of arguments: 3

Go

Returning values
Values are returned by using
the
optional
Microsoft
Internet
Explorer return
- x

statement. Any type may FilebeEdit returned,


including
View Favorites Tool
Help
<?php
Address the function to end
Go
arrays and objects. This causes
function square($num)
pass control back to
{ its execution immediately and
16
return $num * $num;
} the line from which it was called
echo square(4); // outputs '16'.

If the return() is omitted the value NULL will be


?>
returned.
http://localhost/myweb/test.php

Returning values
A function can not return
multiple
values, -but x
Microsoft
Internet Explorer

similar results can be obtained


by View
returning
anHelp
array.
File Edit
Favorites Tool

<?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

means that if a variable name has parentheses


appended to it, PHP will look for a function with the
same name as whatever the variable evaluates to,
and will attempt to execute it. Among other things,
this can be used to implement callbacks, function
tables, and so forth.

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()
?>

Microsoft Internet Explorer


File Edit
Address

View Favorites Tool Help


http://localhost/myweb/test.php

In foo()
In bar(); argument was 'test'.
test

Go

Internal functions
function_exists Return TRUE
the
Microsoftif
Internet Explorer

function has been defined

File Edit
Address

given
- x

View Favorites Tool Help


http://localhost/myweb/test.php

bool function_exists ( string $function_name )

<?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

functions in PHP, you also know to get value


returned from a function, & to pass parameters by
value or reference. You also know about variable
function

Das könnte Ihnen auch gefallen