Wednesday, 29 January 2014

Php Interview Question Answer

Q-What is PHP?
A- 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.

Q-Explain the different types of errors in PHP.
A- 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.

Q-What is difference between require_once(), require(), include()?
A- require() includes and evaluates a specific file, if the file is not found then it shows a Fatal Error.
require_once() includes only the file which is not being included before. It is used to be recommended for the files where you have lots of functions stored.
include() includes the file, even if the file is not found, but it gives a warning to the user to include().

Q- Write a program to show the joining of two strings in PHP?
A- Two strings can be joined together by the use of a process called as concatenation. A dot (.) operator is used for this purpose. Example is as follows:
$string1 = _Hello_;
$string2 = _World!_;
$stringall = $string1.$string2;
echo $stringall;

Q- Explain when to use GET or POST. Provide example for both the ways.
A- Both GET and POST are used to collect data from a form. However, when security is desired $_POST should be used. When the $_ POST variable is used, all variables used are NOT displayed in the URL. Also, they have no restrictions to the length of the variables. GET should be used when the interaction with the form is more like a question. For e.g. firing a query etc. POST should be used more often when the interaction is more like an order or the interaction changes the state of the resource.
POST example:
<form action="sample.php" method="post">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
On clicking submit the URL resembles to: http://www.mysamplesite.com/sample.php
The sample.php file can be used for catching the form data using $_POST
Hello <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old!
GET EXAMPLE
<form action="sample.php" method="get">
Name: <input type="text" name="name" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
On clicking submit the URL resembles to : http://www.mysamplesite.com/sample.php?name=jim&age=37
The sample.php file can be used for catching the form data using $_GET
Hello <?php echo $_GET["name"]; ?>.<br />
You are <?php echo $_GET["age"]; ?> years old!

Q- What is HereDoc in PHP?
A- PHP heredoc syntax allows creating multiple lines of string without using quotations.
Example: Here, the heredoc starts with <<< operator and the multiple lines are followed. <<<identifier_name is used as an identifier making the start of  heredoc. To end the heredoc, the identifier is written again.
<?php
$str = <<<Sample
This is a sample string
A sample string
Sample;
Echo $str;
?>

Q- what is Session ? how to Start a Session and Destroy ?
A- PHP sessions introduction
When a user logs in an application, his details are usually stored in a session variable. This information is available to all pages in one application. Sessions in PHP work using a unique id for each visitor.
Starting a php session: This tag must also appear before the HTML tag.
Session_start();
Storing a session variable: Here in the sample variable value is set to 1
Session_start();
$_session[‘sample]=1;
Destroying a session:
Session_destory();

Q- Is multiple inheritance supported in PHP?
A- PHP includes only single inheritance, it means that a class can be extended from only one single class using the keyword ‘extended’.

Q- What is the meaning of a final class and a final method?
Q- ‘final’ is introduced in PHP5. Final class means that this class cannot be extended and a final method cannot be overrided.

Q- How is it possible to set an infinite execution time for PHP script?
A- 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.

Q- What is the function file_get_contents() usefull for?
A- file_get_contents() lets reading a file and storing it in a string variable.

Q- How can we connect to a MySQL database from a PHP script?
A-  To be able to connect to a MySQL database, we must use mysql_connect() function as follows:-
        <?php
            $database = mysql_connect("HOST", "USER_NAME", "PASSWORD");
              mysql_select_db("DATABASE_NAME",$database);
         ?>

Q- What is the function mysql_pconnect() usefull for?
A- mysql_pconnect() ensure a persistent connection to the database, it means that the connection do not close when the the PHP script ends.

Q- How is it possible to know the number of rows returned in result set?
A- The function mysql_num_rows() returns the number of rows in a result set.

Q- What is the difference between mysql_fetch_object() and mysql_fetch_array()?
A- The mysql_fetch_object() function collects the first single matching record where mysql_fetch_array() collects all matching records from the table in an array.

