PHP Interview Questions and Answers
PHP is a popular scripting language that has solid scope for professionals in creating dynamic websites and mobile APIs. PHP is the top language implemented in web development as its ability to handle huge projects with fewer codes.
The companies are looking for PHP professionals to manage dynamic content, session tracking, databases, and develop e-commerce websites for their clients. Here are the frequently asked PHP Questions and Answers in interviews and we hope they help you ace the technical rounds in top companies.
1.What are the variables in PHP?
The value of a variable can be changed during the execution of the PHP program and it requires compulsory usage of the $ sign at the beginning. It is possible to define a variable by simple assignment and the default scope will be the current access scope in PHP.
2. Define constraints in PHP
The constraints are the values that can’t be changed during the execution of PHP scripts and no $ sign is required for using constraints. They can’t be defined by simple assignments and they should be defined using the define () function. Constraints can be accessed throughout the PHP program without any separate scope rules.
3. Explain about sessions in PHP
A session is a way to store information that can be used across multiple pages of the whole website and the information will not be stored on the user’s computer like cookies. The information and variables of a session will be stored in a temporary directory on the server and will be available during the visit. The session variables will remain until the user closes the browser and it holds single user information for all the pages in one application.
4. What is PEAR in PHP?
PHP Extensions and Application Repository is formally known as PEAR. This is a framework and repository for all reusable PHP components and it provides a higher level of programming experience for web developers. PEAR contains all kinds of PHP code snippets and libraries along with a command-line interface to install the required packages automatically.
5. Explain the difference between $message and $$message
The main differences between the $message and $$message are $message is a regular variable and $$message is a reference variable. $message has a fixed name with fixed value but $$message stores data of the variable and it will be changed dynamically as the variable changes.
6. Is PHP case-sensitive?
PHP is partial case-sensitive as the variables are completely case-sensitive but functions are not case-sensitive. The user-defined functions can be defined in lowercase but can be referred to as uppercase.
7. What are the various types available in PHP?
The various types of PHP variables are Integers (1234), Doubles (2.21), Booleans (true or false), NULL, Arrays (“red”, “green”, “blue”, “yellow”), Strings (ABCD), Resources (database connections), and Objects ($red = new Color ()).
8. Differentiate “echo” and “print” in PHP.
The “echo” can output one or more strings but the “print” can only output one string with returning value 1. Echo is faster than print as it does not return any value and if the user wants to pass more than one parameter to echo, they should use parenthesis but the use of parenthesis is not needed for the argument list in print.
9. What are the rules to be followed for naming a variable in PHP?
The variable must start with a dollar ($) symbol followed by the variable name ($number = 123 – the number is a variable name). The variable names must start with a letter or underscore and a variable may contain letters and numbers. The symbols and characters like +, -, %, &, ^, or #, and space are not to be used in variable names. The variables of PHP should be case-sensitive as $NUMBER and $number are two different variables in PHP.
10. What are the limitations of PHP?
The limitations of PHP are it is not suitable for large content-based web applications and it is not secure as it is open-source. The ASCII files of PHP can be easily accessible and the changes in the core behavior of online applications are not allowed by PHP. PHP’s performance will be delayed if the developer is using many frameworks and tools. PHP is poor in handling errors as it lacks debugging tools.
11 .What is the use of @ in PHP?
The @ is used to suppress error messages in the PHP program and if any runtime error occurs on the line that consists of @ at the beginning then the error will be handled by PHP.
12.How to integrate PHP and HTML?
PHP scripts can be able to generate HTML as it is easy to pass information from HTML to PHP. PHP is a server-side language that can easily integrate with HTML as a client-side language. When PHP is used to execute the server-side codes and return its results as strings and objects to display them in HTML.
13. What is the importance of Parser in PHP?
A PHP Parser is an application that translates source code into machine code. The set of instructions we give in PHP will be converted to machine-readable formats by Parser in PHP.
14. What are the types of arrays in PHP?
PHP contains three types of arrays namely Indexed array, Associated array, and Multidimensional array. The indexed array is an array with a numeric key and the values will be accessed in linear order. The associate array is the array with strings for indexing elements and the values are stored with key values in strict linear index order. A multidimensional array is an array that contains one or more arrays and the values are accessed using multiple indices.
15. Explain the types of errors
The main types of errors are Notices, Warnings, and Fatal. Notices are non-critical errors that appear during the execution of the scripts and that are not visible to users. Warnings are more critical than notices and they don’t interrupt the script execution. It will be visible to users. Fatal is more critical and when it occurs, it will be terminated immediately from the execution of the scripts.
16. Define traits.
Traits are a popular PHP mechanism that allows the user to create reusable code in PHP and it is not possible to instantiate on its own. It has an intention to reduce the limitations of single inheritance by allowing a developer to reuse the sets of methods for numerous independent classes of different hierarchies of the classes in PHP.
18. How to use “foreach” loop in PHP?
The ‘foreach’ loop is used to iterate statements for the array data type. In the ‘foreach’ loop, elements will be assigned with a value in every single pass of the value and pointers will be incremented. It will happen repeatedly until the end of the array has been reached its assigned value.
The syntax for using the ‘foreach’ loop in PHP.
foreach ($array as $value)
19. What is the popularly used method for hashing passwords in PHP?
The crypt () function is the most used function as it offers a large number of hashing algorithms to be used in PHP and the algorithms are designed to be faster and more efficient.
20. Define include () function
Include () is a function that is used to copy all the contents of a file called within the function and the text wise into a file from which it is called. It produces a warning and the script will continue to be executed when the file included is not found.
21. What is the required () function in PHP?
The require () function acts as the include () function but it also takes the required files and copies the code into the file from which it is called and it will produce a fatal error and terminates the execution when the ‘include ()’ file is not found.
22. How to create cookies in PHP and why it is used?
A cookie is a small record that the server installs on the computer of clients and it stores the data about the user on the browser. Cookies are used to identify a user and they will be utilized when the client requests a particular page repeatedly. Cookies are temporary for each browser by default.
In PHP, the developer can create cookies using setcookie () method and the syntax is below
setcookie (name, value, expire, path, domain, secure, httponly);
23. How does PHP differ from ASP.Net?
PHP is a server-side scripting language while ASP.Net is a web application framework. PHP is platform independent while ASP is designed for Windows operating system. PHP has interpreted the mode of execution while ASP codes will be compiled and executed. PHP is open-source but ASP.Net has a license cost associated with it.
24. How to create a new database using MySQL and PHP?
There are four major steps involved in creating a database in MySQL with PHP and they are follows
- Establishing the connection to the MySQL Server using the PHP script
- Verifying the connection validation and if the connection is successful, then write a sample query to confirm.
- Creating the queries for input to the database and storing them into a string variable
- Generating the queries to be executed one after another.
25.Why do you have to use session_start () and session_destroy () in PHP?
The session_start () method is used to begin a new session and it resumes an existing session when it is stopped. The session_destroy () is used to destroy all the session variables. The syntax for them is as follows.