You are on page 1of 4

DEPARTMENT OF COMPUTER ENGINEERING

Subject: Web Based Application Subject Code: 22616


Development with PHP
Semester: 6th Semester Course: Computer Engineering

Laboratory No: Name of Subject Teacher: Prof. Sneha


Patange
Name of Student: Harshraj Mandhare Roll Id: 21203A0062

Experiment No: 6

Title of Write a simple PHP program to demonstrate use of simple


Experiment function and parameterized function.

Practical related Questions:


1. What is anonymous function?
In PHP, an anonymous function is a function that is created without a name and is
assigned to a variable or passed as an argument to another function.
2. Write the difference between built in function & user defined function.
 Built-in functions are pre-defined in PHP, while user-defined functions are
created by the user.
 Built-in functions are generally faster than user-defined functions.
 Built-in functions are available to use without having to write any code, while
user-defined functions must be defined before they can be used.
 Built-in functions are often used for common tasks such as string manipulation,
while user-defined functions are used for more specific tasks or to encapsulate
a set of related tasks.

 What is variable function?


In PHP, a variable function is a function that can be dynamically called based on the
value of a variable. This means that the function name can be stored in a variable, and
the function can be called by using the variable name as if it were the actual function
name.
Exercise:
1. Write a code to perform addition of 3 numbers using function.
function addfunc($num1, $num2,$num3){
$sum = $num1 + $num2 + $num3;
echo "Sum of the three numbers is : $sum";
}
addfunc(50, 20, 10);

2. Write a PHP program to check whether number is even or odd using function.

function check($number){
if($number % 2 == 0){
echo "<br>Even";
}
else{
echo "<br>Odd";
}
}
$number = 20;
echo 'Number is: ' . $number;
check($number)

3. Write a PHP program to print factorial of number using function.


function Factorial($number){
$factorial = 1;
for ($i = 1; $i <= $number; $i++){
$factorial = $factorial * $i;
}
return $factorial;
}
$number = 5;
$fact = Factorial($number);
echo "Factorial = $fact";

4. Write PHP program to calculate the sum of digits using function.


function sum($num){
$sum = 0;
for ($i = 0; $i < strlen($num); $i++){
$sum += $num[$i];
}
return $sum;
}
$num = "234";
echo 'Sum of no: '.sum($num);

5. PHP program to check whether a number is prime or not using function.

echo 'Prime number or Not<br>';


function primeCheck($number){
if ($number == 1)
return 0;
for ($i = 2; $i <= $number/2; $i++){
if ($number % $i == 0)
return 0;
}
return 1;
}
$number = 1;
$flag = primeCheck($number);
if ($flag == 1)
echo "$number is a Prime";
else
echo "$number is Not Prime";

You might also like