Skip to main content

List of latest and most asked PHP practical interviews questions & answers

Core PHP Practical Interview Questions

In this blog post I am sharing a list of some most asked PHP interview questions & answers. These are very useful and helpful for the freshers and experienced developer too. I have taken these questions from different sources and listed here at one place.

Ques. How to reverse a string without using any builtin function?

Ans:

 
<?php
  $str = 'My name is Diwakar Kumar';
  $len = 0;
  while(isset($str[$len]) != '')
      $len++;
  for($i = $len ; $i >= 0 ; $i--)
  {
      echo @$str[$i];
  }
Ques: Write a function to check if a given string is a palindrome or not.

Ans:

1st Method:

<?php	
function isPalindrome($str) {
    $str = strtolower(preg_replace('/[^a-zA-Z0-9]/', '', $str)); // Convert to lowercase and remove non-alphanumeric characters
    $reverse = strrev($str); // Reverse the string
    return $str === $reverse; // Compare original and reversed string
}
2nd Method:

<?php
function isPalindrome($str) {
    $left = 0;
    $right = strlen($str) - 1;
    while ($left < $right) {
        if($str[$left] != $str[$right]) {
            return false;
        }
        $left++;
        $right--;
    }
    return true;
}

Ques: Write a function to calculate the factorial of a given number.

Ans:


<?php
function factorial($num) {
    if ($num <= 1) {
        return 1;
    } else {
        return $num * factorial($num - 1);
    }
}

Ques: Write a function to reverse a string.

Ans:


<?php
function reverseString($str) {
    $reverse = '';
    $length = strlen($str);
    for ($i = $length - 1; $i >= 0; $i--) {
        $reverse .= $str[$i];
    }
    return $reverse;
}

Ques: Write a function to check if a given number is prime or not.

Ans:


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

Ques: Write a function to reverse the order of words in a given string.

Ans:


<?php
function reverseWords($str) {
    $words = [];
    $word = '';
    for ($i = 0; $i < strlen($str); $i++) {
        if ($str[$i] == ' ') {
            $words[] = $word;
            $word = '';
        } else {
            $word .= $str[$i];
        }
    }
    $words[] = $word;
    $reverse = '';
    for ($i = count($words) - 1; $i >= 0; $i--) {
        $reverse .= $words[$i] . ' ';
    }
    return trim($reverse);
}

Ques: Write a function to find the maximum element in an array.

Ans:


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

Ques: Write a function to generate the Fibonacci sequence up to a given number.

Ans:


<?php
function fibonacci($num) {
    $fib = [0, 1];
    for ($i = 2; $i <= $num; $i++) {
        $fib[$i] = $fib[$i - 1] + $fib[$i - 2];
    }
    return $fib;
}

Ques: Write a function to remove all duplicate elements in an array.

Ans:


<?php
function removeDuplicates($arr) {
    $result = [];
    foreach ($arr as $value) {
        if (!in_array($value, $result)) {
            $result[] = $value;
        }
    }
    return $result;
}

Some pattern Questions:

Ques: Write a PHP program to print the following pattern.

1
2 3
4 5 6
7 8 9 10

Ans:


