There are 25 interview questions and their answers for a php developer which can help you getting job as php developer. Generally, these questions are asked for fresher or experience candidate.
1. What is PHP?
Ans: PHP stands for Hypertext Preprocessor which is open source server side scripting language that is used for web development and can be easily embedded into HTML.
2. What are uses of GET, POST and REQUEST?
Ans: a) GET Method: This method is used to send data with a page request with URL. In other words, When data is send using GET method, data is appended to the URL in encoded form and separated by the ? character. Ex: http://www.test.com/index.php?name1=value1&name2=value2. The PHP provides $_GET variable to access all the sent information using GET method.
b) POST Method: This method is used to send form data. The POST method does not have any restriction on data size to be sent and it is secure. The PHP provides $_POST variable to access all the sent information using POST method.
c) REQUEST Method: Using REQUEST method, we can access all the sent information using POST and GET both.
3. What is session?
Ans: A session is a way to store information (in variables) to be used across multiple pages. The PHP provides $_SESSION variable to store and access data of a session.
4. What are differences between session and cookies?
Ans: session variables are stored on server and cookies are stored on client system and so cookies are not secure. $_SESSION is used for accessing session variables and $_COOKIES is used for accessing cookies variables.
5. Is it possible to use session without cookies?
Ans: No, It is not possible to use session without cookies because session id is stored in cookies.
6. How to start and destroy a session in php?
Ans: session_start() function is used to initialize session which is called on the top of a page generally and session_destroy() function is used to destroy a session.
7. How to get current session id in php?
Ans: session_id() function is used to get current session id.
8. How to redirect pages in php?
Ans: By using header('Location: http://www.example.com/');
9. Why is “header already sent” error generated?
Ans: This error message gets triggered when anything is sent or output before send HTTP headers. Common reasons for this error are: Accidental whitespace, often at the beginning or end of files or explicit output, such as calls to echo, printf.
10. How to fix “header already sent” error?
Ans: To avoid this error, remove whitespaces and do not use any echo statement or print function before using header function. Also we can use ob_start() for output buffering like below:
11. What is the difference between echo and print()?
Ans: echo is a statement which is used to display the output and print() is a function and it returns value. Echo is faster than print(). echo can pass multiple string separated as ( , ). Ex:
$name = "Array99 ";
$profile = "Blogger";
$age = 25;
echo $name , $profile , $age, " years old";
But in print() function, it gives fatal error.
12. What is the use of nl2br()?
Ans. This function converts new line character ‘\n’ to html element ‘<br>’.
13. How to unset a variable?
Ans: Using unset() function, we can unset a variable. Ex: unset($var1);
14. What are superglobal variables in PHP?
Ans: $GLOBALS, $_SERVER, $_GET, $_POST, $_FILES, $_COOKIE, $_SESSION, $_REQUEST, and $_ENV are super global variables.
15. Which superglobal variable is used to get file information when file is uploaded?
Ans: $_FILES superglobal variable is used. $_FILES contains all the uploaded file information. This information is:
- The original name of the file on the client machine.
- The mime type of the file, if the browser provided this information.
- The size, in bytes, of the uploaded file.
- The temporary filename of the file in which the uploaded file was stored on the server.
- The error code associated with this file upload.
16. How to get variable information in PHP?
Ans: var_dump() function is used to get information of a variable.
$b = 3.1;
$c = true;
$d = “name” ;
Output: float(3.1) bool(true) string(4) "name"
17. What are indexed and associative array?
Ans: The keys of an indexed array are integers, beginning at 0 and keys of an associative array are strings.
18. How to print an array in PHP?
Ans: print_r() function is used to print an array.
19. How to include a file to a php page?
Ans: Using include() or require() function with file path as its parameter.
20. What are differences between require_once(), require() and include()?
Ans: These three functions are used to include files in another file. require_once() function is used to include a file only once in a page but if file is not found, it generates fatal error. require() and include() are also used to include a file and using these functions we can also include same file multitimes, Only difference between both is, if file is not found while using require(), it generates fatal error and while using include() function, it generates warning.
21. What are the different types of errors in PHP?
Ans: There are three basic types of runtime errors in PHP:
- Notices: These are non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined.
- Warnings: These are more serious errors - for example, attempting to include() a file which does not exist.
- Fatal errors: These are critical errors and at this point, script fails and stop.- for example, instantiating an object of a non-existent class, or calling a non-existent function.
22. What are the differences between in_array() and array_search() function?
Ans: in_array() function checks values exists or not in an array and return true if exist otherwise false. array_search() function return key of value if found otherwise return false.
23. What are the differences between array_combine() and array_merge()function?
Ans: array_merge() function is used to merge multiple arrays whatever these are associative or numeric array. array_ merge () function is used to create an array by using one array for keys and another for its values.
23. What are the sorting functions in php?
Ans: Sorting functions are: sort(), rsort(),asort(),ksort () etc.
24. How can you submit a form without a submit button?
25. How can we get the IP address of the client?
Ans: Using $_SERVER["REMOTE_ADDR"].