Monday, 27 May 2013

Php Interview Questions

Que: What is PHP or Describe PHP in few words.

Ans: PHP is usually known as Personal Home Page but it is also known as Hypertext Pre-processor. It is server side scripting language.

Que: Why $abc and $$abc are different or what is the difference between $abc and $$abc?

Ans: $abc is used to store variable data. $$abc can be used to store variable of a variable. Data stored in $abc is fixed while data stored in $$abc is not fixed, it can be changed dynamically.

Que: What is Session in PHP or explain Session in PHP?

Ans: A PHP session is used to store information on the server for future use. This storage is temporary and is flushed out when the site is closed. Sessions can start by first creating a session id (unique) for each user.
You can use session_start() function.

Que: Do the compiler is needed in php or Do I need to compile my PHP files?

Ans: No, PHP is an interpreted language so you don't need compiler.

Que: What are different types of errors in php?

Ans: Notices, warnings and Fatal errors.

Que: What is urlencode and urldecode?

Ans: Urlencode can be used to encode a string that can be used in a url. It encodes the same way posted data from web page is encoded. It returns the encoded string.

Syntax: urlencode (string $str )

Urldecode can be used to decode a string. Decodes any %## encoding in the given string (Inserted by urlencode)

Syntax: urldecode (string $str )

Que: What is urlencode and urldecode in PHP?

urlencode () is the function that can be used conveniently to encode a string before using in a query part of a URL. This is a convenient way for passing variables to the next page.
urldecode() is the function that is used to decode the encoded string.

Que: What is meant by PEAR in php?

Ans: PEAR is short for "PHP Extension and Application Repository" PEAR brings 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 organised OOP library. PEAR also provides a command-line interface that can be used to automatically install "packages"
It provides us:
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

Que: What is the difference between ereg_replace() and eregi_replace()?
Ans: eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.

Que: How can we know the number of days between two given dates using PHP?
Ans: $date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";

Que: What Is a Persistent Cookie?
Ans: A persistent cookie is a cookie which is stored in a cookie file permanently on the browser's computer. By default, cookies are created as temporary cookies which stored only in the browser's memory. When the browser is closed, temporary cookies will be erased. You should decide when to use temporary cookies and when to use
Temporary cookies can not be used for tracking long-term information.
Persistent cookies can be used for tracking long-term information.
Temporary cookies are safer because no programs other than the browser can access them.
Persistent cookies are less secure because users can open cookie files see the cookie values.

Que: What does a special set of tags <?= and ?> do in PHP?
Ans: The output is displayed directly to the browser.

Que: How do you define a constant?
Ans: Via define() directive, like define ("MYCONSTANT", 100);

Que: What are the differences between require and include, include_once?
Ans: All three are used to an include file into the current page.
If the file is not present, require(), calls a fatal error, while in include() does not.
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. It des not call a fatal error if file not exists. require_once() does the same as include_once(), but it calls a fatal error if file not exists.

Que: How will you get the Uploaded File Information in the Receiving Script?
Ans: Uploaded file information is organized in $_FILES as a two-dimensional array as:
$_FILES[$fieldName]['name'] - The Original file name on the browser system.
$_FILES[$fieldName]['type'] - The file type determined by the browser.
$_FILES[$fieldName]['size'] - The Number of bytes of the file content.
$_FILES[$fieldName]['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES[$fieldName]['error'] - The error code associated with this file upload.

Que: When you use endif to end the conditional statement?
Ans: When the original if was followed by : and then the code block without braces.

Que: Can we send mail using JavaScript?
Ans: No. There is no way to send emails directly using JavaScript.
But you can use JavaScript to execute a client side email program send the email using the "mailto" code. Here is an example:
function myfunction(form)
{
tdata=document.myform.tbox1.value;
location="mailto:mailid@domain.com?subject=...";
return true;
}

Que: Explain function strstr and stristr?
Ans: strstr() returns part of a given string from the first occurrence of a given sub string to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".
stristr() is identical to strstr() except that it is case insensitive.

Que: Which functions allows you to store session data in a database?
Ans: session_set_save_handler() allows you to store the session data in a database.

Que: What will the following script output?
<?php
$ans = 3 - 5 % 3;
echo $ans;
?>
Ans: 1 is the answer

Que: Which data type will the $a variable have at the end of the following script?
<?php
$a = “1”;
echo $x;
?>
A. (int) 1
B. (string) “1”
C. (bool) True
D. (float) 1.0
E. (float) 1
Ans: B is correct

Que: What do you mean by an Abstract class and Interface ?
Ans: Depending on the context, an interface class is either a class of the interface layer or a class whose purpose is to create a contract between a caller and an implementation (usually by providing only purely virtual functions).
An abstract class is a class that is declared abstract—it may or may not include abstract methods. Abstract classes can contain abstract and concrete methods. Abstract classes cannot be instantiated directly i.e. we cannot call the constructor of an abstract class directly nor we can create an instance of an abstract class but they can be sub-classed.

Que: When a file is uploaded to the sever how can you check whether its uploaded or not ?
Ans: Use is_uploaded_file function which tells whether the file was uploaded via HTTP POST.
is_uploaded_file($_FILES['userfile']['tmp_name']);
Returns TRUE on success or FALSE on failure.

Copyright © 2013 Powered by http://answerphp.blogspot.in/