PHP Function - learning php -

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.

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


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

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

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

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

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.