PHP Interview Questions And Answers

1. What is PHP?
Answer: PHP is a web language based on scripts that allow developers to dynamically create generated web pages.
PHP is a server-side scripting language commonly used for web application

PHP has many frameworks and cms for creating websites. Even a nontechnical person can create sites using its CMS.

WordPress,osCommerce are the famous CMS of php. It is also an object-oriented programming language like java, C-sharp, etc. It is
very easy for learning.

2. What are the reasons for selecting a lamp (Linux, Apache, MySQL, PHP) instead of a combination of other software programs, servers and operating
systems?
Answer: 
All of those are open source resources. The security of Linux is very more than the windows. Apache is a better server that IIS both in functionality and security. Mysql is the world’s most popular open source database. Php is faster that asp or any other scripting language.

3. What Is The Difference Between Group By And Order By In Sql?
Answer: 
To sort a result, use an ORDER BY clause.
The most general way to satisfy a GROUP BY clause is to scan the whole table and create a new temporary table where all rows from each group are consecutive, and then use this temporary table to discover groups and apply aggregate functions (if any). ORDER BY [col1],[col2],…[coln]; Tells DBMS according to what columns it should sort the result. If two rows will have the same value in col1 it will try to sort them according to col2 and so on.

GROUP BY [col1],[col2],…[coln]; Tells DBMS to group (aggregate) results with the same value of column col1. You can use COUNT(col1), SUM(col1), AVG(col1) with it, if you want to count all items in group, sum all values or view average.

4. How To Support Multiple-page Forms?
Answer: 
If you have a long-form with lots of fields, you may want to divide the fields into multiple groups and present multiple pages with one group of fields on one page. This makes them a long-form more user-friendly. However, this requires you to write good scripts that:
• When processing the first page and other middle pages, you must keep those input values collected so far in the session or as hidden values in the next page form.
• When processing the last page, you should collect all input values from all pages for the final processes, like saving everything to the database.

5. How to create an array of a group of items inside an HTML form?
Answer: 
We can create input fields with the same name for “name” attribute with squire bracket at the end of the name attribute, It passes data as an array to PHP.

For instance: Define Object-Oriented Methodology
Object orientation is a software programming methodology that is based on modeling a real-world system. An object is the core concept involved in the object orientation. An object is a copy of the real-world entity. An object-oriented model is a collection of objects and its inter-relationships

6. What is PEAR?
Answer: 
PEAR is a framework and distribution system for reusable PHP components. The project seeks to provide a structured library of code, maintain a system for distributing code and for managing code packages, and promote a standard coding style.PEAR is broken into three classes: PEAR Core Components, PEAR Packages, and PECL Packages. The Core Components include the base classes of PEAR and PEAR_Error, along with database, HTTP, logging, and e-mailing functions. The PEAR Packages include functionality providing for authentication, networking, and file system features, as well as tools for working with XML and HTML templates.

7. What Is The Difference Between Php4 And Php5?
Answer: 
PHP4 cannot support oops concepts and Zend engine 1 is used.
PHP5 supports oops concepts and Zend engine 2 is used.
Error supporting is increased in PHP5.
XML and SQLLite will is increased in PHP5

8. What Are The Difference Between Abstract Class And Interface?
Answer: 
Abstract class: abstract classes are the class where one or moremethods
are abstract but not necessarily all method has to be abstract.
Abstract methods are the methods, which are declared in their class but not defined.
The definition of those methods must be in its extending class.
Interface: Interfaces are one type of class where all the methods are abstract. That
means all the methods only declared but not defined. All the methods must be
defined by its implemented class.

9. What is a session?
Answer: 
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.

10. What Are The Options To Transfer Session Ids?
Answer: 
Once a new session is created, its session ID must be transferred to the client browser and included in the next client request, so that the PHP engine can find the same session created by the same visitor. The PHP engine has two options to transfer the session ID to the client browser:

