Top 10 PHP Interview Questions and Answers That You Should Know

Do you want to learn or test your PHP skills for an interview, or have a PHP interview coming up?

Here in this article with a few of the best developers within our network share their top PHP interview questions, answers, and interview tips to help test a developer’s PHP knowledge and expertise.

Q. #1 What’s difference between require(), require_once(), and include()?



require() is used to include and evaluate a specific file. If the file is not found, it gives a Fatal Error.

require_once() only includes the file which is being included once. It is recommended for the files where you have lots of functions stored.

include() is used to include the file, and if the file is not found, it gives a warning to the user.

Q. #2 What’s the difference between unset() and unlink()

unset() sets a variable to “undefined” while
unlink() deletes a file we pass to it from the file system.

Q. #3 What are the main error types in PHP and how do they differ?

Notices – Simple, non-critical errors that are occurred during the script execution. An example of a Notice would be accessing an undefined variable.

Warnings – more important errors than Notices, however the scripts continue the execution. An example would be include() a file that does not exist.

Fatal – this type of error causes a termination of the script execution when it occurs. An example of a Fatal error would be accessing a property of a non-existent object or require() a non-existent file.

Q. #4 What is the difference between GET and POST?

In PHP there are three main type of errors:GET displays the submitted data as part of the URL, during POST this information is not shown as it’s encoded in the request.
GET can handle a maximum of 2048 characters, POST has no such restrictions.
GET allows only ASCII data, POST has no restrictions, binary data are also allowed.
Normally GET is used to retrieve data while POST to insert and update.

Q. #5 How can you enable error reporting in PHP?

Check if “display_errors” is equal “on” in the php.ini or declare “ini_set(‘display_errors’, 1)” in your script.
Then, include “error_reporting(E_ALL)” in your code to display all types of error messages during the script execution.

Q. #6 What are Traits?

Traits are a mechanism that allows you to create reusable code in languages like PHP where multiple inheritance is not supported. A Trait cannot be instantiated on its own.

Q. #7 What are the differences between session and cookie?

The session is a global variable which is used in the server to store the session data. When a new session creates the cookie with the session id is stored on the visitor’s computer. The session variable can store more data than the cookie variable.

Session data are stored in a $_SESSION array and Cookie data are stored in a $_COOKIE array. Session values are removed automatically when the visitor closes the browser and cookie values are not removed automatically.

Q. #8 What is php.ini & .htacess file?

Both are used to make the changes to your PHP setting. These are explained below:

php.ini: It is a special file in PHP where you make changes to your PHP settings. It works when you run PHP as CGI. It depends on you whether you want to use the default settings or changes the setting by editing a php.ini file or, making a new text file and save it as php.ini.

.htaccess: It is a special file that you can use to manage/change the behavior of your site. It works when PHP is installed as an Apache module. These changes include such as redirecting your domain’s page to https or www, directing all users to one page, etc.

Q. #9 What is difference Between PHP 5 and 7?

There are many differences between PHP 5 and 7. Some of the main points are:

Performance: it is obvious that later versions are always better than the previous versions if they are stable. So, if you execute code in both versions, you will find the performance of PHP 7 is better than PHP5. This is because PHP 5 use Zend II and PHP & uses the latest model PHP-NG or Next Generation.

Return Type: In PHP 5, the programmer is not allowed to define the type of return value of a function which is the major drawback. But in PHP 7, this limitation is overcome and a programmer is allowed to define the return type of any function.

Error handling: In PHP 5, there is high difficulty to manage fatal errors but in PHP 7, some major errors are replaced by exceptions which can be managed effortlessly. In PHP 7, the new engine Exception Objects has been introduced.

64-bit support: PHP 5 doesn’t support 64-bit integer while PHP 7 supports native 64-bit integers as well as large files.

Anonymous Class: Anonymous class is not present n PHP 5 but present in PHP 7. It is basically used when you need to execute the class only once to increase the execution time.

New Operators: Some new operators have added in PHP 7 such as <=> which is called a three-way comparison operator. Another operator has added is a null coalescing operator which symbol as?? and use to find whether something exists or not.

Group Use declaration: PHP 7 includes this feature whereas PHP 5 doesn’t have this feature.

Q. #10 What is Cross -site scripting?

Cross-site scripting (XSS) is a type of computer security vulnerability typically found in web applications.

XSS enables attackers to inject client-side script into web pages viewed by other users.

A cross-site scripting vulnerability may be used by attackers to bypass access controls such as the same-origin policy.



Leave a comment

Your email address will not be published. Required fields are marked *