Thursday, December 24, 2009

PHP Tutorial- Functions

In this chapter you will be getting technical know how of functions. Functions are useful in any type of programming language. Whenever you want to use a particular piece of code over and over again, it is very useful to put that code in a function. This is done so that the code can be reused easily. Another use of using functions is that whenever there is an error in the code, and then it has to be corrected only at one place; in the function. With the use of functions, the entire code can be understood easily.


Declaring Functions


Creating your own function in your script is a straightforward task. It is like creating a function in any other language. The functions in PHP begin with the keyword"function" which is followed by the function name. Following the function name there is a set of parenthesis which contains the parameters to be passed. These parameters are an optional set of variables passed to the function.









Syntax

function <name of function> ([$var1 [= constant]], [$var2 [= constant]], ...)


{


Body of the function;


}

 

Your First Function


Now that you have got an idea of th2e functions, you are ready to start with functions. In the following example the string "Welcome to vudevelopers!" will be printed five times on the browser with the help of functions. By writing a single code once in a function, we can call the same function again in a script.

















































Example

<html>


<body>


<?php


function fundisp()


{


for($i = 0; $i < 5; $i++)


{


echo "Welcome to vudevelopers!<br/>";


}


}


echo "After this statement the function will be called.<br/><br/>";


fundisp();


echo "<br/>";


echo "This statement is printed after the function has been called.";


echo "<br/>";


?>


</body>


</html>

 
 

Output



Passing Parameters to Functions


In this section we will be discussing about how to pass parameters to a function in PHP. A parameter of a function is a piece of data that a function requires to execute. If we define parameters formally then, function parameters are represented by variable names which are located within the parenthesis of the function definition.


In the following example of passing parameters to functions we will be displaying the string "Welcome to vudevelopers!." The difference that this example has got with the previous example is that, in this example the number of times the text will be displayed is not fixed, rather that number is passed as a parameter when the function is called.


















































Example


<html>


<body>


<?php


function fundisp($num)


{


for($i = 0; $i < $num; $i++)


{


echo "Welcome to vudevelopers!<br/>";


}


}


echo "After this statement the function will be called.<br/><br/>";


fundisp(7);


echo "<br/>";


echo "This statement is printed after the function has been called.";


echo "<br/>";


?>


</body>


</html>


Output


No comments:

Post a Comment