Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Jun 12, 2024

PHP interview questions

Author Akash
0 upvote
Table of contents
Projects ideas to get shortlisted for data roles at MAANG
13 Jun, 2024 @ 01:30 PM
Speaker
Muskan Rathore
Data Scientist @

Introduction

This blog enlists the most important 30 intermediate-level PHP Interview questions. Questions in this blog are of intermediate difficulty and have a high probability of being asked in interviews. These are questions related to basic concepts of the PHP language. So, let's begin to go through the questions one by one

 

1. Describe the steps to create a database using PHP and MySQL?
To create a database using MySQL and PHP, we have to follow the following steps-
Connect MySQL to your PHP script.
After a successful connection, run a SQL query to create a database after storing it in a string variable.

 

2. What is the $_GET method in PHP?
The $_GET method is a superglobal variable in PHP. These are variables that are always available in PHP in all the scopes. After submitting an HTML form with method = "get," it is used to collect the form data.

 

3. What is the $_POST  method in PHP?
Like the $_GET  method, it is also a super globe variable and always available in all the scopes. It is also used to submit HTML form data, and if we submit by this method, the data is collected by the $_POST method. Unlike the $_GET  method, the $_POST method has no restriction on the amount of data that can be sent. HTTP header is used to send data in the Post method. ASCII and binary data in both forms can be transferred through this method.

 

4. What is the function of $_FILES['userfile']['name'] , $_FILES['userfile']['tmp_name'] and $_FILES['userfile']['error'] commands?

  • $_FILES['userfile']['name']It is used to represent a file's original name on the client's system.
  • $_FILES['userfile']['tmp_name']- It is used to represent a file's temporary name while it is stored on the server.
  • $_FILES['userfile']['error'] - It is used to contain the error code while uploading a file.

 

5. What is fopen() in PHP?
fopen() is used to either open or create a file in PHP. If the file already exists, it will be opened, but it will be created first then opened if it does not exist. The file is created in the same directory that contains the PHP scripts.
Syntax-

$newfile = fopen("newfile.txt", “w”)

 

6. What is $_SERVER  function in php?
It is a superglobal variable and holds the details about the headers, paths, and script locations. we can pass different elements to it to carry out various functions. for example- $_SERVER['PHP_SELF']  will return the currently executing script's filename, and $_SERVER['SERVER_ADDR'] is used to return the host server's  IP address.

 

7. Describe the use of the session_start() and session_destroy() function?
The session_start() function helps us start a new session. It can also resume the current session which has been stopped and return it if resumed.
The session_destroy() is used to clear any active session.

 

8. Differentiate between require() and require_once()?
The major difference between require() and require_once() is that require() is used to add a file in PHP, and if it can't find the file, it will produce an error message and stop execution. Whereas, in the case of require_once(), the file is only included if it's not included before. Though it also shows an error message and stops execution if it can't locate the file.

 

9. What is the __construct() function in PHP?
__construct() function is used to initialize the properties of an object while creating it. If declared, this function is automatically called when an object is created.
 

10. What is a "__destruct" in PHP?
The "__destruct" function is called at the end of the script to destruct the object. If we create a "__destruct" function, it is called automatically at the end of the script, or there aren't any references to the object anymore.

 

11. What is the $_ENV  function in php?
It is a superglobal associative array. It is used to store available environment variables for the current script. A similar function $_HTTP_ENV_VARS  is also used for the same purpose, but it is not a superglobal and has a limited scope. Since the shell of the PHP parser provides these environment variables, thus the environment variables in the function can vary depending on the platform. 
 

12. What is overriding and overloading in PHP?
Overriding happens when a derived class overrides a method defined in the parent class. Whereas overloading means defining the same functions with different parameters, we use the magic method_call for overriding in PHP.

 

13. Describe type hinting in PHP?
It helps us specify the type of data we expect to pass as an argument while declaring the function. When the code is executed, whether the arguments have the same data type as specified or not. In the case of different data types, it shows an error message and halts the execution.

 

14. Describe the callback functions in PHP?
The callback is a function in PHP that can be passed as an argument to another function. It can also be an existing function. To use it as a callback function, we need to pass its name as an argument to another function using string.

 

15. What is an anonymous function in PHP?
Anonymous functions are also known as closures; these are used to create functions with no specified name. These are implemented using closure classes. They end with a semicolon because they are expressions.

