Let us now begin with the basic PHP interview questions and answers.
1. Who do we know as the father of PHP?
Rasmus Lerdorf who created the language in 1994.
2. What did the acronym PHP originally stand for?
Initially, PHP stood for Personal Home Page, but it now stands for the recursive acronym for PHP: Hypertext Preprocessor.
3. What are some of the common applications of PHP?
PHP is commonly used to perform:
- system functions. It can create, open, read, write, and close to files on a system.
- handling forms. It can help. Collect data from files, save data to a file, send data through email, return data to the user, etc.
- tasks that involve adding, deleting, and modify elements within your database
- accessing cookies variables and set cookies.
- user restriction to access some pages of your website
- data encryption
4. What is the recommended PHP version?
The latest versions of PHP are 7.2.30, 7.3.17, and 7.4.5, which was released in April 2020
5. Which programming language is PHP similar to?
PHP is influenced by Perl and C. As such, PHP syntax resembles these languages.
6. What is the PHP scripting engine called?
PHP is powered by the scripting engine Zend Engine 2.
7. What is the main difference between PHP4 and PHP5?
PHP4 uses Zend Engine 1 and doesn't support the OOPs concept, while PHP5 supports the OOPs concept and uses Zend Engine 2.
8. Is PHP a case sensitive language?
Not fully. PHP is partly a case sensitive language where the variable names are case-sensitive but function names are not. Also, user-defined functions aren’t case sensitive but the rest of the language is.
9. What are the popular frameworks in PHP?
Some popular PHP frameworks are:
- CodeIgniter
- CakePHP
- Symfony
- Zend Framework
- Yii 2
--
10. Name the popular Content Management Systems (CMS) in PHP.
- WordPress
- Joomla
- Magento
- Drupal
11. What is PEAR in PHP?
PEAR stands for PHP Extension and Application Repository and is an online repository of free, open-source PHP software packages. It was created as a centralized platform to manage, develop and distribute reusable PHP components. PEAR provides various packages and functions, from database and networking to file system access and remote process control. It also includes robust tools to help developers quickly and easily create web-based applications and services.
12. What is the difference between static and dynamic websites?
Static websites are stored in a file system and served as is to the user. These websites are not dynamic and cannot be changed without manually editing the files on the server. On the other hand, dynamic websites are content-rich and can be altered based on various external factors. For example, content may be changed on the fly based on user input, or the content may be generated with the help of a database.
13. What are the characteristics of PHP variables?
PHP variables are values stored in memory that the user can manipulate. They store data for a specific purpose and can be used in calculations and other processes. PHP variables can be declared with keywords such as 'var,' 'int,' 'float,' and' array.' After announcing a variable, it can then be used to store a value.
14. What are the rules for naming a PHP variable?
Naming a variable in PHP is simple, but some rules must be followed. Variable names must begin with a letter or an underscore, followed by any number of letters, numbers, or underscores. Additionally, PHP variable names are case sensitive, meaning that a variable named 'myVariable' is distinct from 'my variable.' Furthermore, variable names cannot use PHP keywords such as 'int,' 'float,' or 'array.'
15. What are the rules to determine the "truth" of any value not already of the Boolean type?
In PHP, the "truth" of any value is determined by the following rules: a value is considered "true" if it is a non-zero number or a non-empty string. Any other matters, such as an empty string or zero value, are considered false. Additionally, an array is considered accurate if it contains at least one element and false if it is open. Null is also regarded as wrong.
16. What is NULL?
NULL is a particular type of value in PHP. It represents a variable with no value and is usually used to denote the absence of a deal.
17. How do you define a constant in PHP?
A constant is a value in PHP that cannot be changed once it has been defined. Constants are defined using the define() function, which takes two arguments: the name of the constant and the value to assign it. Constants can be limited to storing numbers, strings, or arrays, and they are most often used to store configuration values that need to be accessible from different parts of a program.
18. What is the purpose of the constant() function?
The constant() function is a valuable tool that can be used to define a constant. This value remains unchanged throughout a script's lifetime and can be used as a reference for various calculations.
19. What are the differences between PHP constants and variables?
One of the most significant differences between PHP constants and variables is their scope. In PHP, constants are global, meaning they are accessible in all parts of an application, while variables are generally only accessible in the area where they were declared. Additionally, constants must be explicitly defined before they can be used, while variables can be said without initializing them.
20. What is the purpose of the break and continue statement?
Break and continue statements are two fundamental concepts of programming. A break statement is used to terminate a loop or switch statement immediately. In contrast, a continue statement is used to skip the current iteration of the loop and continue with the next iteration.
21. What are the two most common ways to start and finish a PHP code block?
When writing a PHP block of code, the two most common ways to start and finish it are by using the opening tag.
22. What is the meaning of a final class and a final method?
A final class is a class that cannot be extended or inherited. Any methods, instance variables, and constants inside the course cannot be modified or used in another category.
23. How can you compare objects in PHP?
Comparing objects in PHP is a valuable way to determine if two things are equal. The comparison is made based on the properties of the object and the values of those properties. PHP offers a built-in function for comparing objects called var_dump().
24. What are constructors and destructors in PHP?
Constructors and destructors are two essential concepts in the PHP language. A constructor is a particular method automatically called when an object is created. This process allows the thing to initialize any necessary data and perform any setup required before use.
25. Explain the syntax for the 'for each' loop with an example.
The 'for each' loop is a loop in programming languages that allows for the iterative processing of elements. It is most commonly used when a program requires multiple parts in a list or array. To use the for each loop, the programmer must first declare the variable before the loop, which will store each element as the loop iterates through the list. For example, to loop through the array of integers called myArray, the following code can be used: for each (int i in myArray) { //your code }.
26. What is the difference between single quoted string and double quoted string?
The primary difference between single quoted strings and double-quoted strings is that single quoted strings are literal and do not allow for the evaluation of variables, while double-quoted strings do.
27. How to concatenate two strings in PHP?
Concatenating two strings in PHP is straightforward. To do so, use the concatenation operator, which is a period (.).
28. What is the difference between "echo" and "print" in PHP?
The primary difference between "echo" and "print" in PHP is that "echo" is a language construct, and "print" is a function. "echo" also has slightly better performance and is more succinct, making it preferable in situations where performance and size matter.
29. Name some of the functions in PHP.
Some of the most commonly used functions in PHP include strlen(), str_replace(), urlencode(), and md5(). These functions can perform various tasks, such as counting the number of characters in a string, replacing the contents of a string, encoding a URL string, and generating a hash value.
Let us now go through the intermediate PHP interview questions and answers.
30. How can PHP interact with HTML?
PHP is designed to interact with HTML. It’s possible to embed PHP scripts in an HTML page without a problem and transfer information from HTML to PHP.
31. How can you embed PHP code in an HTML page?
When embedding PHP code with an HTML file, we must use the .php file extension for that file. This allows your webserver to send the file to PHP for processing. If the webserver is configured to use a different extension for PHP files, that extension must be used instead.
We need to use PHP start and end tags to tell the web server where the PHP code starts and ends. There are three styles of start and end tags that PHP parser recognizes.
XML style: Mostly used when when merging PHP with XML or HTML documents
<? php
PHP code
?>
Short style: This is the simplest type, though it is not recommended to avoid interference with XML document declarations.
<?
PHP code goes here
?>
Long style: This style is the longest and matches the tag style used with JavaScript.
PHP code
32. How does PHP interact with Javascript?
Because PHP is a server-side language and Javascript is a client-side language, these two cannot interact. But we can exchange variables since it’s possible to generate Javascript code via PHP, which can be executed by the browser and specific variables can be passed back to PHP via the URL.
33. Name the different types of variables in PHP.
There are eight data types used to construct variables in PHP:
Integers: these are whole numbers without a decimal point, like 4195.
Doubles: are floating-point numbers, like 3.14159 or 49.1.
Booleans: come with only possible values of either true or false.
NULL: is a special type with only one value: NULL.
Strings: are sequences of characters.
Arrays: are named and indexed set of other values.
Objects: are f programmer-defined classes, which can include both other kinds of values and functions that are specific to the class.
Resources: are special variables that refer to resources external to PHP (such as database connections).
34. List the main types of errors in PHP and explain their differences.
There are three main error types in PHP:
- Notices. These are non-critical errors that can occur during the script execution. These are not visible to users. Accessing an undefined variable is an example of a 'Notice'.
- Warnings. These are more critical than Notices, but just like them, Warnings don't interrupt the script execution. However, these are visible to the user by default. Example: include() a file that doesn't exist.
- Fatal. This is the most critical error type which, when occurs, immediately terminates the script execution. Accessing a property of a non-existent object or require() a non-existent file is an example of this error type
35. What are 'Traits'?
A mechanism that lets you create reusable code in PHP and similar languages where multiple inheritances are not supported is called Traits. It's not possible to instantiate it on its own.
36. Can the value of a constant change during the script’s execution?
No, the value of a constant cannot be changed once it’s declared during the PHP execution.
37. Name and define the three scope levels available in PHP.
Private – Detectable only in its own class
Public – Can be seen by any other code accessing the class
Protected – Can be seen by classes parent(s) and classes that extend the current class
38. What do we mean by ‘escaping to PHP’?
The PHP parsing engine must be able to distinguish PHP code from other page elements. The mechanism to achieve this is called ‘escaping to PHP’.
39. How many types of an array are there in PHP?
Three types of an array are present in PHP:
Indexed array, which is an array with a numeric key.
An associative array is where each key has its specific value.
A multidimensional array contains one or more arrays within itself.
Read more: Learn what are Arrays in PHP?
If you're eager to gain the skills required to work in a challenging, rewarding, and dynamic IT role - we've got your back! Discover the endless opportunities through this innovative Post Graduate Program in Full Stack Web Development course designed by our partners at Caltech CTME. Enroll today!
40. What do we mean by a PSR standard? What is its importance?
Because varying coding standards exist between developers and companies, one may run into problems when trying to fix another developer’s code which might have a different structure. That’s where a PSR standard comes in. It standardizes how the code should look, which helps eliminate issues and even syntax errors in some cases.
Advanced-Level PHP Interview Questions
We will now look into some of the advanced PHP interview questions and answers.
41. What is the use of the function' imagetypes()'?
The imagetypes() function is an inbuilt PHP function which is used to give the image types supported by the current version of PHP inbuilt installed library.
42. What is needed to be able to use the image function?
GD library, which is the inbuilt installed library in PHP, is needed to execute image functions.
43. How do you handle failures in execution with include() and require() functions?
If the function requires () fails to access the file, then it ends with a 'fatal' error. However, in case of the include() function, it gives a warning, and the PHP script continues to run.
44. What is the main difference between require() and require_once()?
require(), and require_once() basically perform the same task. The difference is that the latter checks whether the PHP script is already included or not before executing it.
45. What is the primary difference between include_once() and include()?
Same as above.
46. How do you display text with a PHP script?
There are two ways to do this:
47. How do we use PHP to display information of a variable that's readable by a human?
To achieve this result, we need to use print_r().
48. How do we set an infinite execution time for PHP script?
We add a set_time_limit(0) at the start of a script to make the time of execution infinite. This helps to prevent the PHP error of 'maximum execution time exceeded.' We can also specify this in the php.ini file.
49. Explain the PHP error 'Parse error in PHP - unexpected T_variable at line x'.
This is a syntax error in PHP which tells us that a mistake at the line x stops parsing and executing the program.
50. How to export data into an Excel file?
Commonly, the data is obtained in a format supported by Excel. For instance, we can write a .csv file, select a separator between fields (a comma for example), and then proceed to open the file with Excel.
51. How to execute a PHP script from the command line?
To execute a PHP script, we can use the PHP Command Line Interface (CLI) and specify the file name of the script in this way:
PHP script.php
52. What is the main difference between asp net and PHP?
ASP.NET, a Microsoft technology, is a web application framework built on the .NET framework. It is an open-source server-side scripting language used to make dynamic webpages. Unlike PHP, ASP.NET is not a scripting language but allows developers to write code using any.
53. What is the use of session and cookies in PHP?
Sessions and cookies are essential in PHP, as they provide a way for the server to store information about a user's interaction with the website. Sessions are used to store information about a user's session, such as login status and other information that the website needs to know. Cookies store information that the server can use to identify a user, such as a username or a password.
54. What is overloading and overriding in PHP?
PHP is a powerful scripting language used to create websites and web applications. It has many features, one of which is overloading and overriding. Overloading is the ability to define multiple methods with the same name but with different parameters. Overriding is the ability to redefine an inherited way.
55. What is the difference between $message and $$message in PHP?
The difference between $message and $$message in PHP is that the first is a variable with a fixed value. In contrast, the second is a variable stored in another variable. For example, if the $message is set to "Hello World," $$message would be set to "Hello World."
56. What is GET and POST method in PHP?
GET, and POST are two of the most basic methods used in web communication between a server and a client. GET is a type of request that asks a server to send back a resource, while POST is a type of request that sends data to a server.
57. What is the difference between GET and POST methods?
The GET and POST methods are two distinct ways of sending information to the server. The GET method is used when requesting data, while the POST method is used to submit data such as form information.
58. What is the use of callback in PHP?
A callback is a PHP function used to execute other parts, and it is often used to add custom functionality to the code or to provide an alternative way to execute code.
59. What is a lambda function in PHP?
Lambda functions in PHP are special functions that can perform specific tasks. These functions are anonymous, meaning they are declared and used without a name.
60. What are PHP Magic Methods/Functions?
PHP Magic Methods are powerful functions that perform specific tasks within a program. These functions are unique as they are part of the core language and can be used to create custom functions that allow for complex operations.
What's Next?
These were some of the common PHP interview questions that hiring companies are most likely to ask during their interview process. We hope these PHP interview questions for freshers as well as professionals will help you regardless of whether you’re about to start your PHP career or are ready to take it to the next level.
If you’re serious about a PHP development career and want to answer your PHP interview questions more confidently, you’d want to master this language and get more in-depth answers to these questions. As such, getting an industry-standard Post Graduate Program in Full Stack Web Development course certification will steer you in the right direction.
Connect with me
LinkedIn : https://www.linkedin.com/in/durgesh4993/
GitHub : https://github.com/Durgesh4993
LeetCode : https://leetcode.com/durgesh4993/
Top comments (5)
I guess recruiters stopped updating their php knowledge in 2020, see question 4.
Question 7 got me nostalgic. Where is the time I wrote php 4 code. What a wild time that was.
Please write a better post with more relevant information. And leave out the parts you shouldn't have copied (29, 39, 40, 51)
"54. What is overloading and overriding in PHP?"
From what I know, overloading in PHP is different than the one in other OOP languages like C++, Java and C#.
In PHP, it is used for dynamically creating properties and methods. These dynamic entities are processed via magic methods such as
__set(), __get()
for properties and__call(), __callStatic()
for methods.Btw, no mention about class autoloading, esp with
spl_autoload_register()
function ?A lot of these (I got through 1, 2, 4, 5, and 6 before stopping counting), nobody needs to know - it's just not useful information or relevant to a job.
If someone asked me them in an interview, I'd ask them why they wanted to know, and when was the last time they used that information in the real world.
I rarely ever conduct interviews, but if I ever ask you one of these questions, esp. the first half -> throw something at me xD
Ofc different requirements might require different approaches. But I assume we are talking about a junior role / candidates first job maybe.
I'm usually more curious about where they are in their journey e.g.
And I just go from there to get an idea about a candidate and the way they approach a given problem etc.
In other words, I want to figure out if they are 1) a fit for the teams 2) can grow into the job and how many weeks/months it'll roughly take for them to be productive and contribute in a meaningful manner.
This is a joke right? If those questions are asked and used to determine qualities of a candidate - run!