JOBSEEKER?

PHP Interview Questions

Practice for PHP interviews by solving TestDome questions. Our interview questions are used by more than 7,000 companies and 450,000 individual test takers.

Jobseekers: Certify Your Knowledge

Take a Certification Test

Companies: Use Our Tests for Screening

Buy a Pack Of Candidates

Need to practice your PHP programming skills for an upcoming job interview? Try solving these PHP interview questions that test knowledge of arrays, closures, sorting, inheritance, and other skills. We’ll provide feedback on your answers, and you can use a hint if you get stuck.

These PHP interview questions are examples of real tasks used by employers to screen job candidates such as PHP developers, back-end developers, full-stack developers, and others that require knowledge of the PHP programming language and its built-in functions.

1. File Owners

Associative arrays
   
Easy  

Implement a groupByOwners function that:

  • Accepts an associative array containing the file owner name for each file name.
  • Returns an associative array containing an array of file names for each owner name, in any order.

For example, for associative array ["Input.txt" => "Randy", "Code.py" => "Stan", "Output.txt" => "Randy"] the groupByOwners function should return ["Randy" => ["Input.txt", "Output.txt"], "Stan" => ["Code.py"]].

Solve Question

2. Merge Names

Arrays
   
Easy  

Implement the unique_names function. When passed two arrays of names, it will return an array containing the names that appear in either or both arrays. The returned array should have no duplicates.

For example, calling unique_names(['Ava', 'Emma', 'Olivia'], ['Olivia', 'Sophia', 'Emma']) should return ['Emma', 'Olivia', 'Ava', 'Sophia'] in any order.

Solve Question

3. Quadratic Equation

Arithmetic
   
Easy  

Implement the function findRoots to find the roots of the quadratic equation: ax2 + bx + c = 0. The function should return an array containing both roots in any order. If the equation has only one solution, the function should return that solution as both elements of the array. The equation will always have at least one solution.

The roots of the quadratic equation can be found with the following formula: A quadratic equation.

For example, findRoots(2, 10, 8) should return [-1, -4] or [-4, -1] as the roots of the equation 2x2 + 10x + 8 = 0 are -1 and -4.

Solve Question

4. User Input

Inheritance OOP
   
Easy  

The user interface contains two types of user input controls: TextInput, which accepts all texts and NumericInput, which accepts only digits.

Implement the class TextInput that contains:

  • Public function add($text) - adds the given text to the current value.
  • Public function getValue() - returns the current value (string).

Implement the class NumericInput that:

  • Inherits from TextInput.
  • Overrides the add method so that each non-numeric text is ignored.

For example, the following code should output '10':

$input = new NumericInput();
$input->add('1');
$input->add('a');
$input->add('0');
echo $input->getValue();
Solve Question

5. Thesaurus

JSON Serialization
   
Hard  

A thesaurus contains words and synonyms for each word. Below is an example of a data structure that defines a thesaurus:

array("buy" => array("purchase"), "big" => array("great", "large"))

Implement the function getSynonyms, which accepts a word as a string and returns all synonyms for that word in JSON format, as in the example below.

For example, the call $thesaurus->getSynonyms("big") should return:

'{"word":"big","synonyms":["great", "large"]}'

while a call with a word that doesn't have synonyms, like $thesaurus->getSynonyms("agelast") should return:

'{"word":"agelast","synonyms":[]}'
Solve Question

6. League Table

Arrays Sorting
   
Hard  

The LeagueTable class tracks the score of each player in a league. After each game, the player records their score with the recordResult function. 

The player's rank in the league is calculated using the following logic:

  1. The player with the highest score is ranked first (rank 1). The player with the lowest score is ranked last.
  2. If two players are tied on score, then the player who has played the fewest games is ranked higher.
  3. If two players are tied on score and number of games played, then the player who was first in the list of players is ranked higher.

Implement the playerRank function that returns the player at the given rank.

For example:

$table = new LeagueTable(array('Mike', 'Chris', 'Arnold'));
$table->recordResult('Mike', 2);
$table->recordResult('Mike', 3);
$table->recordResult('Arnold', 5);
$table->recordResult('Chris', 5);
echo $table->playerRank(1);

All players have the same score. However, Arnold and Chris have played fewer games than Mike, and as Chris is before Arnold in the list of players, he is ranked first. Therefore, the code above should display "Chris".

Solve Question

7. Pipeline

Closures Higher order function
   
Hard  

As part of a data processing pipeline, complete the implementation of the make_pipeline method:

  • The method should accept a variable number of functions, and it should return a new function that accepts one parameter $arg.
  • The returned function should call the first function in the make_pipeline with the parameter $arg, and call the second function with the result of the first function.
  • The returned function should continue calling each function in the make_pipeline in order, following the same pattern, and return the value from the last function.

For example, make_pipeline(function($x) { return $x * 3; }, function($x) { return $x + 1; }, function($x) { return $x / 2; }) then calling the returned function with 3 should return 5.

Solve Question

If you feel ready, take one of our timed public PHP Interview Questions tests:
PHP

PHP Online Test (Easy / Hard)

PHP and SQL Online Test (Easy / Hard)

PHP and WordPress Online Test (Easy / Hard)

PHP, MySQL, and SQL Online Test (Easy / Hard)

HTML/CSS

HTML/CSS, JavaScript, PHP, and SQL Online Test (Easy / Hard)

Not exactly what you are looking for? Go to our For Jobseekers section.
Dashboard Start Trial Sign In Home Tour Tests Questions Pricing For Jobseekers