Show List

PHP Coding Questions


  • Write a PHP script to print the numbers from 1 to 100. But for multiples of three print "Fizz" instead of the number and for the multiples of five print "Buzz". For numbers that are multiples of both three and five print "FizzBuzz".
php
Copy code
for ($i = 1; $i <= 100; $i++) { if ($i % 15 == 0) { echo "FizzBuzz\n"; } else if ($i % 3 == 0) { echo "Fizz\n"; } else if ($i % 5 == 0) { echo "Buzz\n"; } else { echo "$i\n"; } }
  • Write a PHP function to calculate the factorial of a number.
php
Copy code
function factorial($n) { if ($n == 0) { return 1; } else { return $n * factorial($n - 1); } } echo factorial(5); // Output: 120
  • Write a PHP program to find the second largest number in an array.
php
Copy code
function secondLargest($arr) { $first = $second = PHP_INT_MIN; for ($i = 0; $i < count($arr); $i++) { if ($arr[$i] > $first) { $second = $first; $first = $arr[$i]; } else if ($arr[$i] > $second && $arr[$i] != $first) { $second = $arr[$i]; } } return $second; } $arr = [10, 20, 30, 40, 50]; echo secondLargest($arr); // Output: 40
  • Write a PHP program to count the number of words in a string.
php
Copy code
function countWords($str) { $words = explode(' ', $str); return count($words); } $str = "This is a test string."; echo countWords($str); // Output: 5
  • Write a PHP program to check whether a number is prime or not.
php
Copy code
function isPrime($n) { if ($n <= 1) { return false; } for ($i = 2; $i <= sqrt($n); $i++) { if ($n % $i == 0) { return false; } } return true; } echo isPrime(7); // Output: true
  • Write a PHP program to remove duplicates from an array.
php
Copy code
function removeDuplicates($arr) { return array_values(array_unique($arr)); } $arr = [10, 20, 30, 20, 40, 10]; print_r(removeDuplicates($arr)); // Output: Array ( [0] => 10 [1] => 20 [2] => 30 [4] => 40 )
  • Write a PHP program to check whether a string is a palindrome or not.
php
Copy code
function isPalindrome($str) { $str = str_replace(' ', '', $str); $str = strtolower($str); return $str == strrev($str); } $str = "A man a plan a canal Panama"; echo isPalindrome($str); // Output: true
  • Write a PHP program to find the length of the longest consecutive sequence in an array.
php
Copy code
function longestConsecutive($arr) { $hash = array_flip($arr); $longest = 0; foreach ($arr as $num) { if (!isset($hash[$num-1])) { $current = $num; $length = 1; while (isset($hash[$current+1])) { $current++; $length++; } $longest = max($longest, $length); } } return $longest; } $arr = [100, 4, 200, 1, 3, 2]; echo longestConsecutive($arr); // Output: 4
  • Write a PHP program to find the largest sum of contiguous subarray in an array.
php
Copy code
function maxSubarraySum($arr) { $max = $current = $arr[0]; for ($i = 1; $i < count($arr); $i++) { $current = max($arr[$i], $current + $arr[$i]); $max = max($max, $current); } return $max; } $arr = [-2,1,-3,4,-1,2,1,-5,4]; echo maxSubarraySum($arr); // Output: 6
  • Write a PHP program to find the missing number in an array of consecutive integers.
php
Copy code
function missingNumber($arr) { $n = count($arr) + 1; $sum = ($n * ($n + 1)) / 2; $sumArr = array_sum($arr); return $sum - $sumArr; } $arr = [1, 2, 4, 5, 6]; echo missingNumber($arr); // Output: 3
  • Write a PHP program to find the first non-repeating character in a string.
php
Copy code
function firstNonRepeatingChar($str) { $count = array_count_values(str_split($str)); foreach ($count as $char => $num) { if ($num == 1) { return $char; } } return null; } $str = "leetcode"; echo firstNonRepeatingChar($str); // Output: "l"
  • Write a PHP program to find the maximum product of two integers in an array.
php
Copy code
function maxProduct($arr) { $max1 = $max2 = PHP_INT_MIN; $min1 = $min2 = PHP_INT_MAX; foreach ($arr as $num) { if ($num > $max1) { $max2 = $max1; $max1 = $num; } else if ($num > $max2) { $max2 = $num; } if ($num < $min1) { $min2 = $min1; $min1 = $num; } else if ($num < $min2) { $min2 = $num; } } return max($max1 * $max2, $min1 * $min2); } $arr = [-10,-10,5,2]; echo maxProduct($arr); // Output: 100
  • Write a PHP program to find the sum of all the numbers in a given array.
php
Copy code
function arraySum($arr) { return array_sum($arr); } $arr = [1, 2, 3, 4, 5]; echo arraySum($arr); // Output: 15
  • Write a PHP program to find the second largest number in an array of integers.
php
Copy code
function secondLargest($arr) { $max1 = $max2 = PHP_INT_MIN; foreach ($arr as $num) { if ($num > $max1) { $max2 = $max1; $max1 = $num; } else if ($num > $max2 && $num != $max1) { $max2 = $num; } } return $max2; } $arr = [1, 2, 3, 4, 5]; echo secondLargest($arr); // Output: 4
  • Write a PHP program to reverse a given string.
php
Copy code
function reverseString($str) { return strrev($str); } $str = "hello"; echo reverseString($str); // Output: "olleh"
  • Write a PHP program to check if a given string is a palindrome.
php
Copy code
function isPalindrome($str) { return $str == strrev($str); } $str1 = "racecar"; $str2 = "hello"; echo isPalindrome($str1); // Output: true echo isPalindrome($str2); // Output: false
  • Write a PHP program to count the number of vowels in a given string.