$func = function ($ar1, $ar2) { return $val ; } ;

 

16. What is the foreach loop in PHP?
It is a looping method in PHP that is used while iterating through an array. It works by assigning a value to the element and incrementing the pointer in each pass. This process gets repeated until we iterate the whole array.

foreach ($arr as $iterator)
{
	//code goes here.
}

 

17. How do PHP and HTML interact?
PHP is a server-side scripting language, whereas HTML is a client-side language, PHP can generate HTML. Similarly, HTML can also pass information to PHP. PHP gets executed on the server-side, and then it can pass its results to the HTML in the form of strings, objects, arrays, etc.

 

18. Is it possible for javascript to interact with PHP?
Like generating HTML, PHP can also generate javascript variables, which can be further executed in the browser. And with this, PHP can get variables using URLs.

 

19. How can we connect to a URL in PHP?
In most cases, the installation of PHP automatically installs a library called cURL, which stands for client URL. It helps us connect to a URL and retrieve the content and data associated with that page, such as HTML, HTTP header, etc.

 

20. What are global variables in PHP?
Super global variables are predefined variables available across the program, i.e., in all the scopes. they can be used across the program without any special conditions or requirements. The global variables in PHP are as follows-
$_SERVER, $_REQUEST, $_POST, $_GET, $_FILES, $_ENV, $_COOKIE, $_SESSION

 

21. What is a parser in PHP?
The software used to convert source code into simpler code that the computer can understand is known as the parser. token _get_all function can be used in PHP to parse PHP code.

 

22. What are traits in PHP?
These mechanisms help us create reusable code in PHP, as PHP doesn't support multiple inheritances. It helps us to bypass the limitations occurring because of single inheritance.

 

23. What are cookies in PHP?
It is a small record containing the user's information and can be installed on the user's computer by the server. In simple words, it is a bundle of information unique to every URL that is saved on the user's system, and whenever the user tries to access that site, this data is also passed to the site. These mainly help the sites to facilitate users by remembering their preferences each time they visit the site.

 

24. How are passwords hashed in PHP?
We use the crypt() function for hashing passwords, and the main reason is that it provides large numbers of fast and efficient hashing algorithms like sha1, sha256, and md5.

 

25. What is asp.net?
ASP.NET is a free, open-source web application framework that can be used on almost any platform. It also can compile and execute code. It allows you to build your web apps using a language called razor, a combination of HTML and C#.

 

26. What is Memcache in PHP?
It is a storage space for memory. It can be run on single or multiple servers. Several projects might share a single instance of Memcache. Also, we can configure the clients to handle different sets of instances, thus allowing two instances of Memcache to run simultaneously. It will work fine until there is a partition of data among both processes.

Must Read PHP Projects With Source Code and Power Electronics Interview Questions

 

27. What is Memcached in PHP?
It is a caching daemon(Memcached daemon) specially designed to decrease database load for dynamic web applications. It helps us speed up the database-driven sites by caching the data and other objects in server memory. This decreases the number of times the data source needs to be read.

 

28. How can we terminate the execution of a script in PHP?
We use the exit() function to terminate the execution of scripts in PHP. This built-in function prints an output message then terminates the execution. The output message can be passed to the exit() function as a parameter. The function first displays the message then terminates the execution.

 

29. What are interfaces in PHP?
Interfaces in PHP are used to specify the methods a class should implement. Interface keyword is used to declare interfaces, and to implement it, the class must use the implements keyword. Whenever a class implements an interface, it has to implement all the methods in the interface.

 

30. What are abstract classes and methods in PHP?
Abstract classes have at least one abstract method, which means that the method is declared in the class but has no definition to it. Thus whenever a child class inherits this class, it needs to define these methods. Abstract classes or methods are defined using abstract keywords.

Click on the following link to read further: Javascript Interview Questions and Answers

Key Takeaways

In this blog, we enlisted 30 intermediate-level PHP Interview questions that are highly probable to be asked in interviews to check your understanding of PHP. This series of questions has covered more basic and advanced level questions in PHP Interview questions and Advanced PHP Interview questions respectively. Do check them out once you have read the above questions. If you like this blog, do share it with your friends!

 

Related Links:

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp
Previous article
PHP Interview Questions for 5 year Experience
Next article
Most Asked PHP oops Interview Questions and Answers