PHP Interview Questions and Answers

Hi , these PHP Interview Questions have been written specially to get you familiar with the type of questions you may encounter during your interview for the PHP programmer. As per my experience, most of interviewers hardly plan to ask any particular question during your interview, normally questions start with some basic concept of the PHP and later they continue based on further discussion and what you answer. So here I have listed some common PHP interview questions with answer that every PHP programmer should need to know, I will add few more questions with answers in coming days.

1. What is PHP?

Ans. PHP (Hypertext Preprocessor) is a server side scripting language that is embedded in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-commerce sites.
2. What are the common usage of PHP?

Ans.Common uses of PHP

  • PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close them.

  • PHP can handle forms, i.e. gather data from files, save data to a file, thru email you can send data, return data to the user.

  • You add, delete, modify elements within your database through PHP.

  • You can create SESSION variables, access cookies variables and set cookies.

  • Using PHP, you can restrict users to access some pages of your website.

  • It can encrypt data.

3. What is the purpose of php.ini file?

Ans. It is a PHP configuration file that controls and effect PHP's functionality. The php.ini file is read each time when PHP is initialized. You can check settings of php.ini by calling phpinfo() function in PHP script.
4. Is PHP a case sensitive language?

Ans. Yes, PHP is case sensitive.
5. How will you define a constant in PHP?

Ans. To define a constant you have to use define() function and to retrieve the value of a constant, you have to simply specifying its name. Unlike with variables, you do not need to have a constant with a $.
define("CONSTANT", "Hello world.");
echo CONSTANT; // outputs "Hello world."

6. What is the purpose of constant() function?

Ans. As indicated by the name, this function will return the value of the constant. This is useful when you want to retrieve value of a constant, but you do not know its name, i.e. It is stored in a variable or returned by a function.
define("CONSTANT", "Hello world.");
echo constant("CONSTANT"); // same thing as the previous line

7. What are the differences between PHP constants and variables?


  • There is no need to write a dollar sign ($) before a constant, where as in Variable one has to write a dollar sign.

  • Constants cannot be defined by simple assignment, they may only be defined using the define() function.

  • Constants may be defined and accessed anywhere without regard to variable scoping rules.

  • Once the Constants have been set, may not be redefined or undefined.

8. What are PHP magic constants?

Ans. PHP provides a large number of predefined constants like (__LINE__, __FILE__, __DIR__) to any script which it runs known as magic constants.
9. What is numeric array?

Ans. Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.
10. What is associate array?

Ans. Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.
11. What is Multidimensional array?

Ans. Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices.
12. How will you concatenate two strings in PHP?

Ans. To concatenate two string variables together, use the dot (.) operator −
echo $string1 . " " . $string2;

13. How will you find the length of a string in PHP?

Ans. The strlen() function is used to find the length of a string. Let's find the length of our string "Hello world!" −
echo strlen("Hello world!");

14. How will you get environment variables in PHP?

Ans. PHP provides a function getenv() to access the value of all the environment variables.
15. How will you get the browser's details using PHP?

Ans. One of the environemnt variables set by PHP is HTTP_USER_AGENT which identifies the user's browser and operating system.
16. How will you redirect a page using PHP?

Ans. The PHP header() function supplies raw HTTP headers to the browser and can be used to redirect it to another location. The redirection script should be at the very top of the page to prevent any other part of the page from loading. The target is specified by the Location: header as the argument to the header() function. After calling this function the exit() function can be used to halt parsing of rest of the code.
17. What is the purpse $_REQUEST variable?

Ans. The PHP $_REQUEST variable contains the contents of both $_GET, $_POST, and $_COOKIE. The PHP $_REQUEST variable can be used to get the result from form data sent with both the GET and POST methods.
18. How can you sort an array?

Ans. You can use PHP sort() function to sort an array.
19. What is the difference between single quoted string and double quoted string?

Ans. Singly quoted strings are treated almost literally, whereas doubly quoted strings replace variables with their values as well as specially interpreting certain character sequences.
$variable = "Hello";
$literally = 'This $variable will not print!\\n';
$literally = "This $variable will print!\\n";

20. How do you create a cookie, add data to it, and remove data from a cookie

Ans. Here we have created cookie "username" and assign value "phpzag" with expiration duration of 10 seconds to the timestamp.

setcookie('username', 'phpzag', time()+10);

Now we get value of cookie "username"

echo $_COOKIE['username'];

Now we remove the data of cokie. When deleting a cookie you should assure that the expiration date is in the past. Examples follow show how to delete cookies sent in previous example:

setcookie ("username", "", time() - 10);
21. How do you create a session, add data to a session and remove data from a session

Ans. First we start session by inintialzing session_start();

Now we will create session "USERNAME" and add data to our created session.


Now we get the data from our session:

22.  If cookies are disabled will the sessions work? Why?

