InternetProgramming FunctionsPHP
InternetProgramming FunctionsPHP
PHP Functions
Topics
User-defined functions
Function arguments
Returning values
Variable functions
Internal (built-in) functions
Anonymous functions
Defining a function
A function is a routine (or more
accurately, a sub routine) that can be
called to perform its duties from any
point in the program that needs it.
Syntax
function functionName()
{
code to be executed;
}
Function declaration begins with the keyword function. So that PHP parser know
that what follows is a user defined function
Guidelines for PHP functions
function bar()
{
echo "I exist immediately upon program start.\n";
}
Functions within functions
<?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();
Output:
Original Value is 10 ??
Any value return from the function?
Passed by reference
<?php
function setHeight($minheight=50)
{
echo"The height is :". $minheight."<br>";
}
setHeight(350);
setHeight();// will use the default value of 50
setHeight(135);
setHeight(80);
?>
Returning values
function first() {
function second(){
echo "I don't exist until first() is called.\n";
}
}
/* We can't call second() yet since it doesn't exist. */
first();
/* Now we can call second(), first()'s processing has made it
accessible. */
second();
?>
Recursive functions
: Recursion occurs when a function calls itself
recursion occurs when
<?php
//Our recursive function. something contains, or uses, a
function recursive($num){ similar version of itself. That
//Print out the number. similar version then contains
echo $num, '<br>';
or uses another similar
//If the number is less or equal to 50.
if($num < 50){ version of itself, and so on.
//Call the function again. Increment
number by one.
Sometimes this process can
return recursive($num + 1); go on forever
} The number of repetitions, or
}
"depth" of the recursion, is
//Set our start number to 1. limited by some sort of end
$startNum = 1; condition.
//Call our recursive function.
recursive($startNum);
?>
Variable-length argument list
2. func_num_args() –
◦ It returns the total number of arguments
provided in the function call operation.
◦ For example, if "func_num_args()" returns 1,
you know that there is only 1 argument
provided by calling code
Variable-length argument list
3. func_get_args() –
◦ It creates and returns an array which
contains values of all arguments provided in
the function call operation.
◦ For example, if "$args= func_get_args()" is
used in a function, $args will be array
containing all arguments
Variable-length argument list
//explode(separator,string)
$str2 = "Hello world. It's a beautiful day.";
print_r(explode(" ",$str2)); //print_r(var_name, return_output)
Array ( [0] => Hello [1] => world. [2] => It's
[3] => a [4] => beautiful [5] => day. )
//str_replace(find,replace,string,count)
echo str_replace("world","Peter","Helloworld!");
?>
<?php
//strlen(string)
echo strlen("Hello world!");
//trim(string,charlist)
$str4 = " Hello World! ";
echo "With trim: " . trim($str4);
//strrev(string)
$str5 = “Hello world”;
echo strrev("Hello world!");
?>
Array functions
mail($to,$subject,$txt,$headers);
?>
Anonymous functions
also known as closures
allow the creation of functions which have
no specified name.
They are most useful as the value
of callback parameters (small localized
throw-away function consisting of a few
lines for a specific purpose.)
Can be used as the values of variables
Anonymous function with
create_function (creates an anonymous
function from the parameters passed, and
returns a unique name for it)
$square=create_function('$x', 'return $x*$x;');
echo $square(2);//4
Anonymous function as of PHP 5.3,
<?php
$square=function($x){
return $x * $x;
}
echo $square(2);
?>
Variable Scope
In general, variable names used inside of
function code, do not mix with the variables
outside of the function. They are walled-off
from the rest of the code. This is done
because you want to avoid “unexpected”
side effects if two programmers use same
variable name in different parts of the code.
We call this “name spacing” the variables.
The function variables are in one name
space whilst the main variables are in
another name space.
<?php
$a = 1; /* global scope */
function test()
{
echo $a; /* reference to local scope variab
le */
}
This script will not produce any output
because the echo statement refers to a local
test(); version of the $a variable, and it has not been
assigned a value within this scope.
?>
<?php
$a = 1;
$b = 2;
function Sum()
{
global $a, $b; The above script will output 3. By
declaring $a and $b global within the function,
$b = $a + $b; all references to either variable will refer to
} the global version. There is no limit to the
number of global variables that can be
manipulated by a function.
Sum();
echo $b;
?>
Summary
The real power of PHP comes from its
functions.
Creating functions lets you reuse code
that you’ve used before without rewrite
the whole. This will save you time and
make programming easier.