PHP MySQL Interview Questions

I’ve been interviewed by many companies for PHP development positions, every company has a slightly different interview process, but there seems to be a lot of commonalities among them. I’ve outlined below the main areas that companies look for knowledge in, and some questions/answers you may get asked. At the end of the day you will not be able to entirely fake your way through a good interview, but I thought this would be a huge help for newer or mid level developers going into an interview. So given below are some PHP MySQL Interview Questions to help you out there.

Q1. What’s PHP ?

The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

Q2. What’s a Session ?

A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.

There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.

Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

Q3. What is a Cookie ?

A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user’s computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP, you can both create and retrieve cookie values.

Cookie is of two types 1) Persistent Cookie 2) Temporary Cookie

The setcookie() function is used to set a cookie.

setcookie(“user”, “Dipak”, time()+3600);

Here this cookie specifies that a cookie has been created which will be expired in next one hour holds the record that the current user is “Dipak”.

We can destroy the cookie by specifying expiry date less than the current time.

Q4. What is meant by PEAR in php ?

PEAR is short for “PHP Extension and Application Repository”. The purpose of PEAR is to provide:

  • A structured library of open-sourced code for PHP users
  • A system for code distribution and package maintenance
  • A standard style for code written in PHP

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

$message is a simple variable whereas $$message is a reference variable. Here $message is a variable with a fixed name. $$message is a variable who’s name is stored in $message.
For example, if $message contains “var”, $$message is the same as $var.

Q6. What are the differences between require and include, include_once and require_once ?

The include() statement includes and evaluates the specified file. The documentation also applies to require(). The two functions are identical in every way except how they handle failure.  include() produces a Warning while require() results in a Fatal Error.

The include_once() statement includes and evaluates the specified file during the execution of the script. This is a behavior similar to the include() statement, with the only difference being that if the code from a file has already been included, it will not be included again. As the name suggests, it will be included just once.

require_once() should be used in cases where the same file might be included and evaluated more than once during a particular execution of a script, and you want to be sure that it is included exactly once to avoid problems with function redefinition, variable value reassignments, etc.

Q7. What are the differences between public, private, protected, static, transient, final and volatile ?

Public: Public declared items can be accessed everywhere.

Protected: Protected limits access to inherited and parent classes.

Private: Private limits visibility only to the class that defines the item.

Static: A static variable exists only in a local function scope, but it does not lose its value when program execution leaves this scope.

Final: Final keyword prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.

Transient: A transient variable is a variable that may not be serialized.

Volatile: A variable that might be concurrently modified by multiple threads should be declared volatile. Variables declared to be volatile will not be optimized by the compiler because their value can change at any time.

Q8. What is the difference between the functions unlink and unset ?

unlink() deletes the given file from the file system.
unset() makes a variable undefined.

Q9. What is meant by urlencode and urldecode ?

urlencode returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type.

urldecode decodes any %## encoded in the given string.

Q10. How can we submit a form without a submit button ?

Javascript submit() function is used to submit a form by using the form name.

By a OnClick callback function we can use “document.formname.submit();”.

We can do this by using ID also like, “document.getElementById(“FormID”).submit();”.

We can also use jQuery like, “$(“FormID”).submit();”.

Q11. What are the differences between GET and POST methods in form submitting ?

On the server side, the main difference between GET and POST is where the submitted is stored. The $_GET array stores data submitted by the GET method. The $_POST array stores data submitted by the POST method.

On the browser side, the difference is that data submitted by the GET method will be displayed in the browser’s address field. Data submitted by the POST method will not be displayed anywhere on the browser.

GET method is mostly used for submitting a small amount and less sensitive data. POST method is mostly used for submitting a large amount or sensitive data.

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

Here are three basic types of runtime errors in PHP:

  1. Notices: These are trivial, 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 you can change this default behavior.
  2. Warnings: These are more serious errors – for example, 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.
  3. 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.

Internally, these variations are represented by twelve different error types,

  • E_ERROR: A fatal error that causes script termination
  • E_WARNING: Run-time warning that does not cause script termination
  • E_PARSE: Compile time parse error.
  • E_NOTICE: Run time notice caused due to error in code
  • E_CORE_ERROR: Fatal errors that occur during PHP’s initial startup (installation)
  • E_CORE_WARNING: Warnings that occur during PHP’s initial startup
  • E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
  • E_USER_ERROR: User-generated error message.
  • E_USER_WARNING: User-generated warning message.
  • E_USER_NOTICE: User-generated notice message.
  • E_STRICT: Run-time notices.
  • E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error
  • E_ALL: Catches all errors and warnings