As URL parameter – The Session ID will be embedded in all URLs in the HTML document delivered to the client browser. When the visitor clicks any of those URLs, the session ID will be returned to the Web server as part of the requesting URL.
As a cookie – The session ID will be delivered as a cookie to the client browser. When a visitor requests any other pages on the Web server, the session ID will be returned back to the Web server also like a cookie.
The PHP engine is configured to use URL parameters for transferring session IDs by default

11. What Is A ResultSet Object?
Answer: 
A result set object is a logical representation of data rows returned by mysql_query() function on SELECT statements. Every result set object has an internal pointer used to identify the current row in the result set. Once you get a result set object, you can use the following functions to retrieve detail information:
• mysql_free_result($rs) – Closes this result set object.
• mysql_num_rows($rs) – Returns the number rows in the result set.
• mysql_num_fields($rs) – Returns the number fields in the result set.
• mysql_fetch_row($rs) – Returns an array contains the current row indexed by field position numbers.
• mysql_fetch_assoc($rs) – Returns an array contains the current row indexed by field names.
• mysql_fetch_array($rs) – Returns an array contains the current row with double indexes: field position numbers and filed names.
• mysql_fetch_lengths($rs) – Returns an array contains lengths of all fields in the last row returned.
• mysql_field_name($rs, $i) – Returns the name of the field of the specified index.

12. What are PHP sessions and how do they work?
Answer: 
What you’re really asking is whether they know how to use session_start(). It either creates or resumes a session based on an identifier that is sent to the server via a GET or POST request or a cookie. The most common use case scenario on the web is when a website won’t let you comment or post without first prompting a login. How does it know whether you’re logged in? One way would be to place a cookie on the user’s browser; on every request, the cookie is sent server-side, where PHP can be used to determine which information is sent back and displayed to the client. While session_start() saves session data in files by default, it is also possible to store sessions directly in the database.

13. What is the difference between Session and Cookie?
Answer: 
The main difference between sessions and cookies is that sessions are stored on the server, and cookies are stored on the user’s computers in the text file format. Cookies can’t hold multiple variables while session can hold multiple variables. We can set expiry for a cookie, The session only remains active as long as the browser is open. Users do not have access to the data you stored in Session Since it is stored in the server. The session is mainly used for login/logout purposes while cookies using for user activity tracking

substring of string formed by splitting it on boundaries formed by the string delimiter.

14. What are the different errors in PHP?
Answer:

In PHP, there are three types of runtime errors, they are:

Warnings: These are important errors. Example: When we try to include () file which is not available. These errors are shown to the user by default but they will not result in ending the script.
Notices:
These errors are non-critical and trivial errors that come across while executing the script in PHP. Example: trying to gain access to the variable which is not defined. These errors are not showed to the users by default even if the default behavior is changed.
Fatal errors:
These are critical errors. Example: instantiating an object of a class that does not exist or a non-existent function is called. These errors result in the termination of the script immediately and default behavior of PHP is shown to them when they take place. Twelve different error types are used to represent these variations internally.

15. Explain PHP variable-length argument function?
Answer:
 PHP supports variable-length argument function. It means you can pass 0, 1 or n number of arguments in a function. To do this, you need to use 3 ellipses (dots) before the argument name. The 3 dot concept is implemented for variable-length argument since PHP 5.6.

16. What is PHP session_start() and session_destroy() function?
Answer: 
PHP session_start() function is used to start the session. It starts a new or resumes the existing session. It returns the existing session if the session is created already. If the session is not available, it creates and returns new sessions.

17. What Are The Differences Between getting And Post Methods In Form Submitting, Give The Case Where We Can Use Get And We Can Use Post Methods?
Answer: 
When you want to send short or small data, not containing ASCII characters, then you can use GET” Method. But for long data sending, say more than 100 characters you can use the POST method.

Once the most important difference is when you are sending the form with getting a method. You can see the output which you are sending in the address bar. Whereas if you send the form with the POST” method then the user can not see that information.

18. What Are The Different Types Of Errors In Php?
Answer:

Here are three basic types of runtime errors in PHP:

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.
Warnings: These are more serious errors – for example, attempting to include() a file that 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 nonexistent 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. 

