PHP Logical Interview Questions

Taaza Content Team

Ace your next PHP interview with these 30 most important logical PHP interview questions. Each question includes clear explanations and examples, perfect for freshers and experienced developers alike.

Prepare for your PHP interviews with these 30 logical questions and answers. Covers patterns, arrays, loops, recursion, and more—with code examples and detailed explanations.


1. Write a PHP program to check if a number is prime or not.

Explanation: A prime number is a number greater than 1 and divisible only by 1 and itself.

function isPrime($num) {
    if ($num <= 1) return false;
    for ($i = 2; $i < $num; $i++) {
        if ($num % $i == 0) return false;
    }
    return true;
}

echo isPrime(7) ? "Prime" : "Not Prime";

2. Check if a string is a palindrome or not.

Explanation: A palindrome reads the same backward as forward.

function isPalindrome($str) {
    $rev = '';
    for ($i = count(str_split($str)) - 1; $i >= 0; $i--) {
        $rev .= $str[$i];
    }
    return $str === $rev;
}

echo isPalindrome("madam") ? "Palindrome" : "Not Palindrome";

3. Write a program to check if a number is an Armstrong number.

Explanation: An Armstrong number is equal to the sum of its own digits each raised to the power of the number of digits.

function isArmstrong($num) {
    $sum = 0;
    $temp = $num;
    $digits = count(str_split($num));
    while ($temp > 0) {
        $digit = $temp % 10;
        $sum += pow($digit, $digits);
        $temp = (int)($temp / 10);
    }
    return $sum == $num;
}

echo isArmstrong(153) ? "Armstrong" : "Not Armstrong";

4. Write a program to calculate the factorial of a number using recursion.

function factorial($n) {
    if ($n == 0 || $n == 1) return 1;
    return $n * factorial($n - 1);
}

echo factorial(5); // Output: 120

5. Sort an array in ascending order without using PHP’s predefined sorting functions.

function sortAsc($arr) {
    $n = count($arr);
    for ($i = 0; $i < $n; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($arr[$i] > $arr[$j]) {
                $temp = $arr[$i];
                $arr[$i] = $arr[$j];
                $arr[$j] = $temp;
            }
        }
    }
    return $arr;
}

print_r(sortAsc([5, 3, 9, 1]));

6. Sort an array in descending order.

function sortDesc($arr) {
    $n = count($arr);
    for ($i = 0; $i < $n; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($arr[$i] < $arr[$j]) {
                $temp = $arr[$i];
                $arr[$i] = $arr[$j];
                $arr[$j] = $temp;
            }
        }
    }
    return $arr;
}

print_r(sortDesc([2, 8, 1, 4]));

7. Find the maximum number in an array.

function findMax($arr) {
    $max = $arr[0];
    for ($i = 1; $i < count($arr); $i++) {
        if ($arr[$i] > $max) {
            $max = $arr[$i];
        }
    }
    return $max;
}

echo findMax([3, 9, 2, 8]);

8. Find the minimum number in an array.

function findMin($arr) {
    $min = $arr[0];
    for ($i = 1; $i < count($arr); $i++) {
        if ($arr[$i] < $min) {
            $min = $arr[$i];
        }
    }
    return $min;
}

echo findMin([3, 9, 2, 8]);

9. Reverse a string without using predefined functions.

function reverseString($str) {
    $rev = '';
    for ($i = count(str_split($str)) - 1; $i >= 0; $i--) {
        $rev .= $str[$i];
    }
    return $rev;
}

echo reverseString("hello");

10. Count the number of vowels in a string.

function countVowels($str) {
    $vowels = ['a', 'e', 'i', 'o', 'u'];
    $count = 0;
    $str = strtolower($str);
    for ($i = 0; $i < count(str_split($str)); $i++) {
        if (in_array($str[$i], $vowels)) {
            $count++;
        }
    }
    return $count;
}

echo countVowels("Hello World");

11. Write a PHP program to reverse an array.

Explanation: Reverse the order of array elements manually without using built-in functions.

function reverseArray($arr) {
    $reversed = [];
    $n = count($arr);
    for ($i = $n - 1; $i >= 0; $i--) {
        $reversed[] = $arr[$i];
    }
    return $reversed;
}

print_r(reverseArray([1, 2, 3, 4, 5]));

12. Check if a number is even or odd.

function isEven($num) {
    return $num % 2 == 0;
}

echo isEven(10) ? "Even" : "Odd";

13. Count the number of digits in a number.

function countDigits($num) {
    $count = 0;
    while ($num != 0) {
        $num = (int)($num / 10);
        $count++;
    }
    return $count;
}

echo countDigits(12345); // Output: 5

14. Find the sum of digits in a number.

function sumOfDigits($num) {
    $sum = 0;
    while ($num != 0) {
        $sum += $num % 10;
        $num = (int)($num / 10);
    }
    return $sum;
}

echo sumOfDigits(123); // Output: 6

15. Swap two numbers without using a third variable.

function swapNumbers(&$a, &$b) {
    $a = $a + $b;
    $b = $a - $b;
    $a = $a - $b;
}

$x = 5;
$y = 10;
swapNumbers($x, $y);
echo "x = $x, y = $y";

16. Generate Fibonacci series up to N terms using recursion.

function fibonacci($n) {
    if ($n == 0) return 0;
    if ($n == 1) return 1;
    return fibonacci($n - 1) + fibonacci($n - 2);
}

for ($i = 0; $i < 7; $i++) {
    echo fibonacci($i) . " ";
}

17. Find duplicate elements in an array.

