Logical interview questions for PHP developer

By: Ajeet      Posted On: 06 Oct, 2017      Category: QandA

Welcome !. Below, there are some questions that are asked in interview for PHP developer.generally. These questions are basic and logical related to PHP and MySql. It may help you to prepare interview for the job of PHP developer. We hope you find these questions useful. Take the time to read the all  questions

 

1.  Find 2nd largest value in an array.
Ans:

<?php
$numbers = array(5, 3, 7, 9, 6, 8);
sort($numbers);
echo $numbers[count($numbers) - 2];   //Output: 8
?>

2. Find a word in a string and replace it.
Ans:

<?php
$string = "Array99 has good blog for PHP programming.";
$newString = str_replace("good", "nice", $string);
echo $newString;    //Output: Array99 has nice blog for PHP programming."
?>

3. Find a position of a word in a string.
Ans:

<?php
$string = "Array99 has good blog for PHP programming.";
$pos = strpos($string, "good");
echo $pos; //Output: 12
?>

4. How to display first 100 characters of a paragraph.
Ans:

<?php
$paragraph = "OOPs is a powerful software development concept and it always insists to minimize, reuse and better management of code. Inheritance is one of the powerful features of OOPs which provides re-usability of code. Traits which is a new feature introduce in PHP 5.4. The concept of traits added some extra feature in single inheritance.";
$substring = substr($paragraph, 0, 100);
echo $substring;
//Output: OOPs is a powerful software development concept and it always insists to minimize, reuse and better
?>

5. Find last 4 letter of the string.
Ans:

<?php
$string = "Array99 has good blog for PHP programming";
$substring = substr($string, -4);
echo $substring; //Output: ming
?>

6. Extract “good blog” from of below string.
Array99 has good blog for PHP programming.
Ans:

<?php
$string = "Array99 has good blog for PHP programming.";
$extractWord = "good blog";
$substring = substr($string, strpos($string, $extractWord), strlen($extractWord));
echo $substring; //Output: good blog
?>

7. Find the index of any element in an array.
Ans:

<?php
$numbers = array(5, 3, 7, 9, 6, 8);
echo array_search(7, $numbers);
//Output: 2
?>

7. What will be output of below code and why?
<?php
$string = "Array99 has good blog for PHP programming.";
$substr = "Array99";
if(strpos($string, $substr))
{
                echo "found";
}
else
{
                echo "not found";
}
?>
Ans:

Output will be “not found” because strpos($string, $substr) returns 0 as position and this is treated as false in IF condition. So the right code below.
<?php
$string = "Array99 has good blog for PHP programming.";
$substr = "Array99";
if(strpos($string, $substr) !== false)
{
                echo "found";
}
else
{
                echo "not found";
}
?>

8. How to remove first two elements from an array.
Ans:

<?php
$alphabats = array("a", "b", "c", "d", "e");
$output = array_slice($alphabats, 2);
print_r($output);
//Output Array ( [0] => c [1] => d [2] => e )
?>

9. How to remove two elements from middle in below array.
$colors = array("red", "green", "blue", "yellow");
Ans:

<?php
$colors = array("red", "green", "blue", "yellow");
array_splice($colors, 1, -1);
print_r($colors);
//Output: Array ( [0] => red [1] => yellow )
?>

10. How to remove last element and insert two new elements in below array.
$colors = array("red", "green", "blue", "yellow");
$lastColors = array("orange", "maroon");
Ans:

<?php
$colors = array("red", "green", "blue", "yellow");
array_splice($colors, -1, 1, $lastColors);
print_r($colors);
//Output: Array ( [0] => red [1] => green [2] => blue [3] => orange [4] => maroon )
?>

11. Check element in below array is an array or not.
Ans:

<?php
$colors = array("red", "green", array("orange", "maroon"), "blue", "yellow", array("purple", "brown"));
foreach($colors as $c)
{
    if(is_array($c))
    {
        echo "This element is an array.";
    }
    else
    {
        echo "This element is not an array.";
    }
}
?>

12. Convert any name in an array.
Ans:

<?php
$name = "sachin tendulkar";
$nameArray = array();
for($i = 0; $i<strlen($name); $i++)
{
    $nameArray[] = $name[$i]; 
}
print_r($nameArray);
//Output: Array ( [0] => s [1] => a [2] => c [3] => h [4] => i [5] => n [6] => [7] => t [8] => e [9] => n [10] => d [11] => u [12] => l [13] => k [14] => a [15] => r )
?>

13. Find the 2nd last salary. What will happen when two salaries are same in MySql?
Ans:
 Suppose you have an employee table
