Tuesday 27 February 2018

Function Programs In PHP?

1)    Display name and age  using function
Demo.html
<html>
<title> Functions </title>
<body>
<form method="POST" action="f1.php">
Name :<input type="text" name="name">
Age : <input type="text" name="uage">
<input type="submit" name="submitbtn" value="Submit">
 </form>
</body>
</html>

f1.php
<?php
$str=$_POST['name'];
$age1=$_POST['uage'];

function display($f, $a)
{
                   echo"values entered by you<br>";
                   echo"Name : $f<br>";
                   echo"Age : $a<br>";
                  
}

function display1($f="Dhananjay", $a=10)//Default Arguments
{
         
                   echo"Name : $f<br>";
                   echo"Age : $a<br>";
}


if(isset($_POST['submitbtn']))
{
         
echo" Passing by value<br>";
 display($str, $age1);//call by Value
 echo"<br>";
         
echo "Parameters are already present in function definition<br>";
display1();
         
echo "Parameters are already present in function definition however, passed from the function<br>";
display1("Yashodhan",1);

echo "Parameters are already present in function definition however, passed  only 1 parameter from the function<br>";
  display1("Deepali");
         
}
?>

2) Addition of two numbers   (using return statement)

Demo.html
<html>
<title> Add 2 numbers using Function(Return) </title>
<body>
<form action="func_ret1.php" method="POST">

Number 1 : <input type ="number" name="num1"></br>
Number 2 : <input type ="number" name="num2"></br>
 <input type ="submit" name="subtn" value="ADD">
</form>
</body>
 </html>

func_ret1.php
<?php
$n1=$_POST['num1'];
$n2=$_POST['num2'];
$res;


if (isset($_POST['subtn']))
{
         
          function add($a,$b)
{
          $res=$a+$b;
          return($res);
}
          $res=add($n1,$n2);
          Echo "Addtion result is $res";
         
}

?>

3) Addition of two numbers (taking value from user)

Demo.html
<html>
<title> Add 2 numbers using Function(Call by Value) </title>
<body>
<form action="fn1.php" method="POST">

Number 1 : <input type ="number" name="num1"></br>
Number 2 : <input type ="number" name="num2"></br>
 <input type ="submit" name="subtn" value="ADD">
</form>
</body>

fn1.php
</html>
<?php
$n1=$_POST['num1'];
$n2=$_POST['num2'];
$res;
if (isset($_POST['subtn']))
{
         
          function add($a,$b)     //function definition
{
          $res=$a+$b;
          echo" Addition of $a and $b = $res";
}
          add($n1,$n2);  //function call
         
}
?>

No comments:

Post a Comment

apply function in R

1) apply function: It takes 3 arguments matrix,margin and function.. Example: m<-matrix(c(1,2,3,4),nrow=2,ncol=2) m #1 indicates it is ap...