<?php
$num = 1;
for ($i = 1; $i <= 4; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $num . ' ';
        $num++;
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

*
**
***
****
*****

Ans:


<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo '*';
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

    *
   **
  ***
 ****
*****

Ans:


<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= 5 - $i; $j++) {
        echo ' ';
    }
    for ($k = 1; $k <= $i; $k++) {
        echo '*';
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

    1
   232
  34543
 4567654
567898765

Ans:


<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= 5 - $i; $j++) {
        echo ' ';
    }
    $num = $i;
    for ($k = 1; $k <= $i; $k++) {
        echo $num;
        $num++;
    }
    $num--;
    for ($l = 1; $l <= $i - 1; $l++) {
        $num--;
        echo $num;
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

A
BB
CCC
DDDD
EEEEE

Ans:


<?php
for ($i = 65; $i <= 69; $i++) {
    for ($j = 65; $j <= $i; $j++) {
        echo chr($i);
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

    A
   B C
  D E F
 G H I J
K L M N O

Ans:


<?php
$alpha = 65;
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= 5 - $i; $j++) {
        echo ' ';
    }
    for ($k = 1; $k <= $i; $k++) {
        echo chr($alpha) . ' ';
        $alpha++;
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

1
22
333
4444
55555

Ans:


<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        echo $i;
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

A
BA
CBA
DCBA
EDCBA

Ans:


<?php
for ($i = 65; $i <= 69; $i++) {
    for ($j = $i; $j >= 65; $j--) {
        echo chr($j);
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

1
10
101
1010
10101

Ans:


<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i; $j++) {
        if ($j % 2 == 1) {
            echo '1';
        } else {
            echo '0';
        }
    }
    echo "\n";
}

Ques: Write a PHP program to print the following pattern.

*****
 ****
  ***
   **
    *

Ans:


<?php
for ($i = 1; $i <= 5; $i++) {
    for ($j = 1; $j <= $i - 1; $j++) {
        echo ' ';
    }
    for ($k = 1; $k <= 6 - $i; $k++) {
        echo '*';
    }
    echo "\n";
}

Some problem solving questions:

Ques: Write a PHP program to find the largest and smallest elements in an array.

Ans:


<?php
$numbers = array(12, 6, 23, 8, 5, 19);
$smallest = $numbers[0];
$largest = $numbers[0];

foreach ($numbers as $num) {
    if ($num < $smallest) {
        $smallest = $num;
    }
    if ($num > $largest) {
        $largest = $num;
    }
}

echo "The smallest number in the array is: " . $smallest . "\n";
echo "The largest number in the array is: " . $largest . "\n";

Ques: Write a PHP program to find the sum of all even numbers and odds numbers in an array.

Ans:


<?php
$numbers = array(12, 6, 23, 8, 5, 19);
$even_sum = 0;
$odd_sum = 0;

foreach ($numbers as $num) {
    if ($num % 2 == 0) {
        $even_sum += $num;
    } else {
        $odd_sum += $num;
    }
}

echo "The sum of even numbers in the array is: " . $even_sum . "\n";
echo "The sum of odd numbers in the array is: " . $odd_sum . "\n";

Ques: Create a function that takes a string consisting of lowercase letters, uppercase letters and numbers and returns the string sorted in the same way as the examples below.

examples:
	sorting("eA2a1E") ➞ "aAeE12"
	sorting("Re4r") ➞ "erR4"
	sorting("6jnM31Q") ➞ "jMnQ136"
	sorting("846ZIbo") ➞ "bIoZ468"

Ans:


<?php
function sorting($str) {
    $lowercase = $uppercase = $numbers = '';
    for ($i = 0; $i < strlen($str); $i++) {
        $char = $str[$i];
        if (ctype_lower($char)) {
            $lowercase .= $char;
        } else if (ctype_upper($char)) {
            $uppercase .= $char;
        } else if (ctype_digit($char)) {
            $numbers .= $char;
        }
    }
    $sortedStr = $lowercase . $uppercase . $numbers;
    return $sortedStr;
}
Note: I will try my best to keep this post updated.

Comments

Popular Posts

Different types of variables in Python with examples.

In Python, instance variables, static variables, and local variables are all different types of variables that serve different purposes within a program. Instance Variables: Instance variables are unique to each instance of a class. They are defined within a class's methods or the __init__ method and are accessed using the self keyword. Each instance of a class maintains its own copy of instance variables. These variables hold data specific to each object and can have different values for each instance of the class. Here's an example that demonstrates instance variables: class Person: def __init__(self, name, age): self.name = name # instance variable self.age = age # instance variable person1 = Person("Alice", 25) person2 = Person("Bob", 30) print(person1.name) # Output: Alice print(person2.name) # Output: Bob print(person1.age) # Output: 25 print(person2.age) # Output: 30  In the example above, name and a...

How to delete multiple Git branches using pattern Matching

To delete multiple branches using Git with pattern matching, you can use a combination of Git commands and shell scripting. Here's an example of how you can achieve this:  1. Open a terminal or command prompt.  2. Navigate to your Git repository directory.  3. Use the following command to list all branches matching a specific pattern: git branch | grep "<pattern>" Replace <pattern> with the pattern you want to match. For example, if you want to delete all branches starting with "feature/" , you can use feature/* as the pattern.  This command lists all branches that match the specified pattern.  4. Review the branch names listed and make sure they are the ones you want to delete.  5. Once you're sure, you can use the following command to delete the branches: git branch | grep "<pattern>" | xargs git branch -D This command combines the previous `git branch` and `grep` commands to find the branches matching ...