19. What’s The Difference Between Include And Require?
Answer: 
It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.

20. What Is Meant By Pear In Php?
Answer: 
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard and packing the strength and experience of PHP users into a nicely organized OOP library. PEAR also provides a command-line interface that can be used to automatically install “packages”.

21. Would You Initialize Your Strings With Single Quotes Or Double Quotes?
Answer:  
Since the data inside the single-quoted string is not parsed for variable substitution, it’s always a better idea speed-wise to initialize a string with single quotes unless you specifically need variable substitution.

22. What Are The Advantages Of Stored Procedures, Triggers, Indexes?
Answer: 
A stored procedure is a set of SQL commands that can be compiled and stored in the server. Once this has been done, clients don’t need to keep re-issuing the entire query but can refer to the stored procedure. This provides better overall performance because the query has to be parsed only once, and less information needs to be sent between the server and the client. You can also raise the conceptual level by having libraries of functions in the server. However, stored procedures, of course, do increase the load on the database server system, as more of the work is done on the server-side and less on the client (application) side. Triggers will also be implemented. A trigger is effectively a type of stored procedure, one that is invoked when a particular event occurs. For example, you can install a stored procedure that is triggered each time a record is deleted from a transaction table and that stored procedure automatically deletes the corresponding customer from a customer table when all his transactions are deleted.

Indexes are used to find rows with specific column values quickly. Without an index, MySQL must begin with the first row and then read through the entire table to find the relevant rows. The larger the table, the more these costs. If the table has an index for the columns in question, MySQL can quickly determine the position to seek to in the middle of the data file without having to look at all the data. If a table has 1,000 rows, this is at least 100 times faster than reading sequentially. If you need to access most of the rows, it is faster to read sequentially, because this minimizes disk seeks.

23. What is the use of the function htmlentities?
Answer: 
htmlentities Convert all applicable characters to HTML entities This function is identical to htmlspecialchars() in all ways, except with htmlentities(), all characters which have HTML character entity equivalents are translated into these entities

24. Explain the difference between classes and interfaces?
Answer: 
In layman’s terms, an interface is a class without all the business logic. In an interface, all methods must be public and multiple inheritances are supported. However, all methods must be defined within the class that implements them. Abstract classes, on the other hand, can be declared with modifiers like public or internal and can define properties or variables. Abstract classes do not support multiple inheritances and can only be extended by one abstract class.

25. What are the different types of errors in PHP?
Answer:

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 that 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.

26. Explain the Normalization Concept?
Answer: 
The normalization process involves getting our data to conform to three progressive normal forms, and a higher level of normalization cannot be achieved until the previous levels have been achieved (there are actually five normal forms, but the last two are mainly academic and will not be discussed).

First Normal Form
The First Normal Form (or 1NF) involves the removal of redundant data from horizontal rows. We want to ensure that there is no duplication of data in a given row and that every column stores the least amount of information possible (making the field atomic).

Second Normal Form
Where the First Normal Form deals with redundancy of data across a horizontal row, Second Normal Form (or 2NF) deals with redundancy of data in vertical columns. As stated earlier, the normal forms are progressive, so to achieve Second Normal Form, your tables must already be in First Normal Form.

Third Normal Form
I have a confession to make; I do not often use the Third Normal Form. In Third Normal Form we are looking for data in our tables that is not fully dependant on the primary key but dependant on another value in the table

27. What is “echo” in PHP?
Answer: 
PHP echo output one or more string. It is a language construct not a function. So the use of parentheses is not required. But if you want to pass more than one parameter to echo, the use of parentheses is required.

28. Explain PHP parameterized functions?
Answer: 
PHP parameterized functions are functions with parameters. You can pass any number of parameters inside a function. These passed parameters act as variables inside your function. They are specified inside the parentheses, after the function name. Output depends upon dynamic values passed as parameters into the function.

29. How can we increase the execution time of a PHP script?
Answer: 
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.

