30 Top PHP Interview Questions And Answers (For ALL)

Core PHP Interview Questions and Answers for Freshers and 1 to 5 Years Experienced Candidates:
Do you want to learn or test your PHP skills for an interview?
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. 
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.
php interview questions and answers

50 Most Common PHP Interview Questions

Given below is the list of most popular PHP interview questions which are commonly asked in the interviews.
Let's Explore!!
Q #1) What is PHP?
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 session etc.
Q #2) Why do we use PHP?
Answer:
There are several benefits 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.
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) Is PHP a strongly typed language?
Answer:
No. PHP is a weakly typed or loosely typed language.
Which 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:
1$var "Hello"//String
2$var = 10; //Integer
Q #4) What is meant by variable variables in PHP?
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:
1$str "PHP";
2$$str " Programming"//declaring variable variables
3echo "$str ${$str}"//It will print "PHP programming"
4echo "$PHP"//It will print "Programming"
Q #5) What are the differences between echo and print?
Answer:
Both echo and print method print the output in the browser but there is a difference 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 boolean value after printing the output.
Sample code:
1echo "PHP Developer";
2$n print "Java Developer";
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 file name is test.php then the following command is used to run the script from the command line.
php test.php
Q #7) How can you declare the array in PHP?
Answer:
You can declare three types of arrays in PHP. They are numeric, associative and multidimensionalarrays.
Sample code:
1//Numeric Array
2$computer array("Dell""Lenavo""HP");
3//Associative Array
4$color array("Sithi"=>"Red""Amit"=>"Blue""Mahek"=>"Green");
5//Multidimensional Array
6$courses array array("PHP",50), array("JQuery",15), array("AngularJS",20) );
Q #8) What are the uses of explode() and implode() functions?
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:
1$text "I like programming";
2print_r (explode(" ",$text));
3$strarr array('Pen','Pencil','Eraser');
4echo implode(" ",$strarr);
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:
1if(!fopen('t.txt','r'))
2exit(" Unable to open the file");
Sample code:
1if(!mysqli_connect('localhost','user','password'))
2die(" Unable to connect with the database");
Q #10) Which function is used in PHP to check the data type of any variable?
Answer:
gettype() function is used to check the data type of any variable.
Sample code:
1echo gettype(true).''//boolean
2echo gettype(10).''//integer
3echo gettype('Web Programming').''//string
4echo gettype(null).''//NULL
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 file 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,
1max_execution_time = 120
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:
1function test($n) {
2$n=$n+10;
3}
4
5$m=5;
6test($m);
7echo $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:
1function test(&$n) {
2    $n=$n+10;
3}
4$m=5;
5test($m);
6echo $m;
Q #13) Explain type casting and type juggling.
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:
1$str "10"// $str is now string
2$bool = (boolean) $str// $bool is now boolean
PHP does not support data type for variable declaration. The type of the variable is changed automatically based on the assigned value and it is called type juggling.
Sample code:
1$val = 5; // $val is now number
2$val "500" //$val is now string
Q #14) How can you make a connection with MySQL server using PHP?
Answer:
You have to provide MySQL hostname, username and password to make a connection with the MySQL server in mysqli_connect() method or declaring database object of the mysqli class.
Sample code:
1$mysqli = mysqli_connect("localhost","username","password");
2$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.
Few functions are mentioned below:
mysqli_fetch_array() – It is used to fetch the records as a numeric array or an associative array.
Sample code:
1// Associative or Numeric array
2$result=mysqli_query($DBconnection,$query);
3$row=mysqli_fetch_array($result,MYSQLI_ASSOC);
4echo "Name is $row[0]
5";
6echo "Email is $row['email']
7";
mysqli_fetch_row() – It is used to fetch the records in a numeric array.
Sample code:
1//Numeric array
2$result=mysqli_query($DBconnection,$query);
3$row=mysqli_fetch_array($result);
4printf ("%s %s\n",$row[0],$row[1]);
mysqli_fetch_assoc() – It is used to fetch the records in an associative array.
Sample code:
1// Associative array
2$result=mysqli_query($DBconnection,$query);
3$row=mysqli_fetch_array($result);
4printf ("%s %s\n",$row["name"],$row["email"]);
mysqli_fetch_object() – It is used to fetch the records as an object.
Sample code:
1// Object
2$result=mysqli_query($DBconnection,$query);
3$row=mysqli_fetch_array($result);
4printf ("%s %s\n",$row->name,$row->email);
Q #16) What are the differences between mysqli_connect and mysqli_pconnect?
Answer:
mysqli_pconnect() function is used for making a persistence connection with the database that does not terminate when the script ends.
mysqli_connect() function searches any existing persistence connection first and if no persistence connection exists, then it will create a new database connection and terminate the connection at the end of the script.
Sample code:
1$DBconnection = mysqli_connect("localhost","username","password","dbname");
2// Check for valid connection
3if (mysqli_connect_errno())
4{
5echo "Unable to connect with MySQL: " . mysqli_connect_error();
6}
mysqli_pconnect() function is depreciated in the new version of PHP, but you can create persistence connection using mysqli_connect with the prefix 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:
1$mysqli = mysqli_connect("hostname","username","password","DBname");
2$result=mysqli_query($mysqli,"select * from employees");
3$count=mysqli_num_rows($result);
Q #18) How can you create a session in PHP?
Answer:
session_start() function is used in PHP to create a session.
Sample code:
1session_start(); //Start session
2$_SESSION['USERNAME']='Fahmida'//Set a session value
3unset($_SESSION['USERNAME']; //delete session value
Q #19) What is the use of imagetypes() method?
Answer:
image types() function returns the list of supported images of the installed PHP version. You can use this function to check if a particular image extension is supported by PHP or not.
Sample code:
1//Check BMP extension is supported by PHP or not
2if (imagetypes() &IMG_BMP) {
3    echo "BMP extension Support is enabled";
4}
Q #20) Which function you can use in PHP to open a file 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:
1$file1 fopen("myfile1.txt","r"); //Open for reading
2$file2 fopen("myfile2.txt","w"); //Open for writing
3$file3 fopen("myfile3.txt","r+"); //Open for reading and writing
Q #21) What is the difference between include() and require()?
Answer:
Both include() and require() function are used for including PHP script from one file to another file. But there is a difference between these functions.
If any error occurs at the time of including a file 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:
1if (!include(‘test.php’)) echo “Error in file inclusion”;
2if (!require(‘test.php’)) echo “Error in file inclusion”;
Q #22) Which function is used in PHP to delete a file?
Answer:
unlink() function is used in PHP to delete any file.
Sample code:
1unlink('filename');
Q #23) What is the use of strip_tags() method?
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.
Sample code:
1//Remove all tags from the text
2echo strip_tags("<b>PHP</b> is a popular <em>scripting</em> language");
3//Remove all tags excluding <b> tag
4echo strip_tags("<b>PHP</b> is a popular <em>scripting</em> language","<b>");
Q #24) How can you send HTTP header to the client in PHP?
Answer:
header() function is used to send raw HTTP header to a client before any output is sent.
Sample code:
1header('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:
1$names=array(“Asa”,”Prinka”,”Abhijeet”);
2echo count($names);
3$marks=array(95,70,87);
4echo sizeof($marks);
Q #26) What is the difference between substr() and strstr()?
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 first 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 first occurrence of the searching string.
Sample code:
1echo substr("Computer Programming",9,7); //Returns “Program”
2echo substr("Computer Programming",9); //Returns “Programming”
Sample code:
1echo strstr("Learning Laravel 5!","Laravel"); //Returns Laravel 5!
2echo strstr("Learning Laravel 5!","Laravel",true); //Returns Learning
Q #27) How can you upload a file using PHP?
Answer:
To upload a file by using PHP, you have to do the following tasks.
#1) Enable file_uploads directive
Open php.ini file and find out the file_uploads directive and make it on.
1file_uploads = On
#2) Create an HTML form using enctype attribute and file element for uploading the file.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="upd" id="upd">
<input type="submit" value="Upload" name="upload">
</form>
#3) Write PHP script to upload the file
1if (move_uploaded_file($_FILES["upd"]["tmp_name"], "Uploads/")) {
2echo "The file "basename$_FILES["upd"]["name"]). " is uploaded.";
3else {
4echo "There is an error in uploading.";
5}
Q #28) How can you declare a constant variable in PHP?
Answer:
define() function is used to declare a constant variable in PHP. Constant variable declares without the $ symbol.
Sample code:
1define("PI",3.14);
Q #29) Which function is used in PHP to search a particular value in an array?
Answer:
in_array() function is used to search a particular value in an array.
Sample code:
1$languages array("C#""Java""PHP""VB.Net");
2if (in_array("PHP"$languages)) {
3echo "PHP is in the list";
4}
5else {
6echo "php is not in the list";
7}
Q #30) What is the use of $_REQUEST variable?

Post a Comment

0 Comments