Q13. What is the difference between session and cookies ?

  • Session are temporary and Cookies are parmanent.
  • Session data is store on server while Cookies are store on user’s computer.
  • Cookies contents can be easily modify but to modify Session contents is very hard.
  • Cookies could be save for future reference but Session couldn’t when user close the browser Session data also lost.

Q14. What is the difference between array_merge and array_combine ?

array_merge merges the elements of one or more than one array such that the value of one array appended at the end of first array. If the arrays have same strings  key  then the later value overrides the previous value for that key.

array_combine creates a new array by using the key of one array as keys and using the value of other array as values.

Q15. What is difference between echo() and print() ?

echo() and print() function both are used to show the output on the visitors screen but in echo we can take one or more parameters.

print() has a return value of true or false whereas echo has a void return type.

echo() is slightly faster than print.

Q16. What is meant by MIME ?

WWW ability to recognize and handle files of different types is largely dependent on the use of the MIME (Multipurpose Internet Mail Extensions) standard. The standard provides for a system of registration of file types with information about the applications needed to process them. This information is incorporated into Web server and browser software, and enables the automatic recognition and display of registered file types.

Q17. What are the different types of MySQL Database Engines ?

Total 5 types of tables we can create

  1. FEDERATED
  2. MRG_MYISAM
  3. MyISAM
  4. BLACKHOLE
  5. CSV
  6. MEMORY
  7. ARCHIVE
  8. InnoDB
  9. PERFORMANCE_SCHEMA

Q18. What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row() ?

mysql_fetch_array() : Fetch a result row as an associative array, a numeric array, or both.

mysql_fetch_object() : Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead. Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows

mysql_fetch_row() : Fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

Q19. What is difference between mysql_connect and mysql_pconnect ?

mysql_connect opens up a database connection every time a page is loaded. mysql_pconnect opens up a connection, and keeps it open across multiple requests.

mysql_pconnect uses less resources, because it does not need to establish a database connection every time a page is loaded.

Q20. What is JOIN in MySQL and explain different types of JOIN ?

JOIN is used to select relevant data between one or more tables.

There are three types of join in sql. that are as following:-
1) Inner join
1.1) equi join
1.2) natural join
2) Outer join
2.1)left join
2.2)right join
2.3) full join
3) Cross join

Q21. Why do we use ob_start() ?

ob_start() used to active the output buffering .When output buffering is on all output of the page sent at one time to the browser ,otherwise sometimes we face headers already sent type errors.

Q22. What is difference between single quotes and double quotes in php ?

When string is in single quotes php will not evaluate it . If there is a string with single quotes and if we place a variable in that it would not substitute its value in string where as double quotes support variable expansion. If we place variable in double quotes it would substitute its value in string.

Q23. What is a .htacces file ?

.htaccess is a configuration file running on Apache server.These .htaccess file used to change the functionality and features of apache web server .

  • .htaccess file used for url rewrite .
  • .htaccess file used to make the site password protected.
  • .htaccess file can restrict some ip addresses ,so that on restricted ip adresses site will not open.

Q24. What is difference between PRIMARY key and UNIQUE Key in sql ?

UNIQUE key can be a NULL value but PRIMARY key can not take NULL values.

A table can have multiple UNIQUE key but can only one PRIMARY key.

Q25. What is difference between TRUNCATE and DELETE in mysql ?

  1. DELETE is a DML(data manipulation lnguage) command where as truncate is a DDL(data definition language) command.
  2. Truncate is much faster than Delete.
  3. We can not roll back in truncate but in delete we can rollback.
  4. We can not use where clause in truncate but in delete we can use conditions using where clause.

Q26. What is use of header() function in php ? What the Limitation of header() ?

There are Number of use of header() function in php like below,

  • The header() function use to sends a raw HTTP header to a client.
  • We can use header() function for redirection of pages.
  • Use for refresh the page on given time interval automatically.
  • To send email header content like cc, bcc , reply to etc data and lot more.

In PHP, it is important to notice the limitation of header() function is that header() must be called before any actual output is send. Means must use header function before HTML or any echo statement.

Q27. What are the differences between procedure oriented languages and object oriented languages ?

There are lot of difference between procedure language and object oriented like below,

  1. Procedure language easy for new developer but complex to understand whole software as compare to object oriented model.
  2. In Procedure language it is difficult to use design pattern mvc, Singleton pattern etc but in OOP you we able to develop design pattern.
  3. IN OOP language we able to ree use code like Inheritance, polymorphism etc but this type of thing not available in procedure language on that our Fonda use COPY and PASTE.