Wednesday, 17 June 2020

php functions



Php functions

<?php  
function sayHello(){  
echo "Hello PHP Function";  
}  
sayHello();//calling function  
?>  


Hello PHP Function


PHP Function Arguments


<?php  
function sayHello($name){  
echo "Hello $name<br/>";  
}  
sayHello("Sonoo");  
sayHello("Vimal");  
sayHello("John");  
?>  
Output

Hello Sonoo
Hello Vimal
Hello John


Php function with arguments

<?php  
function sayHello($name,$age){  
echo "Hello $name, you are $age years old<br/>";  
}  
sayHello("Sonoo",27);  
sayHello("Vimal",29);  
sayHello("John",23);  
?>  


Output

Hello Sonoo, you are 27 years old
Hello Vimal, you are 29 years old
Hello John, you are 23 years old


Php call by reference


<?php  
function adder(&$str2)  
{  
    $str2 .= 'Call By Reference';  
}  
$str = 'Hello ';  
adder($str);  
echo $str;  
?>  

Hello Call By Reference


PHP Function: Default Argument Value


<?php  
function sayHello($name="Sonoo"){  
echo "Hello $name<br/>";  
}  
sayHello("Rajesh");  
sayHello();//passing no value  
sayHello("John");  
?>  


Output

Hello Rajesh
Hello Sonoo
Hello John


PHP Function: Returning Value


<?php  
function cube($n){  
return $n*$n*$n;  
}  
echo "Cube of 3 is: ".cube(3);  
?>  


Cube of 3 is: 27



function using input
<?php  
//add() function with two parameter  
function add($x,$y)    
{  
$sum=$x+$y;  
echo "Sum = $sum <br><br>";  
}  
//sub() function with two parameter  
function sub($x,$y)    
{  
$sub=$x-$y;  
echo "Diff = $sub <br><br>";  
}  
//call function, get  two argument through input box and click on add or sub button  
if(isset($_POST['add']))  
{  
//call add() function  
 add($_POST['first'],$_POST['second']);  
}     
if(isset($_POST['sub']))  
{  
//call add() function  
sub($_POST['first'],$_POST['second']);  
}  
?>  
<form method="post">  
Enter first number: <input type="number" name="first"/><br><br>  
Enter second number: <input type="number" name="second"/><br><br>  
<input type="submit" name="add" value="ADDITION"/>  
<input type="submit" name="sub" value="SUBTRACTION"/>  
</form>     







PHP Call By Value


<?php  
function adder($str2)  
{  
    $str2 .= 'Call By Value';  
}  
$str = 'Hello ';  
adder($str);  
echo $str;  
?>  


Hello




php call by refernce

<?php  
function adder(&$str2)  
{  
    $str2 .= 'Call By Reference';  
}  
$str = 'This is ';  
adder($str);  
echo $str;  
?>  



output

This is Call By Reference





$a .= $b => $a = $a.$b



ist str loads by this is and addr functions calls with argument of $str, it gets the value which is in address of variable a, variable stores some value, that variable has address
the value loads in loads in str2, concatenate and prints

calles the address

call by referencve

  1. <?php  
  2. function increment(&$i)  
  3. {  
  4.     $i++;  
  5. }  
  6. $i = 10;  
  7. increment($i);  
  8. echo $i;  
  9. ?>  


output

11



PHP Variable Length Argument Function


  1. <?php  
  2. function add(...$numbers) {  
  3.     $sum = 0;  
  4.     foreach ($numbers as $n) {  
  5.         $sum += $n;  
  6.     }  
  7.     return $sum;  
  8. }  
  9.   
  10. echo add(1, 2, 3, 4);  
  11. ?>  



to do factorials


  1. <?php    
  2. function factorial($n)    
  3. {    
  4.     if ($n < 0)    
  5.         return -1; /*Wrong value*/    
  6.     if ($n == 0)    
  7.         return 1; /*Terminating condition*/    
  8.     return ($n * factorial ($n -1));    
  9. }    
  10.     
  11. echo factorial(5);    
  12. ?>    
first it retursn 5*factorial(5-1), in second loop 5 alrady there, 4* factorial(3)
loop



No comments:

Post a Comment