Ans. Session will work if cokkies are disabled. If cookie is enabled on client machine then PHP will use cookies to handle sessions else if cookie is disabled on client machine check is made to ensure that if php5 is configured with

--enable-trans-sid and
--enable-track-vars option.

If yes then php will pass session id using post method automatically and we do not need to worry about it. we just have to use sesssion functions regularly. If No then we can pass session id using SID.
23  What is the difference between $var and $$var

Ans. $var is a variable and $$variable is a Reference or Dynamic variable.
24. How to include a file to a php page?

Ans. We can include a file using "include() " or "require()" function with file path as its parameter.
25. What is the difference between include() and require() Function?

Ans. If there is any problem in loading a file then the require() function generates a fatal error and halt the execution of the script whereas include() function generates a warning but the script will continue execution.
26. How will you read a file in php?

Ans. Once a file is opened using fopen() function it can be read with a function called fread(). This function requires two arguments. These must be the file pointer and the length of the file expressed in bytes.
27. How will you open a file in readonly mode?

Ans. The PHP fopen() function is used to open a file. It requires two arguments stating first the file name and then mode in which to operate. "r" mode opens the file for reading only and places the file pointer at the beginning of the file.
28. How will you get the size of a file in php?

Ans. The files's length can be found using the filesize() function which takes the file name as its argument and returns the size of the file expressed in bytes.
29. How will you check if a file exists or not using php?

Ans. File's existence can be confirmed using file_exist() function which takes file name as an argument.
30. How will you send an email using PHP?

Ans. PHP makes use of mail() function to send an email. This function requires three mandatory arguments that specify the recipient's email address, the subject of the the message and the actual message additionally there are other two optional parameters.

mail( to, subject, message, headers, parameters );

31. What is the purpose of $_FILES variable in PHP?

Ans. This is a global PHP variable. This variable is an associate double dimension array and keeps all the information related to uploaded file.
32. How will you access the uploaded file in PHP?

Ans. Using $_FILES['file']['tmp_name'] - it provides access to the uploaded file in the temporary directory on the web server.
33. How will you access the actual name of the uploaded file in PHP?

Ans. Using $_FILES['file']['name'] - it provides the actual name of the uploaded file.
34. What is the purpose of $_SERVER variable in PHP?

Ans. $_SERVER - This is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these.
35. How will you get the current date and time using PHP?

Ans. PHP's time() function gives you all the information that you need about the current date and time. It requires no arguments but returns an integer.
36. What is the purpose of getdate() function?

Ans. The function getdate() optionally accepts a time stamp and returns an associative array containing information about the date. If you omit the time stamp, it works with the current time stamp as returned by time().
37. What is the purpose of date() function?

Ans. The date() function returns a formatted string representing a date. You can exercise an enormous amount of control over the format that date() returns with a string argument that you must pass to it.
38. How will you connect a MySql database using PHP?

Ans. PHP provides mysql_connect function to open a database connection.
connection mysql_connect(server,user,passwd,new_link,client_flag);

39. How will you close a MySql database using PHP?

Ans. Its simplest function mysql_close PHP provides to close a database connection. This function takes connection resource returned by mysql_connect function. It returns TRUE on success or FALSE on failure.
bool mysql_close ( resource $link_identifier );

If a resource is not specified then last opend database is closed.
40. How will you parse an XML document using PHP?

Ans. PHP 5's new SimpleXML module makes parsing an XML document, well, simple. It turns an XML document into an object that provides structured access to the XML. To create a SimpleXML object from an XML document stored in a string, pass the string to simplexml_load_string( ). It returns a SimpleXML object.
41. How will you add a constructor function to a PHP class?

Ans. PHP provides a special function called __construct() to define a constructor. You can pass as many as arguments you like into the constructor function.
42. How will you add a destructor function to a PHP class?

Ans. Like a constructor function you can define a destructor function using function __destruct(). You can release all the resourceses with-in a destructor.
43. How will you access the reference to same object within the object in PHP?

Ans. The variable $this is a special variable and it refers to the same object ie. itself.
44. How will you create objects in PHP?

Ans. Once you defined your class, then you can create as many objects as you like of that class type. Following is an example of how to create object using new operator.

$emp = new Employee;

45. How will you call member functions of a class in PHP?

Ans. After creating your objects, you will be able to call member functions related to that object. One member function will be able to process member variable of related object only. Following example shows how to set name by calling member functions.

$emp->setName( "Tom" );

46. What is function overriding?

Ans. Function definitions in child classes override definitions with the same name in parent classes. In a child class, we can modify the definition of a function inherited from parent class.
47. What are interfaces n PHP?

Ans. Interfaces are defined to provide a common function names to the implementors. Different implementors can implement those interfaces according to their requirements. You can say, interfaces are skeltons which are implemented by developers.
48. What is the use of final keyword?

Ans. PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

You may also check:

You may like these posts