If you have a background in web development, a knowledge of how to solve and answer the most rigorously set PHP interview questions is absolutely essential if you want to progress to the top tier of your field.
Thankfully, because the sector of web development is so collaborative, there is a huge volume of PHP interview questions and answers online, many of them posted by PHP interview experts who freely pass on information on how to best answer these, and what the interviews are looking for in asking them.
Below, we have a mixture of some of the core, common and most difficult PHP interview questions that you could be asked in an interview.
And don’t forget, if you enjoy having your brain teased by what we have listed here, there are a large number of great guidebooks on how best to prepare yourself for the toughest PHP interview questions, including PHP Interview Questions, Answers and Explanations, Conducting the Webmaster Job Interview or PHP MySQL Web Programming Interview Questions, Answers, and Explanations.
9 Core, Difficult PHP Interview Questions
To whet your appetite and help you prepare for the PHP interview questions you will face, here are 9 of the core, most common, and difficult PHP interview questions posted online by experts, with answers, to help you prepare. Good luck, PHP AGENTs!
1. How do you check if a variable is NULL or empty string?
The answer, according to the WebTechnologyExpertsNotes website is:
var_dump($variableName);
It is also possible to use === to compare data type.
PHP Interview Question & Answer Source: WebTechnologyExpertsNotes
2. What is T_PAAMAYIM_NEKUDOTAYIM?
A commenter on a StackOverflow thread submitted the above question. He admits that he is unable to remember its source, but thought it a useful and funny addition to the mix of PHP questions online. The answer is that it is the scope resolution operator (double colon). Any experienced PHP-er should immediately know what it means. The term is actually the Hebrew for “twice colon”.
3. What’s the difference between the include() and require() functions?
This question was provided to the website Codementor.io by former Googler Laszlo Levente Mári, who writes that he asks questions that single out the developers who are constantly learning new things and are generally creative, not just in coding problem-solving.
Here, Laszlo advises that both functions include a specific file but on require() the process exits with a fatal error if the file can’t be included, while include() may still pass and jump to the next step in the execution.
4. What is the cause of this warning and how can it be prevented?
Warning: Cannot modify header information - headers already sent
The answer, provided by a StackOverflow user is that the body data was sent, causing the headers to be sent also. In terms of prevention, the user advises execution of a header-specific code first before any body data is output. Users should also be certain that they have not accidentally sent out whitespace or any other characters.
5. Code problem-solving questions
Interviewees should be prepared to come to the interview wearing their problem-solving hats. Here is a question and answer provided on the Toptal.com website. Consider the following code:
$str1 = 'yabadabadoo'; $str2 = 'yaba'; if (strpos($str1,$str2)) { echo "\"" . $str1 . "\" contains \"" . $str2 . "\""; } else { echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\""; }
The output will be:
"yabadabadoo" does not contain "yaba"
Why? How can this code be fixed to work correctly?
The problem is that strpos() returns the starting position index of $str1 in $str2 (if found), otherwise it returns false. So in this example, strpos() returns 0 (which is then coerced to false when referenced in the if statement). That’s why the code doesn’t work properly.
The correct solution would be to explicitly compare the value returned by strpos() to false as follows:
$str1 = 'yabadabadoo'; $str2 = 'yaba'; if (strpos($str1,$str2) !== false) { echo "\"" . $str1 . "\" contains \"" . $str2 . "\""; } else { echo "\"" . $str1 . "\" does not contain \"" . $str2 . "\""; }
Note: this code uses the !== operator, not just the != operator. Using the latter leaves you with the problem that 0 is coerced to false when referenced in a boolean expression, so 0 != false will evaluate to false.
6. How can I measure the speed of code written in PHP?
Here is an answer to one of the most common PHP interview questions, provided on the WebTechnologyExpertsNotes website.
$startTime= microtime(true); /** Write here you code to check **/ /** Write here you code to check **/ $endTime= microtime(true); echo 'Time Taken to executre the code:'.$endTime-$startTime;
7. What are the main error types in PHP and how do they differ?
Agli Pançi is another experienced PHP interviewer, who tells Codementor.io that she aims to understand “how updated candidates are with the new language features, as well as their level of understanding of basic operations”.
The answer to this question, writes Agli, is that there are three main types of error in PHP:
- Notices: simple, non-critical errors that occur during the script execution. An example of a Notice would be accessing an undefined variable.
- Warnings: more important errors than Notices, but the scripts continue the execution. An example would be include() a file that does not exist.
- Fatal: this error type causes a termination of the script execution when it occurs. An example of a Fatal error would be accessing a property of a non-existent object or require()a non-existent file.
Agli says that understanding error types is crucial, as they help programmers understand what is going on during development, and provide clues as to what you need to look for during debugging.
8. What is the difference between an interface and an abstract class?
According to StackOverflow, an interface defines a contract between an implementing class and an object that calls the interface. An abstract class predefines certain behaviour for classes that will extend it. To a certain degree, this can also be considered a contract, since it guarantees the existence of certain methods.
9. What are the 3 scope levels available in PHP and how would you define them?
Merlyn Coslett tells CodeMentor.io that the three levels and their definitions are as follows:
- Private: visible only in its own class
- Public: visible to any other code accessing the class
- Protected: visible only to classes parent(s) and classes that extend the current class
Knowledge of this shows an understanding by the developer that building applications is about more than writing code. Coslett writes that there must also be understanding about privileges and accessibility. An understanding of scope is needed to protect the integrity of the data in your application along with provide a clear path through the code, he adds.