Being from a IT background myself, I have always been keen to learn about PHP development. I know you do too, that is why you are probably reading my blog!
So, collected from my personal experience of attending ample number of PHP interviews, I have devised a list of PHP Interview Questions including almost every question I have faced while my interview session.
I will also help you with properly framed answers to each question to improve the way you are preparing for your interview.
Advanced PHP Interview Questions Answers List
1. What does the initials of PHP Stands for?
PHP means PHP: Hypertext PreProcessor that previously stood for personal home pages.
2. Who is the father of PHP?
3. The basic question that can comes under both simple to advanced php interview questions is "what do you mean by PHP"?PHP
is an open source server-side scripting language that allows creating web pages dynamically and is used for web applications and can be embedded into HTML code.
To access PHP Scripts, only web browser is required by client computer. PHP can also be used in combination with various web content management systems, web template systems, and web frameworks.
4. Which programming language does PHP resemble to?
PHP syntax bears resemblance with PERL and C.
5. What PHP version is extensively used currently ?
Version 7.1 / 7.2
6. What do you mean you say PHP is Open-Source?
Open-Source Software refers to the software that is dispersed with source code, can be freely used, read, modified and shared by anyone.
PHP is an Open-Source Software (OSS) as it meets the following benchmarks:
a) Source code of PHP is included with the program and is distributed freely.
b) Modified versions of source code for PHP are also redistributed.
c) Users are able to modify the distributed PHP source code.
7. How can we submit a form without a submit button?
8. How can we create a database using PHP and MySQL?
We require a database before saving or accessing any data and to create any database in MySQL, CREATE DATABASE Statement is used.
e.g. mysql_create_db("Database Name")
9. How to include a file to a PHP page?
It is possible to include one PHP file's content to other PHP file. The two functions "include()" and "require()" with file path as its parameter can be used to include a file.
10. What is role of Ksort() in PHP?
Ksort() is an inbuilt PHP function which is used to sort an associative array key in an ascending order of its key values. For example:
$age=array("Jai"=>"59", "Dolly"=>"43", "Mack"=>"49", "Sam"=>"46");
foreach ($age as $a => $b)
echo "$a = $b\n";
Dolly = 43 Jai = 59 Mack = 49 Sam = 46
11. How to get the value of current session id?
session_id() function can be used to return the session id.
12. What are the functions of header() function in PHP?
This function is used for sending raw HTTP header to the client, and redirection of pages. It is very important to remark, that header() must be called before real output is seen, either by blank lines in a file, or from PHP or HTML tags.
13. Are multiple inheritances supported in PHP?
PHP includes only single inheritance. A Class can be extended from only one class and keyword used is 'extended'.
14. How can we execute a PHP script using command line?
We just need to run the PHP command line interface program and add the PHP Script file as the Command line argument
Following are the four steps and paths that can be used to execute PHP in command line.
1. First login to your windows system.
2. Press windows and R key of keyboard simultaneously to open command prompt.
3. To find PHP executable file, move across PHP directory.
4. Executable file is accessed and then Path to PHP File.
The path with Change directory command used is C:\wamp\bin\php\php5.3.13. For Wamp Webserver users: php.exe path-to-your-php-file. If you visualize PHP version info, you can use PHP path-to-your-php-file instead of that command.
15. How can PHP and HTML Interact?
It is possible to generate HTML through PHP scripts and it is possible to pass information's from HTML to PHP.
16. What are final method and a final class?
Final is introduced in PHP5. Final methods cannot be over-ride and final class cannot be extended.
17. What is the difference between PHP4 and PHP5?
- Zend Engine II is used in while Zend engine 1 was is used in PHP4.
- The addslashes() function is used in PHP5 instead of Magic quotes used in PHP4.
- The more procedure ordinate language in PHP4 is replaced with additional object oriented concepts in PHP5.
- Abstract classes can be declared in PHP5 which was not possible in PHP4.
- Static methods and properties and exceptions are introduced in PHP5 which were not present in PHP4.
- There are three level of visibility are present in PHP5 i.e. public protected and private.
In PHP4, all objects were passed by value but in PHP5, all objects are passed by reference and more reserved keywords are used in PHP5
18. How can we directly display output to the browser?
Special tags <? = and?> are used to display directly to browser.
19. How comparisons of objects are done in PHP5?
The operator '==' is used to test whether two objects are instanced from the same class, having equal values and same attributes. We can test if two objects are referring to the same instance of the same class by the use of the identity operator '==='.
20. What type of operations is needed when values are passes through a URL or a form?
We require them to encode and decode with two functions htmlspecialchars() and urlencode.
21. How the result sets of MySQL is handled in PHP?
These can be handled with the use of mysql_fetch_assoc, mysql_fetch_array, mysql_fetch_row or mysql_fetch_object.
22. What is the function mysql_pconnect() used for?
mysql_pconnect() assures a continual connection to the database. It means that even PHP script ends, the connection does not close.
23. Explain the different errors occur in PHP?
There are mainly four types of error found in PHP
Fatal Error: Fatal errors are known as critical runtime errors which occur when we try to access inaccessible data. When this error occurs; it results in immediate script termination.
e.g. trying to access the undeclared function and dead objects.
Parse Error: These types of errors are found when there are syntax faults in code. When this error occurs, it stops scripts execution.
E.g. if there are mismatch brackets or semicolon missing.
Warning Error: These errors will not stop script execution but give the warning and continue with further line of the script. Warning errors occurs when we try to include the missing files or when we try to use the wrong number of parameters in a function.
Notice Error: These errors resemble with warning errors i.e. execution does not halt script. It also gives the notice and continues with further line of the script.
25. What is the difference between $$name and $name?
$$name is a reference variable whereas $name is variable.
E.g. $name=kelvi and $$name=joy so $kelvi value is joy
26. What is PEAR in PHP?
PEAR stands for PHP extension and application repository which is a framework consisting of collections of PHP reusable components, libraries and codes snippets. It is used to ease the life of developer and make it more comfortable and secure.
PEAR also provides a command-line interface to automatically install packages.
27. What is the name of scripting engine in PHP?
28. What are the various methods used in form submission of PHP?
Two methods i.e. get and post
29. How can we get the browser properties using PHP?
We can use the following simple code to get the browser properties:
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser();
30. What is a session in PHP?
A session is an alternate way to make stored information accessible across the multiplied pages. The function used to create a session is session_start(). A file is created in temporary directory by session on the server, which consists of variables and their values. This information will be made available to various pages on the site.
When we create any working environment, we open an application and do some modifications and after that terminate that application. This is more similar to session.
Our computer knows all the logging information about us but when we are accessing internet, there one problem occurs: web server does not have information. This problem is solved by session variables. Session helps to make customized web application where user tracking is required.
31. How we can destroy any session?
We can use the destroy function described in following syntax.
32. What is the difference between unset and unlink function?
When we want to destroy any variable, we can use the unset() function and when we want to destroy a file, unlink() function is used.
33. How we can get the current session ID?
For this, we have to go to session start and then go to echo session id
34. How we can differentiate between explode and split functions?
Both explode and split functions are used to split a string in array and the common difference is that while explode uses a string and split uses a pattern.
Explode is faster than split because it does not perform regular expression based matching, so regex parser is not required to analyze the string. Split() has been deprecated as of PHP5.3.0 and use of this split function is pessimistic.
35. What is the use of PDO classes?
PDO stand for PHP Data Object. PDO contains collections of PHP extensions that provide database precise drivers and core PDO classes. It also targets data access abstraction.
To be compatible with latest versions of PHP, the advanced OOPS feature is required. It grants a lightweight data-access abstraction layer persistent interface to access databases in PHP.
36. What is the use of count() function in PHP?
It is used to count the properties and elements of an object recursively
Where array name specifies countable object and mode sets the function's mode.
37. During PHP installations which function creates an HTML page to display records of how PHP was installed?
38. What is the difference between print and echo in PHP?
Both print and echo are used to display output data on a screen and there are very minor differences are described below:
Print can be used in expressions and has a return value of 1 whereas echo has no return value. Print can take only one parameter whereas echo can take multiple. Print is slower than echo.
39. What is the difference between session and cookie?
|Session is more secure||Cookie is less secure because cookie's values can be easily accessed|
|Session is stored on server side||Cookie is secured on client's computer|
|Session can hold multiple variables||Multiple variable are not held in cookies|
|We can store unlimited data||We can store data in limits|
|The session_start() function must be used before the HTML tag||The setcookie() function must be used before the HTML tag|
40. What is the purpose of php.ini file?
When we install and explore PHP, we will find one configuration file where we can visualize, access and modify the functionality of PHP. This file is the current and final way to affect functionality of PHP in which we can declare changes to PHP settings by editing the existing php.ini file. The php.ini file is read each time PHP is initialized.
We can create a new php.ini file in cPanel in the following steps:
1. First we have to log in to the cPanel.
2. Click on File manager icon in the Files category.
3. Click Go after selecting Home Directory.
4. On top of the page, click the new file button and enter php.ini in the new file name field.
5. Click the file named php.ini.
6. Select and click the edit button.
41. What is escaping to PHP?
To differentiate PHP code from other elements, the PHP parsing engine is required. This mechanism is known as escaping to PHP
42. Is it possible to create a class in PHP?
43. How will you retrieve source line with the use of exception class in PHP after an error has occurred?
Getline method() can be used to return source line.
44. PHP is a case sensitive language?
Yes, PHP is a case sensitive except user defined functions
45. What is the objective of getdate() function?
To return the current time or date information or timestamp, the getdate() function is used.
Syntax used is getdate(timestamp);
Where timestamp is optional and specifies an integer Unix timestamp.
46. Explain PHP variables.
Variables can be used to store data like numbers, string of text etc. Once a variable is declared, it can be reused all over the code and the assignment operator is used to assign the value to the variables.
$var_name = value;
Variable names used in PHP are case sensitive.
47. How to redirect a page in PHP?
48. What is the difference between POST and GET methods in PHP?
The browser client can send information to web server in two ways i.e. the GET method and POST method.
|POST Method||GET Method|
This method can transfer a large amount of data.
Get method can send limited amount of date i.e.1024 bytes.
|More Secure.||Less Secure.|
POST is used for writing data.
GET is used for retrieving data.
Data is not visible to user.
Data is visible to user.
We can make bookmark by using POST.
We cannot make a bookmark.
In POST, all data control travel through URL.
|All data travel through URL as well.|
49. Name PHP supported database?
There are many PHP supported databases available for e.g.
e) SQL server
50. What the difference is between include and require in PHP?
If we include a file which does not exists, it will simply throw an error and rest of the script will run.
|If user require and that file does not exists then it will throw an error and halt the script from further execution|
If file is not important to include then we should use include so that script should continue even when file is not found
If file is important then we should use require so that it should stop to execute when file is not found
These are the list of top 50 PHP interview questions extracted from important concepts of PHP. Students are recommended to go through these questions for revision and to learn PHP online along with examples; we have prepared PHP tutorials for the beginners.