You can change the script run time by changing the max_execution_time directive in php.ini file

When a script is called, set_time_limit function restarts the timeout counter from zero. It means, if the default timer is set to 30 sec, and 20 sec is specified in function set_time_limit(), then the script will run for 45 seconds. If 0sec is specified in this function, the script takes unlimited time.

30. So If Md5() Generates The Most Secure Hash, Why Would You Ever Use The Less Secure Crc32() And Sha1()?
Answer: 
Crypto usage in PHP is simple, but that doesn’t mean it’s free. First off, depending on the data that you’re encrypting, you might have reasons to store a 32-bit value in the database instead of the 160-bit value to save on space. Second, the more secure the crypto is, the longer is the computation time to deliver the hash value. A high volume site might be significantly slowed down if frequent md5() generation is required.

31. Steps For The Payment Gateway Processing?
Answer: 
An online payment gateway is an interface between your merchant account and your Web site. The online payment gateway allows you to immediately verify credit card transactions and authorize funds on a customer’s credit card directly from your Web site. It then passes the transaction off to your merchant bank for processing, commonly referred to as transaction batching.

32. What Is A Cookie?
Answer: 
A cookie is a small amount of information sent by a Web server to a web browser and then sent back unchanged by the browser each time it accesses that server. HTTP cookies are used for authenticating, tracking, and maintaining specific information about users, such as site preferences and the contents of their electronic shopping carts. The term “cookie” is derived from “magic cookie”, a well-known concept in computing which inspired both the idea and the name of HTTP cookies.
A cookie consists of a cookie name and cookie value.

33. what is the use of rand() in php?
Answer: 
It is used to generate random numbers. If called without the arguments it returns a pseudo-random integer between 0 and grandma(). If you want a random number between 6 and 12 (inclusive), for example, use rand(6, 12). This function does not generate cryptographically safe values, and should not be used for cryptographic uses. If you want a cryptographically secure value, consider using openssl_random_pseudo_bytes() instead.

34. What is mean by an associative array?
Answer: 
Associative arrays are arrays that use string keys and are called associative arrays.

35. What is htaccess? Why do we use this and Where?
Answer: 
htaccess files are configuration files of Apache Server which provide

a way to make configuration changes on a per-directory basis. A file,
containing one or more configuration directives, is placed in a particular
document directory, and the directives apply to that directory, and all
subdirectories thereof. (Company)

36. How can we send mail using JavaScript?
Answer: 
JavaScript does not have any networking capabilities as it is designed to work on the client site. As a result, we can not send mails using javascript. But we can
call the client-side mail protocol mailtovia JavaScript to prompt for an email to
send. this requires the client to approve it.

37. What is the difference between include(), include_once() and require_once()?
Answer:
 They include() statement includes and evaluates a specified line i.e. it
will include a file-based in the given path. require() does the same thing except upon failure it will generate a fatal error and halt the script whereas include() will
just give a warning and allow the script to continue. require_once() will check if the
file already has been included and if so it will not include the file again.

38. What is the use of the explode() function?
Answer: 
Syntax : array explode ( string $delimiter , string $string [, int $limit ] );

This function breaks a string into an array. Each of the array elements is a substring of string formed by splitting it on boundaries formed by the string delimiter.

39. What is PEAR in PHP?
Answer: 
PEAR(PHP Extension and Application Repository) is a framework and

repository for reusable PHP components. PEAR is a code repository containing all
kinds of php code snippets and libraries.
PEAR also offers a command-line interface that can be used to automatically
install “packages”.
$message vs. $message in PHP.
$message is a variable with a fixed name. $message is a variable whose name is
stored in $message.
If $message contains “var”, $message is the same as $var.Echo vs. print
statement.

40. What is the difference between getting and POST functions?
Answer: 
GET function is normally used when the result of using this function does not

cause any visible change in the state of the world. This is normally used in cases
like searching from a database where no changes are made to the system.
POST function is used when there is a visible change in the state of the world.
When we use a POST function, normally the system changes. E.g. when we add,
delete or modify values from a database. A simple example is a form of submission.

