Home >>PHP Object Oriented >PHP Function

PHP Function

Create Own function in PHP

Function are self contained block of statement which used to perform any specific task.

Types of Function

  1. system defined/library/inbuilt
  2. user defined

Advantages Of Function

  • A function is created once but used many times, often from more than one program.
  • It reduces duplication within a program.
  • Debugging and testing a program becomes easier when the program is subdivide.

How to define Function

A function will be executed by a call to the function. Syntax

function function_name( )
	  {
		code to be executed;	
	  }	

A simple function that shows my name when it is called.

<?php

	  //define function and implements it
function writeName()
		
{
		
 echo "phptpoint";
		
}  
	  
//call function whenever you need
	 
 writeName();
	 
 writeName();

?>

Output phptpoint phptpoint

in the above example first define a function( ) writeName. inside function body print "phptpoint" string using echo statement. To call a function always only function name is required.


Create add( ) function

<?php
	
function add()
	
{
         	
$x=1000;
         	
$y=500; 
		 
$sum=$x+$y;
         	
echo "sum of given no=".$sum;
            
}
	
function sub()
	   
{
         	
$x=1000;
         	
$y=500; 
		
 $sub=$x-$y;
         	
echo "subtraction of given no=".$sub;
            
}
	  
//call function whenever you need
	  
add();
	  
sub();

?>
Output Sum of given no=1500 subtraction of given no=500

In the above example. define function add( ). inside the body declare two variable $x,$y with value=1000,500. Add these variable result store in variable ($sum=$x+$y). print sum. Another function sub( ), subtraction of these variable stored in $sub variable. To call function : add( ) add function being called sum of two no is shown sub( ) sub function being called subtraction of two no is shown.


There are 3 types of variables i.e used inside a function

  1. Local
  2. Global
  3. Static


Create a dynamic function to find the sum of N numbers

<?php

error_reporting(1);

function add()

{

$str=$_POST['t1'];

$arr=explode(",",$str);

$sum=0;

$l=count($arr);

for($i=0;$i<$l;$i++)

{

$sum=$sum+$arr[$i];

}

echo "sum is:".$sum;

}


if(isset($_POST['b1']))

{

add();

}

?>


<form method="post">

enter the numbers:<input type="text" name="t1"><br>

<input type="submit" name="b1" value="add">

</form>

Output sum is : 15 enter the numbers:

In the above example Create a textbox and submit button using HTML script. declare function add( ) inside PHP script. inside this function,$_POST[ ] is used to collect inputted value. explode() function is used to convert string into array. Now user inputs value=1,2,3,4,5. use explode function convert this value to an array form because <input type="text" > always accept string value. variable $sum=0. count($arr) count the element of an array, stored in variable ($l). now start for-loop loop iterate from $i=0, to $i<$l(no of element of an array). $sum=$sum+$arr[$i] perform addition of input number. Call function add( ) inside isset( ). output will display.


Create a dynamic function to Count the even and odd numbers

<?php

error_reporting(1);

function check()

{

$str=$_POST['t1'];

$arr=explode(",",$str);

$c1=0;

$c2=0;

$l=count($arr);

for($i=0;$i<$l;$i++)

{

if($arr[$i]%2==0)

{

$c1++;

}

else

{

$c2++;

}

}

echo "Total even number =".$c1.'<br>';

echo "Total odd number =".$c2.'<br>';

}

if(isset($_POST['b1']))

{

check();

}

?>

<form method="post">

enter the numbers:<input type="text" name="t1"><br>

<input type="submit" name="b1" value="Count">

</form>

Output Total even number = 4 Total odd number = 3 enter the numbers:

In the above example Create function check( ). $_POST[ ] is used to collect user input ,explode(",",$str) function convert string into an array, declare inside body of function. variable $c1, $c2 initial hold value=0, $l store the number of element of an array. count($arr) count number elements of an array. use for loop start from $i=0 to $i<$l, if first number is even. if condition execute calculate modulus of the number, $c1++ count the number of even. otherwise else condition execute c2++ count the number of odd. call the function when submit button click, output will show count the even and odd number.


Create a dynamic function to find the sum of even and odd numbers

<?php

error_reporting(1);

function check()

{

$str=$_POST['t1'];

$arr=explode(",",$str);

$c1=0;

$c2=0;

$l=count($arr);

for($i=0;$i<$l;$i++)

{

if($arr[$i]%2==0)

{

$c1=$c1+$arr[$i];

}

else

{

$c2=$c2+$arr[$i];

}

}

echo "sum of even = ".$c1.'<br>';

echo "sum of odd = ".$c2.'<br>';

}

if(isset($_POST['b1']))

{

check();

}

?>

<form method="post">

enter the numbers:<input type="text" name="t1"><br>

<input type="submit" name="b1" value="add">

</form>

Output sum of even = 36 sum of odd = 39 enter the numbers:

In the above example Create a function check( ) value entered by user collect by $_POST['t1'], explode( ) function convert this value into an array. Declare two variable $c1, $c2 initially hold value=0. Now start for loop. if first element of an array is even, if condition execute and gave sum of even number otherwise it will give sum of odd number. call check() function to display the result.


No Sidebar ads