SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

Functions in PHP

We can use a function in PHP to execute a specific task or a set of tasks. Function is used when a specific code is used again and again. It is better to keep the code inside a function and call the function to perform the same task. We can pass variables to a function and after processing the function will return the value to the main script. So a function is a set of or block of code to do a particular task. PHP has got many built-in functions for us to use. We will discuss on user defined functions here. Let us see how to define a function.





<?
function my_function($var1,$var2){
// Place to enter code here.
print "Value of first variable = $var1";
print "<br>";
print "Value of second variable = $var2";
}
/*The above lines are a declaration of a function which takes two inputs. We can call a function like this. */
my_function("first value", "Second value");
?>

You can see above we have kept the code inside a function and call it from our main script. Two variables we have passed to the function and displayed them using print command. Here print is a built-in function of PHP.

Discuss this tutorial at forum


Further readings
PHP Function
Declaring variables as global inside the function
Returning variable from a function
Returning an array from a function to main script
Array as input to a function








 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.