Important PHP Interview Questions and Answers
If you're in the running for a position that requires knowledge of PHP, read on for great questions to quiz yourself on your PHP prowess!
Join the DZone community and get the full member experience.Join For Free
First Round: Basic PHP 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 CMSs that help create websites. Being object-oriented, PHP resembles languages such as Java and C#, which makes it very easy to learn and implement. A few of the popular applications built on 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 following code prints the text for this item from the webpage: <?php echo 'Branded shirts'; ?>
Q3. What Is the Way in Which a File Is Included in a PHP Page?
Ans: Including a file to a PHP page is simple. All that we need to do is use the
require() function with the file path as its parameter.
Q4. How Does include Differ From require?
Ans: The main difference between
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, 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:
In the GET method, it is possible to send only 1024 bytes, but with the POST method, we can transfer a larger amount of data.
The GET method is relatively less secure than the POST method.
Q7. How Do You Declare an Array in PHP?
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.
print 'management test';
Q9. What Is the Purpose of in_array() function in PHP?
in_array is meant for checking if an array contains a value.
Q10. Explain the Use of the 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:
While sessions are stored on the server, cookies are stored on the user's computers in text file format.
While cookies cannot hold multiple variables, a session can.
An expiration 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.
Cookies are used for tracking user activities, while sessions are mainly used for login/logout.
Q13. How Do You Set Cookies in PHP?
Setcookie("sample", "ram", time()+3600);
Q14. How Is a Cookie Value Retrieved?
Q15. How Is a Session Created? How Is a Value Set in Session? How Do You Remove Data From a Session?
Create a session:
Set the value of a session:
Remove data from a session:
Q 16. What Is the Use of the explode() Function?
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 a string into an array using regular expressions;
explode splits a string into an array.
Q18. Describe the Uses of the mysql_real_escape_string() Function?
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 be done using the following code:
Q21. How Do You Stop the Execution of a PHP Script?
Ans: The execution of a PHP script can be stopped using the
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. How Do You Find the Length of a String?
Ans: The length of a string can be found using the
Q24. Describe the Use of rand() in PHP
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, so using it for cryptography should be avoided. However, if a cryptographically secure value is what you are looking for, you could think of using
Q25. Describe the Use of isset() in PHP
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?
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 an 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 Forms?
Ans: This attribute helps to 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 Is a Constant?
Ans: Using the
define() directive, like
Q31. Describe the Use of “ksort” in PHP
ksort is used to sort an array by key in reverse order.
Q32. What Is SQL Injection?
Ans: 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
Opinions expressed by DZone contributors are their own.