php (2)
php (2)
return $sum;
}
$number = 12345;
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q2. Write a PHP script to remove duplicate elements from an array.
Program:-
<?php
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q3. Write a PHP program to sort an array in ascending order without using built-in
functions.
Program:-
<?php
function bubbleSortAscending(&$array) {
$n = count($array);
for ($i = 0; $i < $n - 1; $i++) {
for ($j = 0; $j < $n - $i - 1; $j++)
{ if ($array[$j] > $array[$j +
1]) {
// Swap the elements
$temp = $array[$j];
$array[$j] = $array[$j + 1];
$array[$j + 1] = $temp;
}
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
Subject: Web Technologies DATE: 19/03/2025
Q4. Write a PHP script to merge two arrays and sort them.
Program:-
<?php
$array1 = array(3, 1, 7);
$array2 = array(4, 2, 6);
$mergedArray = array_merge($array1, $array2);
sort($mergedArray); // This is a built-in function for sorting
print_r($array1);
echo "Array 2: ";
print_r($array2);
echo "Merged and Sorted Array: ";
print_r($mergedArray);
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q5. Write a PHP program to check if an array contains a specific value.
Program:-
<?php
if (in_array($searchValue, $fruits)) {
echo "The value '$searchValue' exists in the array.";
} else {
echo "The value '$searchValue' does NOT exist in the array.";
}
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q6. Write a PHP script to find the second largest number in an
array.
Program:-
<?php
function findSecondLargest($array) {
$largest = $secondLargest = null;
foreach ($array as $num) {
if ($largest === null || $num > $largest) {
$secondLargest = $largest;
$largest = $num;
} elseif ($num != $largest && ($secondLargest === null || $num > $secondLargest)) {
$secondLargest = $num;
} }
return $secondLargest;
}
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q7. Write a PHP program to check if a number is an Armstrong number.
Program:-
<?php
function isArmstrong($number) {
$numStr = strval($number);
$numDigits = strlen($numStr);
$sum = 0;
$temp = $number;
while ($temp > 0) {
$digit = $temp % 10;
$sum += pow($digit, $numDigits);
$temp = (int)($temp / 10);
}
return $sum === $number;
}
$number = 153;
if (isArmstrong($number)) {
echo "$number is an Armstrong number.";
} else {
echo "$number is NOT an Armstrong number.";
}
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q8. Write a PHP script to convert a string to uppercase without using built-in functions.
Program:-
<?php
function toUppercase($str) {
$uppercaseStr = "";
for ($i = 0; $i < strlen($str); $i++) {
$char = $str[$i];
$ascii = ord($char);
if ($ascii >= 97 && $ascii <= 122) {
return $uppercaseStr;
}
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q9. Write a PHP script to check if a given number is even or odd.
Program:-
<?php
function checkEvenOrOdd($number)
{ if ($number % 2 == 0) {
echo "$number is Even.";
} else {
echo "$number is Odd.";
}
$number = 7;
checkEvenOrOdd($number);
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q10. Write a PHP program to find the largest of three numbers.
Program:-
<?php
function findLargest($a, $b, $c) {
if ($a >= $b && $a >= $c) {
return $a;
} elseif ($b >= $a && $b >= $c) {
return $b;
} else {
return $c;
}
$num1 = 45;
$num2 = 78;
$num3 = 23;
$largest = findLargest($num1, $num2, $num3);
echo "The largest of $num1, $num2, and $num3 is: $largest";
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q11. Write a PHP script to check if a given string is a palindrome.
Program:-
<?php
function isPalindrome($string) {
$cleaned = strtolower(str_replace(' ', '', $string));
$reversed = '';
for ($i = strlen($cleaned) - 1; $i >= 0; $i--) {
$reversed .= $cleaned[$i]; }
$cleaned === $reversed;}
$input = "Madam";
if (isPalindrome($input)) {
echo "'$input' is a palindrome.";
} else {
echo "'$input' is NOT a palindrome.";
}
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q12. Write a PHP program to count the number of words in a string.
Program:-
<?php
function countWords($string) {
$trimmed = trim($string);
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q13. Write a PHP script to reverse a string without using built-in functions.
Program:-
<?php
function reverseString($string) {
$reversed = '';
$length = strlen($string);
for ($i = $length - 1; $i >= 0; $i--) {
$reversed .= $string[$i];
}
return $reversed;
}
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q14. Write a PHP script to calculate the factorial of a number using recursion.
Program:-
<?php
function factorial($n) {
// Base case
if ($n <= 1)
{ return 1;
}
return $n * factorial($n - 1);
}
$number = 5;
echo "The factorial of $number is: " . factorial($number);
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q15. Write a PHP program to check whether a number is prime or not.
Program:-
<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}
$number = 17;
if (isPrime($number)) {
echo "$number is a prime number.";
} else {
echo "$number is NOT a prime number.";
}
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q16. Write a PHP script to print the Fibonacci series up to a given number.
Program:-
<?php
function printFibonacciUpTo($limit) {
$a = 0;
$b = 1;
echo "Fibonacci series up to $limit:\n";
while ($a <= $limit) {
echo $a . " ";
$next = $a + $b;
$a = $b;
$b = $next;
}
$limit = 100;
printFibonacciUpTo($limit);
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q17. Write a PHP script to check whether a year is a leap year.
Program:-
<?php
function isLeapYear($year) {
if (($year % 4 == 0 && $year % 100 != 0) || ($year % 400 == 0)) {
return true;
} else {
return false;
}}
$year = 2024;
if (isLeapYear($year)) {
echo "$year is a leap year.";
} else {
echo "$year is NOT a leap year.";
}
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q18. Write a PHP script to implement a simple calculator using switch case.
Program:-
<?php
function calculator($num1, $num2, $operator) {
switch ($operator) {
case '+':
return $num1 + $num2;
case '-':
return $num1 - $num2;
case '*':
case 'x':
case 'X':
return $num1 * $num2;
case '/':
if ($num2 == 0) {
return "Error: Division by zero!";
$num1 = 10;
$num2 = 5;
$operator = '*';
$result = calculator($num1, $num2, $operator);
echo "Result: $num1 $operator $num2 = $result";
?>
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q19. Write a PHP script to count the occurrences of a specific word in a string.
Program:-
<?php
function countWordOccurrences($string, $word) {
$string = strtolower($string);
$word = strtolower($word);
$words = explode(' ', $string);
$count = 0;
foreach ($words as $w) {
// Remove punctuation (optional)
$cleaned = preg_replace('/[^a-z0-9]/', '', $w);
if ($cleaned === $word) {
$count++;
}
}
return $count;
}
$text = "The quick brown fox jumps over the lazy dog. The dog was not amused.";
$targetWord = "the";
echo "The word '$targetWord' appears " . countWordOccurrences($text, $targetWord) . " time(s).";
?>
Output:-
NAME: Manthan Surkar PRN No: ADT24MGTM0946
DIV: B CLASS: MCA-I Sem-II (DS)
SUBJECT: Web Technologies DATE: 19/03/2025
Q20. Write a PHP script to reverse a number.
Program:-
<?php
function reverseNumber($number) {
$reversed = 0;
$isNegative = $number < 0;
$number = abs($number);
$num = 12345;
echo "Original number: $num\n";
Output:-