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 built-in functions

Based on the common tasks required PHP has built in functions available at script level to use. These functions need not be declared and can directly used. Some common examples are string functions, math function and other basic functions to develop scripts.

User defined functions

We can write code and define function for our use in our scripts, we will learn more about these functions here. We will learn how to declare a function, pass a variable or an array and how to return variables or array to main script or calling script. 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.

Testing function support.

We can check if the function support is available or not by using function_exists() function. This function returns TRUE if function is available or else FALSE.

Here is a sample script using function_exists()

echo "<br>crypt function support is available ";
echo "<br>crypt function support is NOT available ";

Returning value from a Functions to main script

Now let us modify the function we discussed and see how we can return value from a php function to our main script. Here we will send three numeric values to the function and get back the sum of the three values in our main script. Here the function will receive three arguments or variables and process it. We will use return command to return the end result to the main script from the function.

function my_function($var1,$var2,$var3){
// Place to enter code here.
return $var;

echo "The return value= $my_val";
// The above line will print the sum of the three variables.

We have seen here how the value is returned to the main calling script by using return command. To access the variables declared in the main script inside the function, we have to declare them as global inside the function.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
Visitors Rating
Your Rating




nice one, but should elaborate function exist more

Post Comment This is for short comments only. Use the forum for more discussions.

Subscribe to our mailing list

* indicates required
Subscribe to plus2net
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer