PHP Interview Questions With Answers

1)What is PHP?
PHP: Hypertext Preprocessor is open source server-side scripting language that is widely used for web development. PHP scripts are executed on the server. PHP allows writing dynamically generated web pages efficiently and quickly. The syntax is mostly borrowed from C, Java and perl. PHP is free to download and use.


2)What is PEAR in php?
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".


3)Explain how to submit form without a submit button.
We can achieve the above task by using JavaScript code linked to an event trigger of any form field and call the document.form.submit() function in JavaScript code.


4)Echo vs. print statement.
echo() and print() are language constructs in PHP, both are used to output strings. The speed of both statements is almost the same
echo() can take multiple expressions whereas print cannot take multiple expressions.
Print return true or false based on success or failure whereas echo doesn't return true or false.


5)$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.


6)Explain the different types of errors in PHP.
Notices, Warnings and Fatal errors are the types of errors in PHP
Notices:
Notices represents non-critical errors, i.e. accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all but whenever required, you can change this default behavior.
Warnings:
Warnings are more serious errors but they do not result in script termination. i.e calling include() a file which does not exist. By default, these errors are displayed to the user.
Fatal errors:
Fatal errors are critical errors i.e. calling a non-existent function or class. These errors cause the immediate termination of the script.


7)Explain the importance of the function htmlentities.
The htmlentities() function converts characters to HTML entities.


8)What is MIME?
MIME - Multi-purpose Internet Mail Extensions.
MIME types represents a standard way of classifying file types over Internet.
Web servers and browsers have a list of MIME types, which facilitates files transfer of the same type in the same way, irrespective of operating system they are working in.
A MIME type has two parts: a type and a subtype. They are separated by a slash (/).
MIME type for Microsoft Word files is application and the subtype is msword, i.e. application/msword.


9)How to use HTTP Headers inside PHP? Write the statement through which it can be added?
HTTP headers can be used in PHP by redirection which is written as:
<?header('Location: http://www.php.net')?>
The headers can be added to HTTP response in PHP using the header(). The response headers are sent before any actual response being sent. The HTTP headers have to be sent before taking the output of any data. The statement above gets included at the top of the script.


10)Why PHP is also called as Scripting language?
PHP is basically a general purpose language, which is used to write scripts. Scripts are normal computer files that consist of instructions written in PHP language. It tells the computer to execute the file and print the output on the screen. PHP is used for webpages and to create websites, thus included as scripting language.


11)What are the steps involved to run PHP?
The steps which are involved and required to run PHP is as follows:
1. Set up the web environment.
2. Set up the web servers. There are many web servers that are available and the mostly used is Apaches which automatically remains installed with linux distribution and on windows it is easy to install. There are other servers like IIS (Internet information server) provided by Microsoft can be used to set up the web environment.
3. Install the web server and PHP
4. Update and administer the system for changes.


12)How can we increase the execution time of a php script?
By the use of void set_time_limit(int seconds)
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini. If seconds is set to zero, no time limit is imposed.
When called, set_time_limit() restarts the timeout counter from zero. In other words, if the timeout is the default 30 seconds, and 25 seconds into script execution a call such as set_time_limit(20) is made, the script will run for a total of 45 seconds before timing out.


13)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?
The HTML 2.0 specification says, in section Form
Submission (and the HTML 4.0 specification repeats this with minor
stylistic changes):
–>If the processing of a form is idempotent
(i.e. it has no lasting observable effect on the state of the
world), then the form method should be GET. Many database searches
have no visible side-effects and make ideal applications of query
forms.
––>If the service associated with the processing of a form has side
effects (for example, modification of a database or subscription to
a service), the method should be POST.


14)How the form data is transmitted?
quotation from the HTML 4.0 specification
–> If the method is “get” – -, the user agent
takes the value of action, appends a ? to it, then appends the form
data set, encoded using the application/x-www-form-urlencoded
content type. The user agent then traverses the link to this URI. In
this scenario, form data are restricted to ASCII codes.
–> If the method is “post” –, the user agent conducts an HTTP post
transaction using the value of the action attribute and a message
created according to the content type specified by the enctype
attribute.


15)Who is the father of PHP and explain the changes in PHP versions?
Rasmus Lerdorf is known as the father of PHP.PHP/FI 2.0 is an early and no longer supported version of PHP. PHP 3
is the successor to PHP/FI 2.0 and is a lot nicer. PHP 4 is the current
generation of PHP, which uses the
Zend engine
under the
hood. PHP 5 uses
Zend engine 2 which,
among other things, offers many additionalOOP features


16)How can we submit a form without a submit button?
The main idea behind this is to use Java script submit() function in
order to submit the form without explicitly clicking any submit button.
You can attach the document.formname.submit() method to onclick,
onchange events of different inputs and perform the form submission. you
can even built a timer function where you can automatically submit the
form after xx seconds once the loading is done (can be seen in online
test sites).


 17)In how many ways we can retrieve the data in the result set of
MySQL using PHP?
You can do it by 4 Ways1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc


18)What is the difference between mysql_fetch_object and
mysql_fetch_array?
mysql_fetch_object() is similar tomysql_fetch_array(), with one difference -
an object is returned, instead of an array. Indirectly, that means that
you can only access the data by the field names, and not by their
offsets (numbers are illegal property names).


 19)What is the difference between $message and $$message?
It is a classic example of PHP’s variable variables. take the
following example.$message = “Mizan”;$$message = “is a moderator of PHPXperts.”;$message is a simple PHP variable that we are used to. But the
$$message is not a very familiar face. It creates a variable name $mizan
with the value “is a moderator of PHPXperts.” assigned. break it like
this${$message} => $mizanSometimes it is convenient to be able to have variable variable
names. That is, a variable name which can be set and used dynamically.


20)How can we extract string ‘abc.com ‘ from a string ‘http://info@abc.com
using regular expression of PHP?
 preg_match(“/^http:\/\/.+@(.+)$/”,’http://info@abc.com&#8217;,$found);
echo $found[1];


21)How can we create a database using PHP and MySQL?
We can create MySQL database with the use of
mysql_create_db(“Database Name”)


22)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
1. MyISAM
2. Heap
3. Merge
4. INNO DB
5. ISAM
MyISAM is the default storage engine as of MySQL 3.23 and as a result if
we do not specify the table name explicitly it will be assigned to the
default engine.


23)Suppose your Zend engine supports the mode <? ?> Then how can u
configure your PHP Zend engine to support <?PHP ?> mode ?
In php.ini file:
set
short_open_tag=on
to make PHP support


24)What is meant by nl2br()?
Inserts HTML line breaks (<BR />) before all newlines in a string
string nl2br (string); Returns string with ” inserted before all
newlines. For example: echo nl2br(“god bless\n you”) will output “god
bless <br /> you” to your browser.


25)How can we encrypt and decrypt a data present in a MySQL table
using MySQL?
AES_ENCRYPT () and AES_DECRYPT ()

Ad Inside Post

Comments system

Disqus Shortname