SELECT * FROM employee ORDER BY salary DESC LIMIT 1, 1
 But the above query will be fail same salary occurs multiple times in table. 

+----+---------+--------+
| id | name    | salary |
+----+---------+--------+
|  1 | Sachin  |  20000 |
|  2 | William |  25000 |
|  3 | Tom     |  20000 |
|  4 | Anky    |  25000 |
+----+---------+--------+

So below is the right query:
SELECT * FROM employee GROUP BY salary ORDER BY salary DESC LIMIT 1, 1

 

14. Write a query to find all students who have taken admission in before 1 month from “students” table.
Ans: 

SELECT * FROM `students` WHERE admission_date <= date_sub(curdate(), INTERVAL 1 month);

15. Write a query to find all coming payments from “payments” table.
Ans:

SELECT * FROM `payments` WHERE payment_date >= curdate();  

16. Write a query to find all customers who have purchasing amount in greater than 1000.
Ans:

+----+---------------+--------------+
| id | customer_name | purchase_amt |
+----+---------------+--------------+
|  1 | Sachin        |          500 |
|  2 | William       |          800 |
|  3 | William       |          400 |
|  4 | William       |          700 |
|  5 | Sachin        |          100 |
+----+---------------+--------------+


SELECT customer_name, SUM(purchase_amt) FROM `customers_orders` GROUP BY customer_name HAVING SUM(purchase_amt) > 1000;
17. Write a select query for below table so that F should display as FEMALE and M should display as MALE for gender field. 
Ans:

+----+---------------+--------------+--------+
| id | customer_name | purchase_amt | gender |
+----+---------------+--------------+--------+
|  1 | Soha          |          500 | F      |
|  2 | William       |          800 | M      |
|  3 | Honey         |          400 | F      |
|  4 | Tom           |          700 | M      |
|  5 | Sachin        |          100 | M      |
+----+---------------+--------------+--------+
SELECT customer_name, purchase_amt, (CASE WHEN gender = 'F' THEN 'FEMALE' WHEN gender = 'M' THEN 'MALE' END) AS gender FROM `customers_orders`;

18. Write a select query to implement SELF JOIN.
Ans:

+----+------------+---------+
| id | reports_to | name    |
+----+------------+---------+
|  1 |          0 | Sachin  |
|  2 |          0 | William |
|  3 |          2 | Tom     |
|  4 |          1 | Anky    |
|  5 |          1 | Mona    |
|  6 |          3 | Vincy   |
+----+------------+---------+

SELECT 
    e1.name AS 'Manager',
    e2.name AS 'Report To'
FROM
    employee e1
        INNER JOIN
    employee e2 ON e2.id = e1.reports_to
ORDER BY e1.id DESC 

Below, there are some patterns related to star, number and alphabets that are also asked in interview to test your logic. Some below: 

19. Write a program to display below pattern.


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

Ans: 
 <?php
for($i = 1;$i<=5;$i++){
    for($j=1;$j<=$i;$j++){
        echo "*";
    }
    echo "<br>";
}
?>

20. Write a program to display below pattern.


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

Ans:
<?php
for($i = 5;$i>=1;$i--){
    for($j=0;$j<$i;$j++){
        echo "*";
    }
    echo "<br>";
}
?>

 21. Write a program to display below pattern.


1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Ans:
<?php
for($i = 1;$i<=5;$i++){
    for($j=1;$j<=$i;$j++){
        echo $i;
    }
    echo "<br>";
}
?>

 

22. Write a program to display below pattern.


1
2 3
4 5 6
7 8 9 10

Ans:
<?php
$k = 1;
for($i = 1; $i<=4; $i++){
    for($j=1; $j<=$i; $j++){
        echo $k;
        $k++;
    }
    echo "<br>";
}
?>

5. Write a program to display below pattern.


      1
    1 2 1
  1 2 3 2 1
1 2 3 4 3 2 1

Ans:
<?php
for($i = 1; $i<=4; $i++){
    for($j=4; $j>=$i; $j--){
        echo "&nbsp;&nbsp;";
    }
    for($k=1; $k<=$i; $k++){
        echo $k;
    }
    for($m=($i-1); $m>=1; $m--){
        echo $m;
    }
    echo "<br>";
}
?>

 


6. Write a program to display below pattern.


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

Ans:
<?php
for($i = 1; $i<=4; $i++){
    for($j=4; $j>=($i + 1); $j--){
        echo "&nbsp;&nbsp;";
    }
    for($k=1; $k<=$i; $k++){
        echo "*";
    }
    for($m=2; $m<=$i; $m++){
        echo "*";
    }
    echo "<br>";
}
?>

You may like: