You are on page 1of 4

Name : Shaikh Hassaan

Roll No : 210418
Practical No : 6
1. Write a program to demonstrate pass by value using function
<html>
<body>
<?php
function myFunction($lang){
echo "i love $lang";
}
myFunction("php");
?>
</body>
</html>

2. Write a program to demonstrate pass by reference using function


<html>
<body>
<?php
function myFunction(&$str){
$str = "Dhukka loves to play";
}
$str = "Dhukka loves to sleep";
echo "before changing the reference $str";
myFunction($str);
echo "<br>$str";
?>
</body>
</html>

3. Write a program to demonstrate default argument in function


<html>
<body>
<?php
function myFunction($name="hukka"){
$name = "<br>The name is $name";
echo "$name";
}
echo "without default argument";
myFunction("hassaan");
echo "<br>with default arguement";
myFunction();
?>
</body>
</html>

4. Write a program to create a variable function


<html>
<body>
<?php
function aging($age){
echo "the age is $age";
}
$x = "aging";
$x(18);
?>
</body>
</html>

5. Write a program for anonymous function using variable


<html>
<body>
<?php
$add = function ($n1,$n2){
$sum = $n1+$n2;
return $sum;
};
echo "The addition is ".$add(18,18);
?>
</body>
</html>
6. Write a program for anonymous function using array
<html>
<body>
<?php
$arr = [function($name){
echo "the name is $name-210418";
},function($name){
echo "<br>the name is $name-210421";
}];
$narr = $arr[0]("Hassaan");
$harr = $arr[1];
$harr("Haris");
?>
</body>
</html>

7. Write a program to implement the concept of callback using anonymous function


<html>
<body>
<?php
$arr1 = [1,2,3,4,5];
$arr2 = array_map('myFunction',$arr1);
function myFunction($num){
return $num*$num;
}
echo "Original array<br>";
print_r($arr1);
echo "<br>New Array<br>";
print_r($arr2);
?>
</body>
</html>
8. Write a program to implement the concept of closure using anonymous function
<html>
<body>
<?php
$name = "Hukka";
$msg = function() use ($name){
echo "The name is $name-210418,21";
};
$msg();
?>
</body>
</html>

You might also like