Home >>PHP Object Oriented >PHP Function
Function are self contained block of statement which used to perform any specific task.
A function will be executed by a call to the function. Syntax
function function_name( ) { code to be executed; }
<?php //define function and implements it function writeName() { echo "phptpoint"; } //call function whenever you need writeName(); writeName(); ?>
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.
<?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(); ?>
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.
<?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>
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.
<?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>
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.
<?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>
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.