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

<
html>
<
body bgcolor="cyan">
<h3>PHP Function Creation<h3><hr />
<?php
function display()
{
echo "Deepak dwij";
}
echo "My name is ";
display();
?>
</body>
</html>

Output 

Type http://localhost/yourfoldername/function.php at your browser, here your output looks like :


r1.gif


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:

<html>
<
body bgcolor="cyan">
<h3>Single Parameter </h3><hr/ >
<?php
function display($fnm)
{
echo $fnm . " Dwij.<br />";
}
echo "I am ";
display(
"Deepak");
echo "My brother's name is ";
display(
"Vikas");
?>
</body>
</html>

Saved it by function1.php

Output

Type http://localhost/yourfoldername/function1.php

r2.gif

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.

<html>
<
body bgcolor="cyan">
<h3> Two Arguments </h3><hr />
<?php
function display($fnm,$profile)
{
echo $fnm . " _Dwij_" . $profile . "<br />";
}
echo "I am ";
display(
"Deepak ","S/w developer");
echo "My brother's name is ";
display(
"Vikas","Student");
?>
</body>
</
html>

Saved it by function2.php

Output

Type  http://localhost/yourfoldername/function2.php

r3.gif

Creation of PHP function that returns the value

In this part f
unction mul returns a value i.e. 16

<html>
<
body bgcolor="Cyan">
<h3>Function Returns a value</h3><hr />
<?php
function mul($x,$y)
{
$mul=$x*$y;
return $mul;
}
echo "The multiplication of 4*4 = " . mul(4,4);
?>
</body>
</
html>

Saved it by function3.php

Output

Type http://localhost/yourfoldername/function3.php , the function mul returns a value i.e. 16

r4.gif

Up Next
    Ebook Download
    View all
    Learn
    View all