Professional Documents
Culture Documents
Functions
A function is a subroutine that allows you to perform a set of instructions by simply
calling this function in the body of the main program. The functions execute in several parts of
the program a series of instructions, this allows simplicity of the code and therefore a minimum
program size.
PHP has a large collection of internal or built-in functions that you can call directly in
your PHP scripts to perform a specific task, like gettype ( ), print_r (), var_dump , etc.
For a complete reference and examples of the built-in functions. Please see the PHP
References section for a complete list of useful PHP built-in functions, PHP Functions
Reference.
In addition to built-in functions, PHP also allows you to define your own functions. It's
a way to create reusable packages of code that perform specific tasks and can be stored and
maintained separately from the main program. Here are some benefits of using these features:
- Functions reduce code repetition in a program: Functions allow you to extract the
most used blocks of code into a single component. You can now perform the same task
by calling this function wherever you want in your script without having to copy and
paste the same block of code repeatedly.
- Functions make code much easier to maintain: Since a function created once can be
used multiple times, any changes made inside a function are automatically implemented
wherever it is applied without touching to different files.
- Functions make it easier to eliminate errors: When the program is divided into
functions, if an error occurs, you know exactly which function is causing the error and
where to find it. Therefore, fixing errors becomes much easier.
- Functions can be reused in other applications : As a function is separated from the
rest of the script, it is easy to reuse the same function in other applications by simply
including the php file containing these functions.
The following section shows how to easily define your own function in PHP.
1.3.Create and use functions in PHP
The basic syntax for creating a custom function can be given as follows: A function will
be executed by a call to the function.
Syntax:
function FunctionName ()
code to execute;
Example :
A simple function that writes my name when called I named it writeName :
<html> _ _
<? php
function writeName ()
?>
</head> _ _
<p> _ _
<? php
writeName ();
?>
</p>
</body>
</html>
Syntax:
code to execute;
Example 1 : The following example will write different first names for the same last
name:
<html> _ _
?>
</body>
</html>
<html> _ _
< body >
<? php
function writeName ($ fname , $ detail )
{
echo $ fname . "the GOOD is ". $ detail . "< br /> ";
}
$cars2= "Volvo";
$cars3= "BMW";
However, what if you want to loop around and find a specific car? What if you didn't
just have 3 cars, but 300? The best solution here is to use an array !
An array can hold all your variable values under a single name. And you can access the
values by referring to the name of the array .
Each element in the array has its own index so that it can be easily accessed. In PHP,
there are three types of arrays :
In the following example the indexes are automatically assigned (the index starts at 0):
<? PHP
$cars[0] = "Renault";
$cars[1] = "Volvo";
$cars[2] = "BMW";
$cars[3] = "Toyota";
echo $cars[1]. ", ". $cars[2]." and ".$ cars[3]. "are not French cars.";
?>
With associative arrays we can use values as keys and assign values to them.
Example 1
In this example we assume that we have a member's area in our site, visitors register to
become a member. We want to store data about a member who logs into his member's area:
Example 2
$member["login"] = "doggie";
$member[" pass "] = "abcd30";
$member["mail"] = "toutou@serveur.com";
$member["status"] = "basic";
Identification keys can be used in php code :
<? PHP
$member["login"] = "doggie";
$member[" pass "] = "abcd30";
$member["mail"] = "toutou@serveur.com";
$member["status"] = "basic";
echo "hello: ". $member ["login"]. "your password is: ". $member[" pass "]."
";
echo "and your email address is: ". $member ["email"]. "your status is: ".
$member["status" ];
?>
2.3.Multidimensional arrays
In a multidimensional array, each element in a main array can also be an array . And
each element in the secondary array can also be an array , and so on.
Example
$families = array
);
<? php
$families = array
("Griffin " => array (" Peter","Lois","Megan "),
"Swamp" => array ("Glenn"),
"Brown " => array (" Cleveland","Loretta","Junior ")
);
echo $families ["Griffin"] [2]." is part of the Griffin family?
";
echo $families ["Brown"] [0]." is part of Brown's family?" ;
?>