30 LAMP(Linux, Apache, MySQL & PHP) Interview Questions and Answers

Welcome back Techies!!! We are providing you with some interview questions and answers that has been asked in most of the interviews about the LAMP environment. People applying for a PHP Programmer job should definitely go through these interview questions, as these are designed specially to get a basic idea of how questions are asked in the interviews these days. This list has been compiled after several requests from our readers to compile a set of questions combining all aspects of LAMP so it would be useful for many people.

Q:1 Please state how to submit a form without using a Submit button ?

Ans: We can submit a form without using a submit button by having a JavaScript code linked to any event trigger of a form field. And just add the code document.form.submit() function to submit the form when the event is triggered.

Q:2 State the main difference between mysql_fetch_array and mysql_fetch_object ?

Ans: Mysql_fetch_array will fetch all the matching records, whereas mysql_fetch_object will only fetch the first record that matches the query.

Q:3 State the main difference between $message and $$message ?

Ans: $message is a name of a variable, whereas $$message is a variable with its name stored inside $message.

For example if $message=”var”, then $$message is the same as $var

Q:4 State the main difference between require and include, include_once ?

Ans: The main difference is that when using require, it will throw a fatal error when a file is not found, whereas include and include_once will show a warning and continue to load the page.

Q:5 What is the difference between the functions unlink and unset?

Ans: The Unlink() function deletes the file whereas Unset() makes a set variable as undefined.

Q:6 How you will define a Session ?

Ans: A Session is a method to store some data to be used across multiple pages. In technical terms it is a logical object that is stored in the server to help you store data and can be accessed across multiple HTTP requests. Session is always temporary based on the session timeout set in your Apache Server.

Q:7 How do you register the variables into a session ?

Ans: To register variables in a session, you need to use the session_register() function

Ex: session_register($login_id)

Q:8 How you will find the number of elements present in an array ?

Ans: To find the no. of elements in an array, you can either use count() or sizeof() function

Ex:  count($array) or sizeof($array).

Q:9 Can you encrypt your password in PHP and how to do it ?

Ans: Yes, you can encrypt passwords and all kinds of data in PHP using md5() or sha() functions.

Q:10 What is a trigger and does MySQL support triggers ?

Ans: A trigger is a database object that is associated with a particular table in a database. It gets activated automatically and performs when either INSERT, UPDATE, DELETE action occurs on the table.

MySQL supports triggers from MySQL 5.0.2 version.

Q:11 State the main difference between mysql_connect and mysql_pconnect ?

Ans: With mysql_connect, you open a database connection each time when the page loads, whereas with mysql_pconnect, connection gets established only once and provides access to the database across multiple requests.

Q:12 How to repair a table in MySQL ?

Ans: To repair a table in MySQL you need to use the following query:

REPAIR TABLE {table name}

MySQL will do a repair of only the index tree, If QUICK is given

MySQL will create index row by row, If EXTENDED is given.

Q:13 Is PHP a case sensitive programming language ?

Ans: It is partially case sensitive, where we can use function and class names in case sensitive manner but variables need to be used in a case sensitive manner.

Q:14 How can one handle loops in PHP ?

Ans: In PHP, you the looping statements like while, do while, for and for each.

Q:15 Can you execute a PHP script in command line ?

Ans: Yes, we can execute a PHP script in command line with the following command line argument

# php yourscript.php

Where php is the command to execute the php script in a Command Line Interface (CLI)

Q:16 What is nl2br() ?

Ans: nl2br() function inserts HTML line breaks before each newline in a string.

For example nl2br(“How are you”) will return strings added with HTML line breaks before all new lines in a string, and the output will be like:




Q:17 How can we encrypt and decrypt a data present in a mysql table using mysql ?

Ans: To encrypt data in a mysql table, you can use the following: AES_ENCRYPT () and AES_DECRYPT ()

Q:18 What are the types of errors in PHP and explain each one of them ?

Ans: The types of errors in PHP are Notices, Warnings & Fatal Errors.

Notices are less important errors that you don’t want to give much importance to it. Like errors that occur, when you try to access a variable that is not defined. If you change the notice errors to be not displayed, you won’t see these kinds of errors at all.

Warnings are errors of some serious nature that demand your attention. Even though these errors are displayed to the user, the script will not terminated. Example of this error includes accessing a file that doesn’t exist.

Fatal Errors are mission critical errors that result in immediate termination of your script. Examples of these errors include, calling an object of a non-existent class etc.

Q:19 What is htmlentities and what is their functionality ?

Ans: Htmlentities() just converts the characters into HTML entities.

Q:20 What is urlencode() and urldecode() ?

Ans: urlencode() converts special characters into characters that are safe to be used in URL’s. Mostly they are converted into % signs along with 2 hex digits.

For ex: urlencode(“20:00%) is converted into “25%2E00%25?”

urldecode() does the opposite and returns the decoded string..

Q:21 What php image functions do you use to get the properties of an image ?

Ans: There are various php images functions that deals with images and you can use:

  • exif_imagetype() – To get the type of the image
  • getimagesize() – To get the size of the image
  • imagesx() – To get the width of the image
  • imagesy() – To get the height of the image
Q:22 Can you increase the execution time of a php script ?

Ans: Yes, we can use the max_execution_time variable to set the desired time you needed for executing a php script.

Q:23 Can you increase the maximum upload size in PHP ?

Ans: Yes, we can use the upload_max_filesize variable to change the maximum size of a file you can upload.

Q:24 Please state how can you take a backup of  the whole database in mysql ?

Ans: You can use the command line utility to take a backup of all the mysql table or a specific mysql table easily with the following:

mysqldump –-user [user_name] –-password=[password] [database_name] > [dump_file_name]
Q:25 How to destroy a session variable ?

Ans: Session_unregister() Unregister a global variable from the current session

Q:26 How can we unset the variable of a session ?

Ans: With the session_unset($variable_name) function, one can clear the session variable.

Q:27 How to destroy a cookie ?

Ans: You just need to set the cookie to a previous date or time.

Q:28 Please explain what is wrong with this query “Select * from table_name” ?

Ans: You should never select all columns of a table unless needed and specify the columns only required in the query. The reason is that it will use a lot of memory to fetch the data, if the records are huge, when you are going to use only 2 or 3 fields from the table.

Q:29 What is SQL Injection and how do you deal with that ?

Ans: SQL injection is a technique utilized by hackers to get access into your database by using malicious SQL statements. Using this, anyone can gain complete access to your database without any authorization or permission.

To start with one need to use mysql_real_escape_string() to filter the user input data, before passing onto the sql statement.

Q:30 Please explain the output of the code provided below and explain the reasoning ?

$a =  012; echo $a / 4;

Ans: The answer is 2.5.

In PHP, whenever a number is prefixed with 0, it will be considered as an octal number, and hence the 012 octal number is equivalent to the decimal number 10, and so 10/4 is 2.5

Leave a Reply

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