Ask your Question

PEOPLE YOU MIGHT KNOW

gourav
gourav

Moradabad,  India
Lonnie
Lonnie

Jacobus,  United States
Praveen
Praveen

Software Developer

NOIDA,  India
William
William

Web Developer

Elkhart,  United States
Vivek
Vivek

PHP Developer

Noida,  India

Write to us

Ask Question

Real-Time PHP Interview Question and Answers for Freshers & Experienced Asked 2019

MarapaReddy SudheerReddy

Want to understand the ways of trumping technical and HR interview rounds as a PHP professional? This is what we want to help you do. We have prepared a set of questions that we have observed from our experience as being the most likely ones to be asked as these rounds. This set of PHP questions are designed to help you answer at interviews from beginner to advanced levels.

What is PHP? PHP can be described as a scripting language that appears on the server-side. It finds its maximum use in web applications. WordPress and osCommerce are a couple of the popular CMS of PHP. Being object-oriented, PHP is handy in creating websites with frameworks and CMS, making it quite easy to learn.

 

1st Round: PHP Basic interview questions and answers:

Q1.     Describe PHP

Ans: PHP is a server-side scripting language whose most common use is in web applications. PHP comes with various frameworks and CMS that help create websites. Being also object-oriented, PHP resembles Java, C Sharp and other related programs, which makes it very easy to learn and implement. PHP’s CMS is built in such a way that it makes it possible for even a lay person to create sites using them. A few of the popular CMS of PHP are WordPress and osCommerce.

Q2.     Show what use “echo” has in PHP

Ans: The main purpose of echo in PHP is to help print data in the webpage. For example, the code print,  prints the text for this item from the webpage.

Q3.     What is the way in which a file is included to a PHP page?

Ans: Including a file to a PHP page is simple. All that we need to do is use "include() " or "require()" function with the file path as its parameter.

Q4.     How does include differ from require?

Ans: The main difference between differ and require relates to file execution. What happens when require() does not find the file is that there will be a fatal error which stops the script from being executed. Whereas, when include() does not find a file, it issues a warning will be issued, but will not stop the execution, which can continue.

Q5.     Describe the difference between require_once(), require(), and include().

Ans: The main difference between these is that while require() includes and assesses a particular file, require_once() does the same, but only if it has not been included before on the same page. Because of this, ideally, it is recommended to use require_once() when you want to include a file in which there are many functions. This is one way of ensuring that file is not included multiple times and to also avoid getting the “function re-declared” error.

Q6.     What are the basic differences between GET and POST methods?

Ans: These are the basic difference between GET and POST methods:

1.     In the GET method, it is possible to send only 1024 bytes, but with the POST method, we can transfer larger amount of data

2.     The GET method is relatively less secure than the POST method.

Q7.     What is the way by which to declare an array in PHP?

Ans: Eg: var $arr = array('brinjal', 'cucumber', 'carrot');

Q8.     What purpose does ‘print’ have in PHP?

Ans: Ironically, the ‘print’ function is not a real function in PHP. Rather, it is a language construct, which means it can be used without parentheses with its argument list.

Example print('Personality Development');

print 'management test');

Q9.     What is the purpose of in_array() function in PHP?

Ans: The in_array is meant for checking if an array contains a value.

Q10.  Explain the use of count() function in PHP

Ans: The use of count() is twofold: 1, to count all elements in an array; 2. To count something in an object.

Q11.  In what ways do include and require differ from each other?

Ans: These functions differ from each other mainly in the way they handle failures. When require() does not find the file, it will result in a fatal error which will halt the execution of the script. On the other hand, if include() does not find a file, it will send out a warning, but continues with the execution.

Q12.  How do you differentiate between Session and Cookie?

Ans: We can explain the differences between sessions and cookies in the following ways:

1.     While sessions are stored on the server, cookies are stored on the user's computers in the text file format

2.     While Cookies cannot hold multiple variables, session can

3.     An expiry can be set for a cookie, because of which the session only remains active as long as the browser is open. Since data is stored in the server in Session, it does not allow access to users.

4.     Cookies are used for tracking user activities, while Session is mainly used for login/logout.

Q13.  What is the way by which to set cookies in PHP?

Ans: Setcookie("sample", "ram", time()+3600);

Q14.  How is a Cookie Value retrieved?

Ans: E.g.: echo $_COOKIE["user"];

Q15.  How is a session created? How is a value set in session? How do you rmove data from a session?

Ans: Create session : session_start();

Set value into session : $_SESSION['USER_ID']=1;

