50 Top PHP Interview Questions and Answers (For ALL)
50 Top PHP Interview Questions and Answers (For ALL)
50 Top PHP Interview Questions and Answers (For ALL)
List of Most Frequently Asked Core PHP Interview Questions with detailed Answers and
Code Examples for Freshers and Experienced Candidates:
Here in this article, we will discuss some of the most common and frequently asked
Core PHP interview questions with detailed answers and code samples.
The demand for PHP jobs is increasing day by day. People who are searching or preparing
for PHP jobs, have to face some common questions in the interview.
FEATURED VIDEOS
Home Resources FREE EBooks QA Testing Courses Automation
NOW
https://www.softwaretestinghelp.com/php-interview-questions/ 1/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
PLAYING
Types Of Testing Tutorials
So, if you are a fresher and if you wish to make your career as a PHP developer or even an
experienced professional looking to secure a higher position, then you must read this
article to increase your chance to get a PHP job easily and quickly.
Let’s Explore!!
Answer: PHP is one of the popular server-side scripting languages for developing a web
application.
The full form of PHP is Hypertext Preprocessor. It is used by embedding HTML for creating
dynamic content, communicating with a database server, handling sessions, etc.
Answer: There are several bene ts of using PHP. First of all, it is totally free to use. So
anyone can use PHP without any cost and host the site at a minimal cost.
What is System Testing
It supports multiple databases. The most commonly used database is MySQL which is also
free to use. Many PHP frameworks are used now for web development, such as CodeIgniter,
CakePHP, Laravel, etc.
These frameworks make the web development task much easier than before.
Q #3)
HomeIs PHPResources
a strongly typed
FREElanguage?
EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 2/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
This means PHP does not require to declare data types of the variable when you declare
any variable like the other standard programming languages C# or Java. When you store any
string value in a variable, then the data type is the string and if you store a numeric value
in that same variable then the data type is an Integer.
Sample code:
Answer: When the value of a variable is used as the name of the other variables then it is
called variable variables. $$ is used to declare variable variables in PHP.
Sample code:
$str = "PHP";
$$str = " Programming"; //declaring variable variables
echo "$str ${$str}"; //It will print "PHP programming"
echo "$PHP"; //It will print "Programming"
Answer: Both echo and print method print the output in the browser but there is a
di erence between these two methods.
echo does not return any value after printing the output and it works faster than the print
method. print method is slower than the echo because it returns the boolean value after
printing the output.
Sample code:
Q #6) How can you execute PHP script from the command line?
Answer: You have to use PHP command in the command line to execute a PHP script. If the
PHP le name is test.php then the following
Whatcommand is used to run the script from the
is System Testing
command line.
php test.php
Answer: You can declare three types of arrays in PHP. They are numeric, associative and
multidimensional arrays.
Home Resources FREE EBooks QA Testing Courses Automation
Sample code:
https://www.softwaretestinghelp.com/php-interview-questions/ 3/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
//Numeric Array
Types
$computer = array("Dell", Of Testing
"Lenavo", Tutorials
"HP");
//Associative Array
$color = array("Sithi"=>"Red", "Amit"=>"Blue", "Mahek"=>"Green");
//Multidimensional Array
$courses = array ( array("PHP",50), array("JQuery",15), array("AngularJS",20
Answer: explode() function is used to split a string into an array and implode() function is
used to make a string by combining the array elements.
Sample code:
Q #9) Which function can be used to exit from the script after displaying the error
message?
Answer: You can use exit() or die() function to exit from the current script after displaying
the error message.
Sample code:
if(!fopen('t.txt','r'))
exit(" Unable to open the file");
Sample code:
if(!mysqli_connect('localhost','user','password'))
die(" Unable to connect with the database");
Q #10) Which function is used in PHP to check the data type of any variable?
Sample code:
Q #11) How can you increase the maximum execution time of a script in PHP?
Answer: You need to change the value of the max_execution_time directive in the php.ini
le for increasing the maximum execution time.
For Example, if you want to set the max execution time for 120 seconds, then set the value
as follows,
Home Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 4/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
max_execution_time = 120
Types Of Testing Tutorials
Q #12) What is meant by ‘passing the variable by value and reference’ in PHP?
Answer: When the variable is passed as value then it is called pass variable by value.
Here, the main variable remains unchanged even when the passed variable changes.
Sample code:
function test($n) {
$n=$n+10;
}
$m=5;
test($m);
echo $m;
When the variable is passed as a reference then it is called pass variable by reference.
Here, both the main variable and the passed variable share the same memory location and
& is used for reference.
So, if one variable changes then the other will also change.
Sample code:
function test(&$n) {
$n=$n+10;
}
$m=5;
test($m);
echo $m;
Answer: The way by which PHP can assign a particular data type for any variable is called
typecasting. The required type of variable is mentioned in the parenthesis before the
variable.
Sample code:
Sample code:
Q #14) How can you make a connection with MySQL server using PHP?
Home Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 5/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Sample code:
$mysqli = mysqli_connect("localhost","username","password");
$mysqli = new mysqli("localhost","username","password");
Q #15) How can you retrieve data from the MySQL database using PHP?
Answer: Many functions are available in PHP to retrieve the data from the MySQL database.
Sample code:
Sample code:
//Numeric array
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result);
printf ("%s %s\n",$row[0],$row[1]);
Sample code:
// Associative array
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result); What is System Testing
printf ("%s %s\n",$row["name"],$row["email"]);
Sample code:
// Object
$result=mysqli_query($DBconnection,$query);
$row=mysqli_fetch_array($result);
printf ("%s %s\n",$row->name,$row->email);
Home Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 6/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Answer:
mysqli_pconnect() function is used for making a persistent connection with the database
that does not terminate when the script ends.
Sample code:
$DBconnection = mysqli_connect("localhost","username","password","dbname");
// Check for valid connection
if (mysqli_connect_errno())
{
echo "Unable to connect with MySQL: " . mysqli_connect_error();
}
mysqli_pconnect() function is depreciated in the new version of PHP, but you can create a
persistence connection using mysqli_connect with the pre x p.
Q #17) Which function is used in PHP to count the total number of rows returned by any
query?
Answer:
mysqli_num_rows() function is used to count the total number of rows returned by the
query.
Sample code:
$mysqli = mysqli_connect("hostname","username","password","DBname");
$result=mysqli_query($mysqli,"select * from employees");
$count=mysqli_num_rows($result);
Answer:
What is System Testing
session_start() function is used in PHP to create a session.
Sample code:
https://www.softwaretestinghelp.com/php-interview-questions/ 7/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Sample code:
Q #20) Which function you can use in PHP to open a le for reading or writing or for both?
Answer: You can use fopen() function to read or write or for doing both in PHP.
Sample code:
Answer: Both include() and require() function are used for including PHP script from one
le to another le. But there is a di erence between these functions.
If any error occurs at the time of including a le using include() function, then it continues
the execution of the script after showing an error message. require() function stops the
execution of a script by displaying an error message if an error occurs.
Sample code:
Answer:
unlink('filename');
Answer: strip_tags() function is used to retrieve the string from a text by omitting HTML,
XML and PHP tags. This function has one mandatory parameter and one optional
parameter. The optional parameter is used to accept particular tags.
Homecode:
Sample Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 8/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
//Remove all tags from the text
Typesis
echo strip_tags("<b>PHP</b> Of aTesting Tutorials
<em>scripting</em>
popular language");
//Remove all tags excluding <b> tag
echo strip_tags("<b>PHP</b> is a popular <em>scripting</em> language","<b>")
Q #24) How can you send an HTTP header to the client in PHP?
Answer: The header() function is used to send raw HTTP header to a client before any
output is sent.
Sample code:
header('Location: http://www.your_domain/');
Q #25) Which functions are used to count the total number of array elements in PHP?
Answer: count() and sizeof() functions can be used to count the total number of array
elements in PHP.
Sample code:
$names=array(“Asa”,”Prinka”,”Abhijeet”);
echo count($names);
$marks=array(95,70,87);
echo sizeof($marks);
Answer:
substr() function returns a part of the string based on the starting point and length. Length
parameter is optional for this function and if it is omitted then the remaining part of the
string from the starting point will be returned.
strstr() function searches the rst occurrence of a string inside another string. The third
parameter of this function is optional and it is used to retrieve the part of the string that
appears before the rst occurrence of the searching string.
Sample code:
Sample code:
https://www.softwaretestinghelp.com/php-interview-questions/ 9/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Open php.ini le and nd out the le_uploads directive and make it on.
file_uploads = On
(ii) Create an HTML form using enctype attribute and le element for uploading the le.
if (move_uploaded_file($_FILES["upd"]["tmp_name"], "Uploads/")) {
echo "The file ". basename( $_FILES["upd"]["name"]). " is uploaded.";
} else {
echo "There is an error in uploading.";
}
Answer: de ne() function is used to declare a constant variable in PHP. Constant variable
declares without the $ symbol.
Sample code:
define("PI",3.14);
Sample code:
Answer: The $_REQUEST variable is used to read the data from the submitted HTML form.
Sample code:
Here, the $_REQUEST variable is used to read the submitted form eld with the name
Home Resources
‘username’. If the form is FREE EBooks
submitted QAany
without Testing thenCourses
value, asAutomation
it will print “Name is
https://www.softwaretestinghelp.com/php-interview-questions/ 10/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Q #31) What is the di erence between for and Foreach loop in PHP?
Answer: For loop is mainly used for iterating a pre-de ned number of times and Foreach
loop is used for reading array elements or MySQL result set where the number of iteration
can be unknown.
Sample code:
Sample code:
Answer: By default, session data will last for 24 minutes or 1440 seconds in PHP. But if you
want, you can change the duration by modifying the value of gc_maxlifetime directive in
php.ini le. To set the session time for 30 minutes, open php.ini le and set the value of
What is System Testing
gc_maxlifetime directive as follows,
gc_maxlifetime = 1800
Answer: “= = =” is called strictly equivalent operator that is used to check the equivalency
of two values by comparing both data types and values.
Sample code:
Home Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 11/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
$n = 10;
if ($n === "10")
echo "n is equal to 10";
else
echo "n is not equal to 10"; //This will print
Sample code:
Anyone can download reusable PHP components by using this framework at a free of cost.
It contains di erent types of packages from di erent developers.
Website: PEAR
Fatal Errors– The execution of the script stops when this error occurs.
Sample code:
In the following script, f1() function is declared but f2() function is called which is not
declared. The execution of the script will stop when f2() function will call. So, “ Testing
Fatal Error” will not be printed.
What is System Testing
function f1()
{ echo "function 1";
}
f2();
echo “Testing Fatal Error”;
Parse Errors– This type of error occurs when the coder uses a wrong syntax in the
script.
Sample
Homecode:
Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 12/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Warning Errors- This type of error does not stop the execution of a script. It
continues the script even after displaying the error.
Sample code:
In the following script, if the test.txt le does not exist in the current location then a
warning message will display to show the error and print “Opening File” text by continuing
the execution.
$handler = fopen("test.txt","r");
echo "Opening File";
Notice Errors- This type of error shows a minor error of the script and continues the
execution after displaying the error.
Here, the variable, $a is de ned but $b is not de ned. So, a notice of the unde ned
variable will display for “echo $b” statement and print “Checking notice error” by
continuing the script.
Sample code:
$a = 100;
echo $b;
echo "Checking notice error";
Answer: PHP does not support multiple inheritances. To implement the features of multiple
inheritances, the interface is used in PHP.
Sample code:
Here, two interfaces, Isbn and Type are declared and implemented in a class, book details
to add the feature of multiple inheritances in PHP.
interface Isbn {
public function setISBN($isbn); What is System Testing
}
interface Type{
public function setType($type);
}
class bookDetails implements Isbn, Type {
private $isbn;
private $type;
public function setISBN($isbn)
{
$this -> isbn = $isbn;
}
public
Homefunction setType($type)
Resources FREE EBooks QA Testing Courses Automation
{
https://www.softwaretestinghelp.com/php-interview-questions/ 13/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
$this -> type = $type;
} Types Of Testing Tutorials
}
Answer: The session is a global variable that is used in the server to store the session data.
When a new session creates the cookie with the session id is stored on the visitor’s
computer. The session variable can store more data than the cookie variable.
Session data are stored in a $_SESSION array and Cookie data are stored in a $_COOKIE
array. Session values are removed automatically when the visitor closes the browser and
cookie values are not removed automatically.
Sample code:
$DBconnection=mysqli_connect("localhost","username","password","dbname");
$productName = mysqli_real_escape_string($con, $_POST['proname']);
$ProductType = mysqli_real_escape_string($con, $_POST['protype']);
Q #40) Which functions are used to remove whitespaces from the string?
Answer: There are three functions in PHP to remove the whitespaces from the string.
trim() – It removes whitespaces from the left and right side of the string.
ltrim() – It removes whitespaces from the left side of the string.
rtrim() – It removes whitespaces from the right side of the string.
Sample code:
setccookie ("cookie_name", "cookie_value", strtotime("+2 years");
Home Resources FREE EBooks QA Testing Courses Automation
https://www.softwaretestinghelp.com/php-interview-questions/ 14/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
Q #44) What is meant by public, private, protected, static and nal scopes?
Answer:
Public– Variables, classes, and methods which are declared public can be accessed
from anywhere.
Private– Variables, classes and methods which are declared private can be accessed
by the parent class only.
Protected– Variables, classes, and methods which are declared protected can be
accessed by the parent and child classes only.
Static– The variable which is declared static can keep the value after losing the
scope.
Final– This scope prevents the child class to declare the same item again.
Answer:
Answer:
Abstract classes are used for closely related objects and interfaces are used for
Home Resources FREE EBooks QA Testing Courses Automation
unrelated objects.
https://www.softwaretestinghelp.com/php-interview-questions/ 15/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
When it runs, it removes all session data which are not accessed for a long time. It runs on
/tmp directory which is the default session directory.
Answer: Using the GD library, various types of image work can be done in PHP. Image work
includes rotating images, cropping an image, creating image thumbnail, etc.
Answer: Appending the session ID in every local URL of the requested page for keeping the
session information is called URL rewriting.
The disadvantages of these methods are, it doesn’t allow persistence between the sessions
and, the user can easily copy and paste the URL and send it to another user.
It is a lightweight PHP extension that uses a consistence interface for accessing the
database. Using PDO, a developer can easily switch from one database server to the other.
But it does not support all the advanced features of the new MySQL server.
Conclusion
I hope, this article will increase your con dence level to face any PHP interview. Feel free to
contact us and suggest missing PHP Interview questions that you face in an interview.
About SoftwareTestingHelp
Helping our community since 2006! Most popular portal for Software professionals
with 100 million+ visits and 300,000+ followers! You will absolutely love our
tutorials on QA Testing, Development, Software Tools and Services Reviews and
more!
https://www.softwaretestinghelp.com/php-interview-questions/ 17/18
25/01/2021 50 Top PHP Interview Questions and Answers (For ALL)
https://www.softwaretestinghelp.com/php-interview-questions/ 18/18