You are on page 1of 23

PHP Functions

PHP Functions

The real power of PHP comes from its functions;


it has more than 1000 built-in functions.
PHP User Defined Functions
Besides the built-in PHP functions, we can create
our own functions.
A function is a block of statements that can be
used repeatedly in a program.
A function will not execute immediately when a
page loads.
A function will be executed by a call to the
function.

Create a User Defined Function in PHP

A user defined function declaration starts


with the word "function":
Syntax
function functionName() {
code to be executed;
}

Continued..
A function name can start with a letter or
underscore (not a number).
Give the function a name that reflects what
the function does.
Function names are NOT case-sensitive.

Example
<?php
function writeMsg() {
echo Welcome to PHP Page!";
}
writeMsg(); // call the function
?>
OutPut
Welcome to PHP Page

PHP Function Arguments


Information can be passed to functions
through arguments. An argument is just like
a variable.
Arguments are specified after the function
name, inside the parentheses. You can add
as many arguments as you want, just
separate them with a comma.

Continued..
The following example has a function with one
argument ($fname). When the familyName() function is
called, we also pass along a name (e.g.Ali), and the
name is used inside the function, which outputs several
different first names, but an equal last name:
<?php
function familyName($fname) {
echo "$fname Ahmed.<br>";
}
familyName(Ali");
familyName(Usman");
familyName(Hafeez");
?>

Continued..
Output:
Ali Ahmed
Usman Ahmed
Hafeez Ahmed

Example

<?php
function familyName($fname, $year) {
echo "$fname Ahmed. Born in $year <br>";
}
familyName(Ali", "1975");
familyName(Usman", "1978");
familyName(Hafeez", "1983");
?>

Continued..
Output:
Ali Ahmed. Born in 1975
Usman Ahmed. Born in 1978
Hafeez Ahmed. Born in 1983

PHP Default Argument


Value
The following example shows how to use a default
parameter. If we call the function setHeight()
without arguments it takes the default value as
argument:
<?php
function setHeight($minheight = 50) {
echo "The height is : $minheight <br>";
}
setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
?>

Continued..
Output :
The height is : 350
The height is : 50
The height is : 135

PHP Functions - Returning


values
To let a function return a value, use the return
statement:
<?php
function sum($x, $y) {
$z = $x + $y;
return $z;
}

echo "5 + 10 = " . sum(5, 10) . "<br>";


echo "7 + 13 = " . sum(7, 13) . "<br>";
?>

Continued..
Output:
5 + 10 = 15
7 + 13 = 20

Returning Values from UserDefined Functions

A function can return a value using the


return statement in conjunction with a value
or object. return stops the execution of the
function and sends the value back to the
calling code.

A Function That Returns a Value


<?php
function addNums( $firstnum, $secondnum;
{
$result = $firstnum + $secondnum )
return $result;11:
}
echo addNums(3,5);
// will print "8"
?>

Dynamic Function Calls


It is possible to assign function names as
strings to variables and then treat these
variables exactly as you would the function
name itself. Listing 6.5 creates a simple
example of this.

Calling a Function
Dynamically
<?php
function sayHello()
{
echo "hello<br>";
}
$function_holder = "sayHello";
$function_holder();
?>

Passing References to Variables


to Functions
When you pass arguments to functions they
are stored as copies in parameter variables.
Any changes made to these variables in the
body of the function are local to that function
and are not reflected beyond it. This is
illustrated in Listing 6.13.

<?php
function addFive( $num )
{
$num += 5;
}
$orignum = 10;
addFive( $orignum );
print( $orignum );
?>

Example
<?php
srand( microtime() * 1000000 );
$Rad=rand(1,10);
echo $Rad;
?>

PHP Random Value


Function
The PHPrand()function is used to generate
a random number.This function can generate
numbers with-in a given range. The random
number generator should be seeded to
prevent a regular pattern of numbers being
generated. This is achieved using
thesrand()function that specifies the seed
number as its argument.

Display Images Randomly


<?php
srand( microtime() * 1000000 );
$num = rand( 1, 4 );
switch( $num ) {
case 1: $image_file = "/php/images/logo.png";
break;
case 2: $image_file = "/php/images/php.jpg";
break;
case 3: $image_file = "/php/images/logo.png";
break;
case 4: $image_file = "/php/images/php.jpg";
break;
}
echo "Random Image : <img src=$image_file />";
?>

You might also like