PHP Interview Questions and Answers

PHP interview questions

These PHP questions have been designed for various interviews, competitive exams and entrance tests. We have covered questions on both basic and advanced concepts which will help you improve your skills to face interview questions on PHP.

Who is this PHP interview questions designed for?

All the Web developers, Front End developers, UI/ UX developers and designers will find these questions extremely useful. All freshers, BCA, BE, BTech, MCA and college students wanting to make a career in front end designing will be highly benefitted by these questions.

PHP interview questions topics

This section covers PHP topics like - PEAR in PHP, PHP script, output buffering in PHP, PHP Session, PHP session and cookie, errors in PHP etc.

1. What is PHP? Why PHP is also called as Scripting language?

PHP stands for Hypertext Preprocessor. It is open source server-side scripting language which is widely used for web development. It is an interpreted language, i.e., there is no need for compilation. It is faster than other scripting languages, for example, ASP and JSP.

PHP is also called as Scripting language because it is an interpreter based language and not a compiler based. This simply means that PHP doesn’t have to be compiled before interpreted. The interpreter executes the instructions directly without first converting them into machine instructions. This is different from programming languages as they first have to be compiled into machine language before being interpreted.

Video : PHP Interview Questions and Answers

2. Why use PHP

PHP is a server-side scripting language, which is used to develop Static as well as Dynamic websites.

PHP is very popular and widely used for several reasons:

  • PHP is an open source language that makes it the most cost-effective method of creating robust websites
  • PHP boasts simplicity, creating a code in PHP is simple.
  • PHP allows its code to be easily embedded into HTML language.
  • PHP has a huge community. If any problem related to your project arises, you can easily ask it in online communities or PHP forums.
  • PHP is cross platform; this means you can deploy your application on a number of different operating systems such as windows, Linux, Mac OS etc.
  • PHP supports several protocols such as HTTP, POP3, SNMP, LDAP, IMAP, and many more.
  • PHP works exceptionally well with CMS like WordPress, Drupal, Joomla etc.
  • PHP has a Huge Standard Libraries that play a crucial role in simplifying and speeding the data processing ability
  • PHP delivers exceptional performance as it has faster data processing features.
  • Most web hosting servers support PHP by default

3. What is PEAR in PHP?

PEAR is a framework and repository for reusable PHP components. PEAR stands for PHP Extension and Application Repository. It contains all types of PHP code snippets and libraries. It also provides a command line interface to install “packages” automatically.

4. How can we increase the execution time of a PHP script?

By default, the maximum execution time for PHP scripts is set to 30 seconds. If a script takes more than 30 seconds, PHP stops the script and returns an error.

This time is set in the php.ini file. This time can be increased by modifying the max_execution_time value defined in the php.ini. The time must be changed keeping the environment of the server. This is because modifying the execution time will affect all the websites hosted by the server.

5. Explain the purpose of output buffering in PHP.

Output buffering is a way to tell PHP engine to hold some data before it is sent to the browser.

PHP sent the output data to the browser in pieces. With output buffering, the output data is stored in a variable and sent to the browser as one piece at the end of the script. Hence it decreases the execution time of PHP script.

6. What is a PHP Session?

PHP Session is a way to preserve data across subsequent HTTP requests. Session stores user information to be used across multiple pages. By default, session variables last until the user closes the browser.

7. What is PHP session_start() and session_destroy() function?

PHP session_start() creates a session or resumes the current one. It returns the current session if the session is already created. If the session is not available, it creates and returns new sessions.

PHP session_destroy() function is used to destroy all session variables completely. This function does not need any argument and a single call can destroy all the session variables.

8. What is the difference between session and cookie?

Both Sessions and cookies are the global storage used to store data to be persistently available all over the website.

The difference between session and cookie are as follows:

A Session resides on the Web Server. A Cookie resides on the User's Computer.

The session values are automatically deleted when the browser is closed. You can manually set an expiry for a cookie. If it is not set, then the cookie expires when the browser is closed.

Sessions have the capacity to store relatively large data compared to cookies.

9. What is the difference between $message and $$message?

Both $message and $$message are variables.

$message is a simple variable whereas $$message is a reference variable.

$message is used to store variable data. $$message can be used to store variable of a variable.

Data stored in $message is fixed while data stored in $$message can be changed dynamically.

10. What are the different types of errors in PHP?

Notices:

These are small, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all, although the default behavior can be changed.

Warnings:

Warnings are more severe errors like attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.

Fatal errors:

These are critical errors - for example, instantiating an object of a non-existent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.

11. What are include() and require() functions?

Both Include() and Require() function can be used to include one PHP file into another PHP file.

The include and require statements are identical, except upon failure:

  • include() function produces a warning but does not stop the execution of the script.
  • require() function produces a warning and a fatal error and stops the script.

12. How will you generate random numbers using PHP?

The PHP rand() function is used to generate a random number. If you want a random integer in some range, we can use

rand(min,max)

This function will generate a random value in the range [min,max]