function findDuplicates($arr) {
    $n = count($arr);
    $duplicates = [];
    for ($i = 0; $i < $n; $i++) {
        for ($j = $i + 1; $j < $n; $j++) {
            if ($arr[$i] == $arr[$j]) {
                $exists = false;
                for ($k = 0; $k < count($duplicates); $k++) {
                    if ($duplicates[$k] == $arr[$i]) {
                        $exists = true;
                        break;
                    }
                }
                if (!$exists) {
                    $duplicates[] = $arr[$i];
                }
            }
        }
    }
    return $duplicates;
}

print_r(findDuplicates([2, 4, 6, 2, 4, 8]));

18. Find unique elements in an array.

function findUnique($arr) {
    $unique = [];
    $n = count($arr);
    for ($i = 0; $i < $n; $i++) {
        $isUnique = true;
        for ($j = 0; $j < $n; $j++) {
            if ($i != $j && $arr[$i] == $arr[$j]) {
                $isUnique = false;
                break;
            }
        }
        if ($isUnique) {
            $unique[] = $arr[$i];
        }
    }
    return $unique;
}

print_r(findUnique([1, 2, 3, 2, 4, 1]));

19. Remove duplicates from an array.

function removeDuplicates($arr) {
    $result = [];
    for ($i = 0; $i < count($arr); $i++) {
        $exists = false;
        for ($j = 0; $j < count($result); $j++) {
            if ($arr[$i] == $result[$j]) {
                $exists = true;
                break;
            }
        }
        if (!$exists) {
            $result[] = $arr[$i];
        }
    }
    return $result;
}

print_r(removeDuplicates([1, 2, 2, 3, 4, 4]));

20. Find the second highest number in an array.

function secondHighest($arr) {
    $first = $second = PHP_INT_MIN;
    for ($i = 0; $i < count($arr); $i++) {
        if ($arr[$i] > $first) {
            $second = $first;
            $first = $arr[$i];
        } elseif ($arr[$i] > $second && $arr[$i] != $first) {
            $second = $arr[$i];
        }
    }
    return $second;
}

echo secondHighest([10, 20, 30, 25, 30]); // Output: 25

21. Find the sum of all elements in an array.

function sumArray($arr) {
    $sum = 0;
    for ($i = 0; $i < count($arr); $i++) {
        $sum += $arr[$i];
    }
    return $sum;
}

echo sumArray([1, 2, 3, 4]); // Output: 10

22. Count the number of occurrences of a specific element in an array.

function countOccurrences($arr, $target) {
    $count = 0;
    for ($i = 0; $i < count($arr); $i++) {
        if ($arr[$i] == $target) {
            $count++;
        }
    }
    return $count;
}

echo countOccurrences([1, 2, 3, 2, 4, 2], 2); // Output: 3

23. Merge two arrays without using built-in functions.

function mergeArrays($arr1, $arr2) {
    $merged = [];
    for ($i = 0; $i < count($arr1); $i++) {
        $merged[] = $arr1[$i];
    }
    for ($i = 0; $i < count($arr2); $i++) {
        $merged[] = $arr2[$i];
    }
    return $merged;
}

print_r(mergeArrays([1, 2], [3, 4])); // Output: [1, 2, 3, 4]

24. Find the largest even number in an array.

function largestEven($arr) {
    $max = null;
    for ($i = 0; $i < count($arr); $i++) {
        if ($arr[$i] % 2 == 0) {
            if ($max === null || $arr[$i] > $max) {
                $max = $arr[$i];
            }
        }
    }
    return $max;
}

echo largestEven([3, 7, 12, 8, 9]); // Output: 12

25. Count how many even and odd numbers are in an array.

function countEvenOdd($arr) {
    $even = $odd = 0;
    for ($i = 0; $i < count($arr); $i++) {
        if ($arr[$i] % 2 == 0) {
            $even++;
        } else {
            $odd++;
        }
    }
    return "Even: $even, Odd: $odd";
}

echo countEvenOdd([1, 2, 3, 4, 5, 6]);

26. Replace all negative numbers in an array with 0.

function replaceNegatives($arr) {
    for ($i = 0; $i < count($arr); $i++) {
        if ($arr[$i] < 0) {
            $arr[$i] = 0;
        }
    }
    return $arr;
}

print_r(replaceNegatives([-1, 5, -3, 7, 0]));

27. Reverse a number without using string functions.

function reverseNumber($num) {
    $rev = 0;
    while ($num != 0) {
        $rev = $rev * 10 + ($num % 10);
        $num = (int)($num / 10);
    }
    return $rev;
}

echo reverseNumber(1234); // Output: 4321

28. Find the GCD (Greatest Common Divisor) of two numbers.

function gcd($a, $b) {
    while ($b != 0) {
        $temp = $b;
        $b = $a % $b;
        $a = $temp;
    }
    return $a;
}

echo gcd(20, 28); // Output: 4

29. Find the LCM (Least Common Multiple) of two numbers.

function gcd($a, $b) {
    while ($b != 0) {
        $temp = $b;
        $b = $a % $b;
        $a = $temp;
    }
    return $a;
}

function lcm($a, $b) {
    return ($a * $b) / gcd($a, $b);
}

echo lcm(4, 5); // Output: 20

30. Find the average of all numbers in an array.

function average($arr) {
    $sum = 0;
    $n = count($arr);
    for ($i = 0; $i < $n; $i++) {
        $sum += $arr[$i];
    }
    return $sum / $n;
}

echo average([10, 20, 30, 40]); // Output: 25


 

Stay Updated!

Would you like to receive notifications when we publish new blogs?