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.
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.
// Place to enter code here.
print "Value of first variable = $var1";
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");
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 ";
Number of User Comments : 1