Remove data from a session : unset($_SESSION['USER_ID'];

Q 16. What is the use of the explode() function?

Ans: Syntax : array explode ( string $delimiter , string $string [, int $limit ] );

What this function does is that it breaks a string into an array. Each of the array elements is a substring of strings that is formed by splitting it on boundaries formed by the string delimiter.

Q17.  Differentiate between explode() and str_split() functions

Ans: While the str_split function splits string into array by regular expression; Explode splits a string into array by string.

Q18.  Describe the uses of the mysql_real_escape_string() function?

Ans: The mysql_real_escape_string() function is used to escape special characters in a string for use in an SQL statement.

Q19.  What use does the header() function have in PHP?  

Ans: The purpose of the header() function is to send a raw HTTP header to a client browser. Bear in mind that this function must be called before sending the actual output. For example, make sure you do not print any HTML element before using this function.

Q20.  How is a page redirected in PHP?

Ans: It can done using the following code: header("Location:index.php");

Q21.  How do you stop the execution of a PHP scrip?

Ans: The execution of a PHP scrip can be stopped using the exit() function of a page.

Q22.  In a PHP based site, how is a page set up as a home page?

Ans: In all PHP-based sites, index.php is the default name of the home page.

Q23.  Using what do you find the length of a string?

Ans: The length of a string can be found using the strlen() function.

Q24.  Describe the use of rand() in PHP

Ans: rand() can be used to generate random numbers. If called without the arguments it returns a pseudo-random integer between 0 and getrandmax(). Let us say you want a random number between 5 and 15 (inclusive). In this case, you need to use rand (5, 15). Note that rand() cannot be used to generate cryptographically safe values, because of which using it for cryptographic uses should be avoided. However, if a cryptographically secure value is what you are looking for, you could think of using openssl_random_pseudo_bytes().

Q25.  Describe the use of isset() in PHP

Ans: The isset() function is used in PHP to determine if a variable is set and is not NULL.

Q26.  How do mysql_fetch_array() and mysql_fetch_assoc() differ from each other?

Ans: While mysql_fetch_assoc function obtains a result row as an associative array; the mysql_fetch_array() fetches either an associative array, a numeric array, or both.

Q27.  What is an associative array?

Ans: Arrays that use string keys are called associative arrays.

Q28.  For what purpose is an “action” attribute in a html form used?

Ans: The purpose of the action attribute is to determine where to send the form-data in the form submission.

Q29.  What use does the "enctype" attribute have in html form?

Ans: This attribute helps understand the manner in which the form-data should be encoded when submitting it to the server. enctype needs to be set as "multipart/form-data" when a form is being used for uploading files.

Q30.  What do you understand by a constant?

Ans: Using define() directive, like define ("MYCONSTANT",150)

Q31.  Describe the use of “ksort” in PHP

Ans: ksort is used to sort an array by key in reverse order.

Q32.  What do you understand by SQL injection?

Ans: The SQL injection is a malicious code injection technique that identifies and exploits SQL vulnerabilities in web applications.

Q33.  Why do you need to use the x+ mode in fopen()?

Ans: It is used for the following: Read/Write. It creates a new file and returns FALSE and an error if the file already exists.

Q34.  What is the way by which the position of the first occurrence of a substring in a string is found?

Ans: The position of the first occurrence of a substring in a string is found using strpos().

 

2nd Round: PHP Technical Interview Questions and answers:

Q1.     Identify the main error types in PHP and explain how they differ from each other.

Ans: It is important for developers to get clarity and understanding of the error types. In the way diagnosis is critical for doctors in treating diseases, error types are crucial in helping developers understand all that goes on during the development. It helps them zero in on what to look out for and debug.

PHP has three main type of errors:

•         Notices: Notices are simple, non-critical errors that occur when the script is being executed. Accessing an undefined variable is an example of a Notice.

•         Warnings: On a notch higher than Notices in terms of gravity; Warnings nevertheless allow the scripts to continue the execution. An include() a file that does not exist is an example of this kind of error in PHP.

•         Fatal: As the name suggests, Fatal errors are serious in that they terminate the script execution when they occur. Accessing a property of a non-existent object or require() a non-existent file is an example of a Fatal error.

Q2.     How is error reporting in PHP enabled?

Ans: Error reporting in PHP is enabled in the following manner:

Check if “display_errors” is equal “on” in the php.ini or declare “ini_set('display_errors', 1)” in your script.

Then, include “error_reporting(E_ALL)” in your code to display all types of error messages during the script execution.

The importance of enabling error messages, especially during the debugging process, can be understood from the fact that it helps the developer to instantly get to the exact line that in which the error is being produced. It also helps the developer visualize if the script is behaving rightly.

Q3.     By what method would you declare a function that receives one parameter name, hello?

Ans: If hello is true, then the function must print hello, but if the function doesn’t receive hello or hello is false the function must print bye.

function showMessage($hello=false){

 echo ($hello)?'hello':'bye';

}

?>

Q4.     Describe the three scope levels available in PHP and explain the importance of knowing them.

Ans: 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

The importance for any PHP developer of the knowledge of the three scope levels available in PHP lies in the fact that it opens the way to the understanding that building applications goes beyond being just able to write code. It also shows the level of understanding the developer has of privileges and accessibility of that code. Protected variables or methods are extremely important, because of which, getting an understanding of the scope is necessary to protect the integrity of the data in the application. This knowledge also provides a clear path through the code.

Q5.     How to create a mysql connection?

Ans: Example (PDO)

$servername = "localhost";

$username = "username";

$password = "password";

 

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";

   }

