PHP Function

Create Own function in PHP

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

Types of Function

system defined/library/inbuilt

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

1

2

3

4

functionfunction_name()

{

code tobe executed;

}

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

<?php

//define function and implements it

functionwriteName()

{

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

<?php

functionadd()

{

$x=1000;

$y=500;

$sum=$x+$y;

echo"sum of given no=".$sum;

}

functionsub()

{

$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.

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

Create a dynamic function to Count the even and odd numbers

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

<?php

error_reporting(1);

functioncheck()

{

$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

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

<?php

error_reporting(1);

functioncheck()

{

$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.