www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons PHP User Defined Functions 1 Bookmark and Share
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
Lesson 41
Lesson 42
Lesson 43
Lesson 44
PHP is a very rich language in terms of its fuctions. There are more than 700 built in fuction available in PHP. PHP also allows us to define our own fuctions.In this lesson we will discuss how we can create our own functions.

In simple words a function is a named block of code that can be executed whenever we need, again and again. All functions start with the word "function()". Function names can start with a letter or an underscore but not with a number. The block of code has to be enclosed between { and }.

Example A simple function that prints "Hello World":

<html>
<body>
<?php
function printHW()
{
echo "Hello World";
}
printVar(); //this is how we call functions in PHP
?>
</body>
</html>


The above was a very simple example of a user defined function. The function in above example does not take any argument and does not return a value. We can define a function that takes arguments and returns a value. Consider the following example:

<?php
function func($arg_1, $arg_2, ..., $arg_n)
{
echo "Example function.\n";
return $retVal;
}
?>

PHP also allows functions to be defined inside functions. This is a rare concept and is not found in many programming languages. The following example shows this concept:

<?php
function func() {
function func2() {
echo "I don't exist until func() is called.\n";
}
}
/* We can't call func2() yet since it doesn't exist. */ func();

/* Now we can call func2(), func()'s processesing has made it
accessible. */
func2(); // this is valid now.
?>


PHP also supports recursive functions:

<?php
function recursive($val) {
if ($val < 10) {
echo "$a\n"; recursive($val + 1);
}
}
?>



Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.