41. require_once(), require(), include().What is the difference between them?
Answer: 
require() includes and evaluates a specific file, while require_once()

does that only if it has not been included before (on the same page). So,
require_once() is recommended to use when you want to include a file where you
have a lot of functions for example. This way you make sure you don’t include the
file more times and you will not get the “function re-declared” error.

42. Explain the Normalization Concept?
Answer: 
The normalization process involves getting our data to conform to three progressive normal forms, and a higher level of normalization cannot be achieved until the previous levels have been achieved (there are actually five normal forms, but the last two are mainly academic and will not be discussed).

First Normal Form
The First Normal Form (or 1NF) involves the removal of redundant data from horizontal rows. We want to ensure that there is no duplication of data in a given row and that every column stores the least amount of information possible (making the field atomic).

Second Normal Form
Where the First Normal Form deals with redundancy of data across a horizontal row, Second Normal Form (or 2NF) deals with redundancy of data in vertical columns. As stated earlier, the normal forms are progressive, so to achieve Second Normal Form, your tables must already be in First Normal Form.

Third Normal Form
I have a confession to make; I do not often use the Third Normal Form. In Third Normal Form we are looking for data in our tables that is not fully dependant on the primary key but dependant on another value in the table

43. What is Open Source Software?
Answer: 
Software in which the source codes are freely used, modify, and shared

by anyone is called Open Source Software. These can also be distributed under
licenses that adhere to the Open Source Definition.

44. Differences between getting and POST methods?
Answer: 
We can send 1024 bytes using the GET method but the POST method can transfer a large amount of data and POST is the secure method than the GET method.

45. Difference between mysql_connect and mysql_pconnect?
Answer: 
There is a good page in the php manual on the subject, in short mysql_pconnect() makes a persistent connection to the database which means a SQL link that does not close when the execution of your script ends. mysql_connect()provides only for the databasenewconnection while using mysql_pconnect, the function would first try to find a (persistent) link that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection… the connection to the SQL server will not be closed when the execution of the script ends. Instead, the link will remain open for future use.

46. Determine the value of $pear after executing the code below. What will strlen($pear) return? Explain your answer.?
Answer: 
This question reveals a few interesting things about the way PHP interprets code. The value of $pear in the code above will be the string “PEAR P” or the string “PEAR ” followed by seven spaces, followed by “P,” which is the first character in the string “PHP Extension and Application Repository.” The value returned by strlen($pear) will thus be 13. Since an element of a string can only consist of one character, and the count of elements within a string starts with 0, $pear[12] sets the 13th character of the string to the letter “P.” Interestingly enough, we chose to set the 13th value of a string that only has five characters. While other language interpreters might have thrown an out-of-bounds index error, PHP is more forgiving and fills in the blanks with empty spaces.

47. What is the importance of the “method” attribute in an Html form?
Answer:
 The “method” attribute determines how to send the form-data into the server. There are two methods, get and post. The default method is to get. This sends the form information by appending it on the URL. Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

48. How is it possible to set an infinite execution time for PHP script?
Answer: 
The set_time_limit(0) added at the beginning of a script sets to infinite

the time of execution to not have the PHP error ‘maximum execution time
exceeded’.It is also possible to specify this in the php.ini file.

49. What is the use of ‘print’ in php?
Answer:
 This is not actually a real function, It is a language construct. So you can use without parentheses with its argument list.

Example print(‘PHP Interview questions’);
print ‘Job Interview ‘);

50. What should we do to be able to export data into an Excel file?
Answer:
 The most common and used way is to get data into a format supported

by Excel. For example, it is possible to write a .csv file, to choose for example
comma as separator between fields and then to open the file with Excel

Note: Browse latest  PHP interview questions and Php tutorial. Here you can check  Testing Tools Training details and PHP training videos for self learning. Contact +91 988 502 2027 for more information.

Leave a Comment

FLAT 30% OFF

Coupon Code - GET30
SHOP NOW
* Terms & Conditions Apply
close-link