Top 10 PHP Interview Questions & Answers
As a candidate for PHP (Web development) you would definitely like to know the best possible questions you might face in your next PHP Interview. Thought the questions change from one interviewer to another, we would like to put together a set of questions considering that it is expected from you to know the correct answers to these questions.
List of PHP Freelancers
Question 1
What is PHP? Is it licensed?
Answer: PHP stands for hypertext pre-processor, which is an open source scripting language. Today it is most famous scripting language for developing web sites. It is used as server side scripting language as well as general purpose programming language. PHP code is executed on the server.
*Tip: It is good to mention about use of language on client side or server side
Question 2
Which are the famous PHP frameworks?
Answer: There are many PHP frameworks. Some of the famous ones are:
Laravel, Phalcon, Symfony 2, Yii Framework, Codeigniter, CakePHP, Zend, Zoop, etc.
*Tip: You can add which ones you are using at present to the answer of this question
Question 3
How will you write a small function in PHP to get remote IP address?
Answer:
function getRemoteIP() {
$ip = $_SERVER[‘REMOTE_ADDR’];
}
*Tip: Be hands-on for quickly writing some snippets in the language you are being interviewed
Question 4
Which data types are supported by PHP?
Answer: PHP supports – String, Boolean, Integer, Float, Object, Array, NULL and Resource
*Tip: PHP data types and may be some examples are important for data handling knowledge
Question 5
What will be the use and output of the below code snippet?
<?php
function file_size($url){
$size = filesize($url);
if($size >= 1073741824){
$fileSize = round($size/1024/1024/1024,1) . ‘GB’;
}elseif($size >= 1048576){
$fileSize = round($size/1024/1024,1) . ‘MB’;
}elseif($size >= 1024){
$fileSize = round($size/1024,1) . ‘KB’;
}else{
$fileSize = $size . ‘ bytes’;
}
return $fileSize;
}
echo file_size(‘/myfile/image.jpg’);
?>
Answer: The above code will display the file size along with GB, MB, KB or bytes
*Tip: You can expect a reverse question also like the above one, where a code snippet can be provided to you and you are expected to tell the outcome
Question 6
What is an Array and what are the types of arrays in PHP?
Answer: An array is a data structure that stores one or more similar type of values in a single value. In PHP, there are three types of arrays:
Indexed arrays – Arrays with a numeric index
Associative arrays – Arrays with named keys
Multidimensional arrays – Arrays containing one or more arrays
*Tip: Don’t miss the Array part in any programming language which will always have at least one of the questions
Question 7
IF a single line to be read from a given text file, how will you write a PHP function to do this task?
Answer:
<?php
$myfile = fopen(“webexample.txt”, “r”) or die(“Unable to open file!”);
echo fgets($myfile);
fclose($myfile);
?>
*Tip: Questions about writing simple functions are common in technical interviews of experienced people.
Question 8
What is PDO? Can you give an example of how to connect to the DB using PDO?
Answer: PDO is PHP Data Object which can be used to access data from any DB from PHP
try {
$conn = new PDO(“mysql:host=$servername;dbname=myDB”, $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo “Connected successfully”;
}
*Tip: Connecting to DB is most important part of your programming as you will not be able to do half of the things without connecting to DB. Also it is not expected to write entire program while giving any example but you can write only the required code
Question 9
What is a CMS and which are some of the famous PHP based CMS?
Answer: CMS or a Content Management System is an Application which allows to manage, organise, create, edit and publish a website easily.
Most famous CMS today are mainly based on PHP and some of them are WordPress, Joomla, Drupal, AdaptCMS, CMS made simple, Exponent CMS, PHP fusion, PHP Nuke, etc.
*Tip: CMS is one of the key part in any web development now and knowledge about a few CMS systems is going to make your life easy
Question 10
What do these functions do in PHP?
max(), min(), floor(), data_diff(), time()
Answer:
max() returns the highest value in an array
min() returns lowest value in an array
floor() rounds a number down to the nearest integer
date_diff()returns the difference between two dates
time() returns the current time
*Tip: Readymade functions are most handy tools and knowing some of the common used functions by heart is always good to speed up your programming.
We hope you have got the gist of important questions which can help you prepare for your interview
All the Best!