php
Copy code
function countVowels($str) { return preg_match_all('/[aeiou]/i', $str); } $str = "hello world"; echo countVowels($str); // Output: 3
  • Write a PHP program to find the factorial of a given number.
php
Copy code
function factorial($n) { if ($n < 2) { return 1; } return $n * factorial($n - 1); } $n = 5; echo factorial($n); // Output: 120
  • Write a PHP program to find the Fibonacci series up to a given number.
php
Copy code
function fibonacci($n) { $fib = [0, 1]; for ($i = 2; $i <= $n; $i++) { $fib[$i] = $fib[$i-1] + $fib[$i-2]; } return $fib; } $n = 10; echo implode(", ", fibonacci($n)); // Output: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
  • Write a PHP program to find the GCD (Greatest Common Divisor) of two numbers.
php
Copy code
function gcd($a, $b) { while ($b != 0) { $temp = $b; $b = $a % $b; $a = $temp; } return $a; } $a = 12; $b = 18; echo gcd($a, $b); // Output: 6
  • Write a PHP program to sort an array of integers in ascending order.
php
Copy code
function sortArray($arr) { sort($arr); return $arr; } $arr = [4, 2, 1, 3, 5]; echo implode(", ", sortArray($arr)); // Output: 1, 2, 3, 4, 5
  • Write a PHP program to remove all duplicate values from an array.
php
Copy code
function removeDuplicates($arr) { return array_unique($arr); } $arr = [1, 2, 1, 3, 2, 4, 5]; echo implode(", ", removeDuplicates($arr)); // Output: 1, 2, 3, 4, 5
  • Write a PHP program to find the intersection of two arrays.
php
Copy code
function arrayIntersection($arr1, $arr2) { return array_intersect($arr1, $arr2); } $arr1 = [1, 2, 3, 4, 5]; $arr2 = [3, 4, 5, 6, 7]; echo implode(", ", arrayIntersection($arr1, $arr2)); // Output: 3, 4, 5
  • Write a PHP program to find the union of two arrays.
php
Copy code
function arrayUnion($arr1, $arr2) { return array_unique(array_merge($arr1, $arr2)); } $arr1 = [1, 2, 3, 4, 5]; $arr2 = [3, 4, 5, 6, 7]; echo implode(", ", arrayUnion($arr1, $arr2)); // Output: 1, 2, 3, 4, 5, 6, 7
  • Write a PHP program to find the first non-repeating character in a string.
php
Copy code
function firstNonRepeating($str) { $count = array_count_values(str_split($str)); foreach ($count as $char => $freq) { if ($freq == 1) { return $char; } } return null; } $str = "hello world"; echo firstNonRepeating($str); // Output: "h"
  • Write a PHP program to find the maximum sum subarray in a given array of integers.
php
Copy code
function maxSubarraySum($arr) { $maxSum = $curSum = $arr[0]; for ($i = 1; $i < count($arr); $i++) { $curSum = max($arr[$i], $curSum + $arr[$i]); $maxSum = max($maxSum, $curSum); } return $maxSum; } $arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]; echo maxSubarraySum($arr); // Output: 6
  • Write a PHP program to find the largest sum of two elements in a given array of integers.
php
Copy code
function maxSumOfTwo($arr) { sort($arr); return $arr[count($arr)-1] + $arr[count($arr)-2]; } $arr = [4, 2, 1, 3, 5]; echo maxSumOfTwo($arr); // Output: 9
  • Write a PHP program to find the number of words in a given string.
php
Copy code
function countWords($str) { return str_word_count($str); } $str = "The quick brown fox jumps over the lazy dog."; echo countWords($str); // Output: 9
  • Write a PHP program to reverse a given string.
php
Copy code
function reverseString($str) { return strrev($str); } $str = "Hello, world!"; echo reverseString($str); // Output: "!dlrow ,olleH"
  • Write a PHP program to check if a given string is a palindrome.
php
Copy code
function isPalindrome($str) { $str = strtolower(str_replace(" ", "", $str)); return $str == strrev($str); } $str = "A man a plan a canal Panama"; echo isPalindrome($str) ? "Palindrome" : "Not a Palindrome"; // Output: Palindrome
  • Write a PHP program to count the frequency of each word in a given string.
php
Copy code
function countWordFrequency($str) { $count = array_count_values(str_word_count($str, 1)); arsort($count); return $count; } $str = "The quick brown fox jumps over the lazy dog."; print_r(countWordFrequency($str)); /* Output: Array ( [the] => 2 [brown] => 1 [dog] => 1 [fox] => 1 [jumps] => 1 [lazy] => 1 [over] => 1 [quick] => 1 ) */
  • Write a PHP program to find the factorial of a given number.
php
Copy code
function factorial($n) { if ($n < 2) { return 1; } return $n * factorial($n-1); } $n = 5; echo factorial($n); // Output: 120
  • Write a PHP program to find the sum of all the elements in a given array.
php
Copy code
function sumArray($arr) { return array_sum($arr); } $arr = [1, 2, 3, 4, 5]; echo sumArray($arr); // Output: 15
  • Write a PHP program to find the average of all the elements in a given array.
php
Copy code
function avgArray($arr) { return array_sum($arr) / count($arr); } $arr = [1, 2, 3, 4, 5]; echo avgArray($arr); // Output: 3
  • Write a PHP program to check if a given number is prime or not.
php
Copy code
function isPrime($n) { if ($n < 2) { return false; } for ($i = 2; $i <= sqrt($n); $i++) { if ($n % $i == 0) { return false; } } return true; } $n = 17; echo isPrime($n) ? "Prime" : "Not a Prime"; // Output: Prime

    Leave a Comment


  • captcha text