PHP Interview Questions

Want to become an expert in cracking PHP interview questions?

Start with practicing the questions below. Whether a question involves multiple choice or live coding, we will give you hints as you go and tell you if your answers are correct or incorrect.

After that, take our timed public PHP Interview Questions Test.

To use our service for testing candidates, buy a pack of candidates.


1. File Owners

PHP Language Public

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"]].

Easy  
10min
PHP 7.3.10  
 


  •   Example case: Wrong answer
  •   Each owner has a single file: Wrong answer
  •   Various files: Wrong answer


2. Merge Names

PHP Arrays Public

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.

Easy  
10min
PHP 7.3.10  
 


  •   Example case: Wrong answer
  •   Each array has distinct names: Wrong answer
  •   Each array has duplicate names: Wrong answer
  •   Arrays have some names in common: Wrong answer


3. Quadratic Equation

PHP Arithmetic Public New

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.

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.

Easy 
10min
PHP 7.3.10  
 


  •   Example case: Wrong answer
  •   Equal roots: Wrong answer
  •   Distinct roots: Wrong answer


4. User Input

PHP OOP Public

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();
Easy  
15min
PHP 7.3.10  
 


  •   Example case: Exception
  •   Adding various inputs to TextInput: Exception
  •   Adding numeric inputs to NumericInput: Wrong answer
  •   Adding various inputs to NumericInput: Wrong answer


5. Pipeline

PHP Closures Public

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.

Hard  
10min
PHP 7.3.10  
 


  •   Example case: Wrong answer
  •   Various functions: Wrong answer
  •   Various data types: Wrong answer


6. Thesaurus

PHP Serialization Public

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":[]}'
Hard  
10min
PHP 7.3.10  
 


  •   Sample case: Wrong answer
  •   Word doesn't have synonyms: Wrong answer
  •   Word has synonyms: Wrong answer
  •   Word has unusual synonyms: Wrong answer


7. League Table

PHP Arrays Sorting Public

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".

Hard  
20min
PHP 7.3.10  
 


  •   Example case: Wrong answer
  •   Players have different scores: Wrong answer
  •   Players tied by score: Wrong answer
  •   Players tied by games played: Wrong answer


If you feel ready, take one of our timed public PHP Interview Questions tests:
  • HTML/CSS, JavaScript, PHP, and SQL Online Test (Easy / Hard)
  • 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)
Not exactly what you are looking for? Go to our For Jobseekers section.