catch(PDOException $e)

   {

   echo "Connection failed: " . $e->getMessage();

   }

?>

 

Example (MySQLi Object-Oriented)

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB"; // Optional

 

// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);

 

// Check connection

if ($conn->connect_error) {

   die("Connection failed: " . $conn->connect_error);

}

echo "Connected successfully";

?>

 

Example (MySQLi Procedural)

$servername = "localhost";

$username = "username";

$password = "password";

$dbname = "myDB"; // Optional

 

 

// Create connection

$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection

if (!$conn) {

   die("Connection failed: " . mysqli_connect_error());

}

echo "Connected successfully";

?>

Q6.     How do you execute an SQL query and how do you fetch its result?

Ans: Example (MySQLi Object-oriented)

$sql = “SELECT id, firstname, lastname FROM MyGuests”;

$result = $conn->query($sql); // execute sql query

if ($result->num_rows > 0) {

   // output data of each row

   while($row = $result->fetch_assoc()) { // fetch data from the result set

       echo “id: “ . $row[“id”]. “ – Name: “ . $row[“firstname”]. “ “ . $row[“lastname”]. “
”;

   }

} else {

   echo “0 results”;

}

Example (MySQLi Procedural)

$sql = “SELECT id, firstname, lastname FROM MyGuests”;

$result = mysqli_query($conn, $sql); // execute sql query

if (mysqli_num_rows($result) > 0) {

   // output data of each row

   while($row = mysqli_fetch_assoc($result)) { // fetch data from the result set

       echo “id: “ . $row[“id”]. “ – Name: “ . $row[“firstname”]. “ “ . $row[“lastname”]. “
”;

   }

} else {

   echo “0 results”;

}

Example (PDO)

Method 1:USE PDO query method

$stmt = $db->query(‘SELECT id FROM Employee’);

$row_count = $stmt->rowCount();

echo $row_count.’ Rows selected’;

Method 2: Statements with Parameters

$stmt = $db->prepare(“SELECT id FROM Employee WHERE name=?”);

$stmt->execute(array($name));

$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);

7.        Write a program using while loop

Ans:

$x = 1;

while($x <= 5)

{   echo "The number is: $x
";

   $x++;

}

?>

Q8.     How do you create a text file in PHP?

Ans:

$filename = "/home/user/guest/newfile.txt";

$file = fopen( $filename, "w" );

if( $file == false )

{

echo ( "Error in opening new file" ); exit();

}

fwrite( $file, "This is a simple testn" );

fclose( $file );

?>

Q9.     How do you convert one date format into another in PHP?

Ans:

$old_date = date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34

$old_date_timestamp = strtotime($old_date);

$new_date = date('Y-m-d H:i:s', $old_date_timestamp);

?>

HR Round:

Describe your career path

What additional educational qualifications have you acquired pertaining to PHP over the years in addition to your main qualification?

Have any of your important PHP projects been delayed in your organization? What were the reasons for it? What steps did you take then?

How would you react if your reporting manager called you and said that you are lagging behind your teammates when it comes to your allotment of work in PHP? The manager could have documentation to show this delay on your part.

Do you have experience in working with cross-cultural teams?

Conclusion:

Determined to pass the technical and HR hurdles at your interview for PHP developer? We want to put you up right there and make sure you face no hiccups! Our set of 30 questions has been derived from our experience in helping people like you go through these rounds without difficulty.

Whether you are a company that is looking to hire a PHP developer for either your desktop application, or e-commerce site, or anything else, or are a candidate unclear about what to expect, we hope we will be of use.

We wish you the best!

MarapaReddy SudheerReddy

Please rotate your device

We don't support landscape mode on your device. Please rotate to portrait mode for the best view of our site