Top PHP Interview Questions and Answer for Intermediate to Advanced Levels
This tutorial has a list of top PHP interview questions and answers designed for intermediate to advanced-level developers. Either you are preparing for a job interview or looking to brush up on your PHP skills, these questions will help you to understand key concepts and demonstrate your expertise.
Table of content
- Remove duplicate elements from a given array [1,2,1,3,2,6,6,5]
- Find the second smallest number from a given array
- Find the factorial of n
- Find the factorial of n using the recursive function
- Write a number in the series of 0 2 4 6 8 10
- How to check a happy number in PHP?
Remove duplicate elements from a given array [1,2,1,3,2,6,6,5]
// method 1 using custom method
function remove_duplciate($input_arr)
{
$length = count($input_arr);
$unique_array = [];
for ($i = 0; $i < $length; $i++) {
$is_unique = false;
for ($j = 0; $j < count($unique_array); $j++) {
if ($input_arr[$i] == $unique_array[$j]) {
$is_unique = true;
break;
}
}
if (!$is_unique) {
$unique_array[] = $input_arr[$i];
}
}
return $unique_array;
}
$inpt_array = [1, 5, 5, 2, 6];
print_r(remove_duplciate($inpt_array));
//method 2 using array_flip() method
function remove_duplciate2($input_arr)
{
$unique_array = array_flip(array_flip($input_arr));
return array_values($unique_array);
}
$inpt_array = [1, 5, 5, 2, 6];
print_r(remove_duplciate2($inpt_array));
//method 3 using array_search() method
function remove_duplciate3($input_arr)
{
$unique_array = [];
for ($i = 0; $i < count($input_arr); $i++) {
$value = $input_arr[$i];
if (array_search($value, $unique_array) === FALSE) {
$unique_array[] = $value;
}
}
return $unique_array;
}
$inpt_array = [1, 5, 5, 2, 6, 9, 6];
print_r(remove_duplciate3($inpt_array));
How It Works
For factorial(5):
- Result:
Explanation:
Loop through each element in the given array.
If the current element is smaller than the smallest then:
- Update $sec_smallest to be the current value of smallest.
- Update $smallest to be the current element.
Otherwise, if the current element is smaller than sec_smallest and not equal to smallest:
- Update sec_smallest to be the current element
How to get the Fibonacci series in PHP
<?php
function get_fabonacies_series(int $n)
{
$fib = [0, 1];
if ($n <= 0) {
return [];
} elseif ($n == 1) {
return [0];
} elseif ($n == 2) {
return $fib;
}
for ($i = 2; $i < $n; $i++) {
$next = $fib[$i - 1] + $fib[$i - 2];
$fib[] = $next;
}
return $fib;
}
$result = get_fabonacies_series(12);
echo implode(', ', $result);
Explanation
Base Cases:
- If $n is less than or equal to 0, return an empty array.
- If $n is 1, return an array with a single element [0].
- If $n is 2, return the first two elements of the Fibonacci series [0, 1].
Loop for Fibonacci Calculation:
- Start from the 3rd term (index 2) and continue up to $n terms.
- Each term is the sum of the two preceding terms.
- Append each new term to the Fibonacci array.
Write a number in the series of 0 2 4 6 8 10
<?php
//Write a number in the series 0 2 4 6 8 10
for ($i = 0; $i <= 10; $i += 2) {
echo $i . "<br>";
}
Find the factorial of n
function factorial(int $n)
{
if ($n == 0) {
return 1;
}
$f = 1;
for ($i = 1; $i <= $n; $i++) {
$f *= $i;
}
return $f;
}
$result = factorial(3);
echo $result;
Find the second smallest number from a given array
<?php
function second_smallest($input){
$length = count($input);
$smallest = PHP_INT_MAX;
$sec_smallest =PHP_INT_MAX;
for ($i=0; $i<$length; $i++){
if ($input[$i] <$smallest) {
$sec_smallest = $smallest;
$smallest = $input[$i];
}elseif($input[$i] < $sec_smallest && $input[$i] != $smallest){
$sec_smallest = $input[$i];
}
}
return $sec_smallest;
}
$arr = [25,4, 50, 20,22,23];
echo second_smallest($arr);
How to check a happy number in PHP
A happy number is a number that finally reaches 1 when it is replaced by the sum of the squares of the individual digits. If it rotates endlessly in a cycle that does not contain 1 then it is not a happy number.
<?php
function is_happynumber(int $number): bool
{
$seen = [];
while ($number != 1 && !isset($seen[$number])) {
$seen[$number] = true;
$sum = 0;
while ($number > 1) {
$digit = $number % 10;
$sum += $digit * $digit;
$number = (int) ($number / 10);
}
$number = $sum;
}
return $number == 1;
}
$number = 11;
if (is_happynumber($number)) {
echo "yes";
} else {
echo "no";
}
Find the factorial of n using the recursive function
function factorial(int $n)
{
if ($n == 0) {
return 1;
}
return $n * factorial($n - 1);
}
$result = factorial(5);
echo $result;
If you want to get a happy number list between two number ranges we can find it like below using the above function:
function get_happy_number_list($min_num, $max_num)
{
$happy_num_list = [];
for ($i = $min_num; $i <= $max_num; $i++) {
if (is_happynumber($i)) {
array_push($happy_num_list, $i);
}
}
return $happy_num_list;
}
echo "<h3>Happy Number list b/w 1 to 40</h3>";
echo "<pre>";
print_r(get_happy_number_list(1, 40));