Working with PHP functions with XAMPP

PHP is a very well known in the world of web-development & is a powerful tool for making dynamic and interactive Web pages. The real strength of PHP is provided by its functions.
 
In this section we are going to understand with php functions. You must installed a XAMPP server on your PC.
 

Syntax of a PHP function

 
function funName()
{
    desired code;
}
 
We can create functions by various ways in our php scripting some of them are as under :
  1. Creation of function in PHP
  2. Creation of function in PHP : Adding arguments/parameters
  3. Creation of function in PHP : Two parameters
  4. Creation of PHP function that returns the value

Creation of a function in PHP

 
A function will be executed by a call to the function. Functions takes the responsibility to perform a work so its very important part any technology we use. This is the php scripting for creation a function named display(). In this function only a name "Deepak dwij " will print.When we call a function display() it will print a name Deepak dwij.
 
Open notepad and type the following code and save it as "function.php"  inside xampp-htdocs-yourfolder-function.php
  1. <html>  
  2. <body bgcolor="cyan">  
  3. <h3>PHP Function Creation<h3><hr />  
  4. <?php  
  5. function display()  
  6. {  
  7. echo "Deepak dwij";  
  8. }  
  9. echo "My name is ";  
  10. display();  
  11. ?>  
  12. </body>  
  13. </html>  
Output
 
Type http://localhost/yourfoldername/function.php at your browser, here your output looks like :
 
PHP functions
 

Creation of function in PHP : Adding arguments/parameters

 
Parameters to the function provides the extra functionality to the functions. A parameter is just like a variable. Parameters are defined after the function name, inside the parentheses.
 
Syntax
 
function funName(agru 1,argu 2, argu 3......) // parameterized function
 
First of all we will work with the single argumented function, here is the example given below:
  1. <html>  
  2. <body bgcolor="cyan">  
  3. <h3>Single Parameter </h3><hr/ >  
  4. <?php  
  5. function display($fnm)  
  6. {  
  7. echo $fnm . " Dwij.<br />";  
  8. }  
  9. echo "I am ";  
  10. display("Deepak");  
  11. echo "My brother's name is ";  
  12. display("Vikas");  
  13. ?>  
  14. </body>  
  15. </html>  
Saved it by function1.php
 
Output
 
Type http://localhost/yourfoldername/function1.php
 
PHP functions with one parameter 
 

Creation of function in PHP : Two Parameters/Arguments 

 
In this part a function is having two arguments one is for firstname and other for profile. The function name display take two arguments.
  1. <html>  
  2. <body bgcolor="cyan">  
  3. <h3> Two Arguments </h3><hr />  
  4. <?php  
  5. function display($fnm,$profile)  
  6. {  
  7. echo $fnm . " _Dwij_" . $profile . "<br />";  
  8. }  
  9. echo "I am ";  
  10. display("Deepak ","S/w developer");  
  11. echo "My brother's name is ";  
  12. display("Vikas","Student");  
  13. ?>  
  14. </body>  
  15. </html>  
Saved it by function2.php
 
Output
 
Type http://localhost/yourfoldername/function2.php
 
PHP functions with two parameters 
 

Creation of PHP function that returns the value

 
In this part function mul returns a value i.e. 16
  1. <html>  
  2. <body bgcolor="Cyan">  
  3. <h3>Function Returns a value</h3><hr />  
  4. <?php  
  5. function mul($x,$y)  
  6. {  
  7. $mul=$x*$y;  
  8. return $mul;  
  9. }  
  10. echo "The multiplication of 4*4 = " . mul(4,4);  
  11. ?>  
  12. </body>  
  13. </html>  
Saved it by function3.php
 
Output
 
Type http://localhost/yourfoldername/function3.php, the function mul returns a value i.e. 16
 
PHP functions with return value