Q- How can we check the value of a given variable is a number?
A- It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.

Q- How can we check the value of a given variable is a number?
A- It is possible to use the dedicated function, is_numeric() to check whether it is a number or not.

Q- Add www before url through .htaccess file.

Ans- Check this link -

http://dense13.com/blog/2008/02/27/redirecting-non-www-to-www-with-htaccess/




Php interview question answer

Q: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?
A:1

When to use GET or POST

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.

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.

Quote from CGI FAQ

Firstly, the the HTTP protocol specifies
differing usages for the two methods. GET requests should always be
idempotent on the server. This means that whereas one GET request
might (rarely) change some state on the Server, two or more
identical requests will have no further effect.
This is a theoretical point which is also good
advice in practice. If a user hits “reload” on his/her browser, an
identical request will be sent to the server, potentially resulting
in two identical database or
guestbook entries, counter increments, etc. Browsers may reload a
GET URL automatically, particularly if cacheing is disabled (as is
usually the case with CGI output), but will typically prompt the
user before
re-submitting a POST request. This means you’re far less likely to
get inadvertently-repeated entries from POST.
GET is (in theory) the preferred method for
idempotent operations, such as querying a database, though it
matters little if you’re using a form. There is a further practical
constraint that many systems have built-in limits to the length of a
GET request they can handle: when the total size of a request (URL+params)
approaches or exceeds 1Kb, you are well-advised to use POST in any
case.
I would prefer POST when I don’t want the status to
be change when user resubmits. And GET
when it does not matter.
Q:2 Who is the father of PHP and explain the changes in PHP versions?
A:2 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
Q:3 How can we submit a form without a submit button?
A:3 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).
Q:4 In how many ways we can retrieve the data in the result set of
MySQL using PHP?
A:4 You can do it by 4 Ways1. mysql_fetch_row.
2. mysql_fetch_array
3. mysql_fetch_object
4. mysql_fetch_assoc
Q:5 What is the difference between mysql_fetch_object and
mysql_fetch_array?
A:5 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).
Q:6 What is the difference between $message and $$message?
A:6 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.
Q:7 How can we extract string ‘abc.com ‘ from a string ‘http://info@abc.com’
using regular expression of PHP?
A:7 preg_match(“/^http:\/\/.+@(.+)$/”,’http://info@abc.com&#8217;,$found);
echo $found[1];
Q:8 How can we create a database using PHP and MySQL?
A:8 We can create MySQL database with the use of
mysql_create_db(“Database Name”)
Q:9 What are the differences between require and include,
include_once and require_once?
A:9
The include() statement includes
and evaluates the specified file.The documentation below also applies to
require(). The two constructs
are identical in every way except how they handle
failure. include() produces a
Warning while require() results
in a Fatal Error. In other words, use
require() if you want a missing
file to halt processing of the page.
include()
does not behave this way, the script will
continue regardless.
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.include_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
redefinitions, variable value
reassignments, etc.
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
redefinitions, variable value
reassignments, etc.
Q:10 Can we use include (”abc.PHP”) two times in a PHP page “makeit.PHP”?
A:10 Yes we can use include() more than one time in any page though it is
not a very good practice.
Q: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)) ?
A:11 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.
Q:12 Functions in IMAP, POP3 AND LDAP?
A:12 You can find these specific information in PHP Manual.
Q:13 How can I execute a PHP script using command line?
A:13 As of version 4.3.0, PHP supports a new SAPI type (Server
Application Programming Interface) named CLI which means Command Line
Interface. 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.
Q:14 Suppose your Zend engine supports the mode <? ?> Then how can u
configure your PHP Zend engine to support <?PHP ?> mode ?
A:14 In php.ini file:
set
short_open_tag=on
to make PHP support
Q:15 Shopping cart online validation i.e. how can we configure Paypal,
etc.?
A:15 We can find the detail documentation about different paypal
integration process at the following site