PHP Questions for Interview with Answers
1. What are the differences between GET and POST methods in form submitting, give the case where we can use GET and we can use POST methods?
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.
2. Who is the father of PHP and explain the changes in php versions?
Rasmus Lerdorf is the father of PHP. For version changes go to http://php.net
3. How can we submit from without a submit button?
4. How many ways we can retrieve the date in result set of MySQL using PHP?
As individual objects so single record or as a set or arrays.
5. What is the difference between mysql_fetch_object and mysql_fetch_array?
mysql_fetch_object will collect first single matching record where as mysql_fetch_array will collect all matching records from the table in an array.
6. What is the difference between $message and $$message?
They are both variables. But $message is a variable with a fixed name. $$message is a variable whose name is stored in $message. For example, if $message contains “var”, $$message is the same as $var.
7. How can we extract string ‘abc.com’ from a string ‘http://email@example.com’ using regular _expression of php?
We can use the preg_match() function with “/.*@(.*)$/” as the regular expression pattern. For example:
8. How can we create a database using PHP and MySQL?
In PHP, we can use mysql_create_db() function to create a database. In MySQL, we can use CREATE DATABASE command.
9. What are the differences between require and include, include_once?
File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once(). This will prevent problems with function redefinitions, variable value reassignments, etc.
10. Can we use include(’abc.php’) two-times in a php page “test.php”?
Yes we can include file more than once in a PHP script.
11. What are the different tables present in mysql, which type of table is generated when we are creating a table in the following syntax?
create table employee(eno int(2),ename varchar(10))
Total 5 types of tables we can create
MyISAM is the default storage engine as of MySQL 3.23.
12. Functions in IMAP, POP3 AND LDAP?
Please visit http://php.net/imap for details of IMAP functions in PHP and http://php.net/ldap for LDAP functions in PHP.
13. How can I execute a php script using command line?
Just run the PHP CLI (Command Line Interface) program and provide the PHP script file name as the command line argument. For example, “php myScript.php”, assuming “php” is the command to invoke the CLI program. Be aware that if your PHP script was written for the Web CGI interface, it may not execute properly in command line environment.
14. Suppose your ZEND engine supports the mode then how can you configure your php ZEND engine to support mode?
If you change the line: short_open_tag = off in php.ini file. Then your php ZEND engine support only mode.
15. What is meant by nl2br()?
nl2br — Inserts HTML line breaks before all newlines in a string $string. Syntax is nl2br($string); Returns string with ‘
’ inserted before all newlines.
For example: echo nl2br(“god bless you”) will output “god bless you” to your browser.
16. What are the reasons for selecting lamp (Linux, apache, mysql, php) instead of combination of other software programs, servers and operating systems?
All of those are open source resources. Security of linux is more than windows. Apache is a better server that IIS both in functionality and security. MySQL is world most popular open source database. PHP is more faster that ASP, ASP.Net or any other scripting language.
17. How can we encrypt and decrypt a data present in a mysql table using mysql?
AES_ENCRYPT() and AES_DECRYPT()
18. How can we encrypt the username and password using php?
You can encrypt a password with the following MySQL> SET PASSWORD=PASSWORD(“Password”);
In PHP, We can encode data using base64_encode($string) and can decode using base64_decode($string);
19. What are the features and advantages of OBJECT ORIENTED PROGRAMMING?
One of the main advantages of OO programming is its ease of modification; objects can easily be modified and added to a system there by reducing maintenance costs. OO programming is also considered to be better at modeling the real world than is procedural programming. It allows for more complicated and flexible interactions. OO systems are also easier for non-technical personnel to understand and easier for them to participate in the maintenance and enhancement of a system because it appeals to natural human cognition patterns. For some systems, an OO approach can speed development time since many objects are standard across systems and can be reused. Components that manage dates, shipping, shopping carts, etc. can be purchased and easily modified for a specific system.
20. What are the differences between PROCEDURE ORIENTED LANGUAGES and OBJECT ORIENTED LANGUAGES?
Traditional programming has the following characteristics:
- Functions are written sequentially, so that a change in programming can affect any code that follows it. If a function is used multiple times in a system (i.e., a piece of code that manages the date), it is often simply cut and pasted into each program (i.e., a change log, order function, fulfillment system, etc). If a date change is needed (i.e., Y2K when the code needed to be changed to handle four numerical digits instead of two), all these pieces of code must be found, modified, and tested.
- Code (sequences of computer instructions) and data (information on which the instructions operates on) are kept separate. Multiple sets of code can access and modify one set of data. One set of code may rely on data in multiple places. Multiple sets of code and data are required to work together. Changes made to any of the code sets and data sets can cause problems through out the system.
Object-Oriented programming takes a radically different approach:
- Code and data are merged into one indivisible item – an object (the term “component” has also been used to describe an object.) An object is an abstraction of a set of real-world things (for example, an object may be created around “date”) The object would contain all information and functionality for that thing (A dateobject it may contain labels like January, February, Tuesday, Wednesday. It may contain functionality that manages leap years, determines if it is a business day or a holiday, etc., See Fig. 1). Ideally, information about a particular thing should reside in only one place in a system. The information within an object is encapsulated (or hidden) from the rest of the system. A system is composed of multiple objects (i.e., date function, reports, order processing, etc.). When one object needs information from another object, a request is sent asking for specific information. (for example, a report object may need to know what today’s date is and will send a request to the date object) These requests are called messages and each object has an interface that manages messages.
- OO programming languages include features such as “class”, “instance”, “inheritance”, and “polymorphism” that increase the power and flexibility of an object.
21. What is the use of friend function?
Friend Functions - Sometimes a function is best shared among a number of different classes. Such functions can be declared either as member functions of one class or as global functions. In either case they can be set to be friends of other classes, by using a friend specifier in the class that is admitting them. Such functions can use all attributes of the class whichnames them as a friend, as if they were themselves members of that class.
22. What are the different types of errors in PHP?
Three are three types of errors:
- 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, as you will see, you can change this default behavior.
- 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.
- 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 behaviour is to display them to the user when they take place.
23. How can we convert ASP Scripts to PHP Scripts?
You can download asp2php application from the site http://asp2php.naken.cc to convert ASP scripts to PHP.
24.How can we get second of the current time using date function?
$second = date(“s”);
25. What is the difference between the functions unlink and unset?
unlink() deletes the given file from the file system. unset() makes a variable undefined.
26. How can we register the variables into a session in PHP?
We can use the $_SESSION[$variable] to register the $variable as a session.
27. What is the difference between char and varchar data types?
char(M) M bytes 0
varchar(M) L+1 bytes where L
i.e. char data type allocate memory statically and varchar data type allocate memory dynamically
28. What is the functionality of md5() function in PHP?
md5() function is used in PHP to calculate the md5 hash of a string. The hash is a 32-character hexadecimal number. We can use it to generate keys, which we use to identify users etc. If we add random number techniques to it the md5 generated now will be totally different for the same string we are using.
29. How can we know the number of days between two given dates using mysql?
30. What are the differences between DROP TABLE and TRUNCATE TABLE statements in MySQL?
DROP TABLE statment is used to delete a table (the table structure, attributes, and indexes will also be deleted). The TRUNCATE TABLE command deletes only the data inside the table. It is used if we only want to get rid of the data inside a table, and not the table itself.
31. What are the different ways to login to a remote server? Explain the means, advantages and disadvantages?
There are 3 ways to logon to a remote server:
- Use ssh
- Use telnet if you are concerned with security
- You can also use rlogin to logon to a remote server
32. How would you backup and restore a big MySQL database? What are the advantages of the approach which you have taken over the others?
Use the mysqldump command. If you have Telnet/SSH access to your MySQL server, log in and issue the following command for each database you want to back up:
shell> mysqldump -u user -ppassword –opt -full database_name > backupfile.sql
Then move the resulting file(s) to your preferred backup areas. If you require more information on the mysqldump command, then simply check out this URL www.mysql.com/documentation/mysql
Copy all the relevant table files.
If the server isn’t updating anything (or you’ve deliberately killed mysqld for this purpose) then you can copy all the files with the following extensions in your MySQL data directory:
Make sure you restart the MySQL daemon once you finish copying and downloading the files to your preferred backup areas.
TIP: once you’ve completed the backup, restart MySQL with the –log-update switch. This will allow you to keep track of all modifications done in the MySQL tables since your last ‘dump’. To restore your dumps, you should either restore to an existing database or create a new database using
shell> mysqladmin create database_name
then issue the following command
shell> mysql -u user -ppassword database_name
If you don’t have access to Telnet/SSH and you’re unable to do backups using the methods described above, you should ask your host if it is possible for them to do a backup for you and put the backups in a separate directory so that you can easily FTP your backups to your selected backup areas. Otherwise, if you have access to phpMyAdmin, you can use the following procedure:
Access phpMyAdmin, and select the database you wish to ‘dump’ (backup). Scroll down and you will see a bulleted point saying: “View dump (schema) of database” along with some radio and check boxes. Choose ‘Structure and data’, then click on ‘Add Drop Table’ and ‘Send’ and click ‘Go’. This will save the ‘dump’ to your hard drive. To restore a dump using phpMyAdmin, simply insert the file in the correct place once you have chosen the correct database by doing the following:
Choose the database you will insert your data into, or create a new database. Insert the appropriate SQL queries you already have, or just paste the name of the .sql file you have on your hard drive into the text box under ‘Location of the textfile’, and fire away.
Tip: Use a cronjob to schedule backups periodically. The advantages of this approach is the backup is only a file consisting of SQL query. So that needs minimum spaces to backup a large database.
33. For the database from the previous question, please give an SQL query which returns the invoice number of all invoices which contain the article with the number "1234". The query should be able to run under a MySQL 4.0 database.
SELECT invoice_no FROM invoice WHERE article_id=1234;
34. What is meant by PEAR in php?
PEAR is short for “PHP Extension and Application Repository” and is pronounced just like the fruit. 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
The PHP Foundation Classes (PFC), The PHP Extension Community Library (PECL), A web site, mailing lists and download mirrors to support the PHP/PEAR community. PEAR is a community-driven project with the PEAR Group as the governing body. The project has been founded by Stig S. Bakken in 1999 and quite a lot of people have joined the project since then.
35. How can we know that a session is started in PHP or not?
A session starts by session_start()function. This session_start() is always declared in header portion. It always declares first then we register sessions variables using $_SESSION[’variable_name’]