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


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

2. Palindrome
PHP Strings Public

A palindrome is a word that reads the same backward or forward.

Write a function that checks if a given word is a palindrome. Character case should be ignored.

For example, isPalindrome("Deleveled") should return true as character case should be ignored, resulting in "deleveled", which is a palindrome since it reads the same backward and forward.

Easy  
10min
PHP 7.1.9
 


  •   Example case: Wrong answer
  •   Lowercase words: Wrong answer
  •   Various words: Wrong answer

3. User Input
PHP OOP Public New

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


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

4. 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.1.9
 


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

5. League Table
PHP Arrays Sorting Public New

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


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

6. Path
PHP Data structures Strings Public

Write a function that provides change directory (cd) function for an abstract file system.

Notes:

  • Root path is '/'.
  • Path separator is '/'.
  • Parent directory is addressable as '..'.
  • Directory names consist only of English alphabet letters (A-Z and a-z).
  • The function should support both relative and absolute paths.
  • The function will not be passed any invalid paths.
  • Do not use built-in path-related functions.

For example:

$path = new Path('/a/b/c/d');
$path->cd('../x')
echo $path->currentPath;

should display '/a/b/c/x'.

Hard  
30min
PHP 7.1.9
 


  •   Example case: Wrong answer
  •   Selecting child directories: Wrong answer
  •   Selecting parent directories: Wrong answer
  •   Selecting complex paths: 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)
Not exactly what you are looking for? Go to our For Jobseekers section.