1. Described PHP?

The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.

2. Described session in PHP?

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.

3. What is PEAR in PHP?

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 organised OOP library. PEAR also provides a command-line interface that can be used to automatically install "packages"

4. Can you please explain the difference between $message and $$message?

They are both variables. But $message is a variable with a fixed name. $$message is a variable who's name is stored in $message. For example, if $message contains "var", $$message is the same as $var.

$message is a simple variable whereas $$message is a reference variable. Example:
$user = 'bob'
is equivalent to
$holder = 'user';
$$holder = 'bob';

5. How you can protect special characters in Query String?

If you want to include special characters like spaces in the query string, you need to protect them by applying the urlencode() translation function. The script below shows how to use urlencode():

<?php
print("<html>");
print("<p>Please click the links below"
." to submit comments about GlobalGuideLine.com:</p>");
$comment = 'I want to say: "It's a good site! :->"';
$comment = urlencode($comment);
print("<p>"
."<a href="processing_forms.php?name=Guest&comment=$comment">"
."It's an excellent site!</a></p>");
$comment = 'This visitor said: "It's an average site! :-("';
$comment = urlencode($comment);
print("<p>"
.'<a href="processing_forms.php?'.$comment.'">'
."It's an average site.</a></p>");
print("</html>");
?>

7. List the purpose of the following files having extensions: frm, myd, and myi? What these files contain?

In MySQL, the default table type is MyISAM.
Each MyISAM table is stored on disk in three files. The files have names that begin with the table name and have an extension to indicate the file type.

The '.frm' file stores the table definition.
The data file has a '.MYD' (MYData) extension.
The index file has a '.MYI' (MYIndex) extension,

8. How to find out the number of parameters passed into function9.?

func_num_args() function returns the number of parameters passed in.

9. Can you please explain the difference between ereg_replace() and eregi_replace()?

eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.

10. Described the functionality of the function strstr and stristr?

strstr() returns part of a given string from the first occurrence of a given substring to the end of the string. For example: strstr("user@example.com","@") will return "@example.com".
stristr() is idential to strstr() except that it is case insensitive.

Download Interview PDF

11. How to send mail using JavaScript?

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;
}

12. When you supposed to use endif to end the conditional statement?

When the original if was followed by : and then the code block without braces.

13. How to pass a variable by value?

Just like in C++, put an ampersand in front of it, like $a = &$b.

14. How to encrypt the username and password using PHP?

You can encrypt a password with the following Mysql>SET PASSWORD=PASSWORD("Password");
Or:
You can use the MySQL PASSWORD() function to encrypt username and password. For example,
INSERT into user (password, ...) VALUES (PASSWORD($password")), ...);

15. How to create table using PHP?

If you want to create a table, you can run the CREATE TABLE statement as shown in the following sample script:

<?php
include "mysql_connection.php";
$sql = "CREATE TABLE ggl_links ("
. " id INTEGER NOT NULL"
. ", url VARCHAR(80) NOT NULL"
. ", notes VARCHAR(1024)"
. ", counts INTEGER"
. ", time TIMESTAMP DEFAULT sysdate()"
. ")";
if (mysql_query($sql, $con)) {
print("Table ggl_links created.n");
} else {
print("Table creation failed.n");
}

mysql_close($con);
?>
Remember that mysql_query() returns TRUE/FALSE on CREATE statements. If you run this script, you will get something like this:
Table ggl_links created.

16. List the different tables present in MySQL?

Total 5 types of tables we can create:
1) MyISAM
2) Heap
3) Merge
4) INNO DB
5) ISAM

17. Can I use print "$a dollars" or "{$a} dollars" to print out the amount of dollars in this example?

In this example it wouldn't matter, since the variable is all by itself, but if you were to print something like "{$a},000,000 mln dollars", then you definitely need to use the braces.

18. I am trying to assign variable the value of 0123, but it keeps coming up with a different number, what is the problem?

PHP Interpreter treats numbers beginning with 0 as octal. Look at the similar PHP interview questions for more numeric problems.

19. How to 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.

20. Can you please explain the difference between mysql_fetch_object and mysql_fetch_array?

MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array.

21. How to get uploaded file information in the Receiving Script?

Once the Web server received the uploaded file, it will call the PHP script specified in the form action attribute to process them. This receiving PHP script can get the uploaded file information through the predefined array called $_FILES. 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.

The $fieldName is the name used in the <INPUT TYPE=FILE, NAME=fieldName>.

22. Define urlencode and urldecode?

urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits. For example: urlencode("10.00%") will return "10%2E00%25". URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.

string urlencode(str) - Returns the URL encoded version of the input string. String values to be used in URL query string need to be URL encoded. In the URL encoded version:

Alphanumeric characters are maintained as is.
Space characters are converted to "+" characters.
Other non-alphanumeric characters are converted "%" followed by two hex digits representing the converted character.
string urldecode(str) - Returns the original string of the input URL encoded string.

For example:
$discount ="10.00%";
$url = "http://domain.com/submit.php?disc=".urlencode($discount);
echo $url;
You will get "http://domain.com/submit.php?disc=10%2E00%25".

23. Can you please explain the difference between require and include, include_once?

require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.
But require() and include() will do it as many times they are asked to do.

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. The major difference between include() and require() is that in failure include() produces a warning message whereas require() produces a fatal errors.

24. Write the FORM Tag correctly for uploading files?

When users clicks the submit button, files specified in the <INPUT TYPE=FILE...> will be transferred from the browser to the Web server. This transferring (uploading) process is controlled by a properly written <FORM...> tag as:

<FORM ACTION=receiving.php METHOD=post ENCTYPE=multipart/form-data>

Note that you must specify METHOD as "post" and ENCTYPE as "multipart/form-data" in order for the uploading process to work. The following PHP code, called logo_upload.php, shows you a complete FORM tag for file uploading:

<?php
print("<html><form action=processing_uploaded_files.php"
." method=post enctype=multipart/form-data>n");
print("Please submit an image file a Web site logo for"
." globalguideline.com:
n");
print("<input type=file name=globalguideline_logo>
n");
print("<input type=submit>n");
print("</form></html>n");
?>

Download Interview PDF

25. Define constant in PHP?

Via define() directive, like define ("MYCONSTANT", 100);

26. What does special set of tags do in PHP?

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

27. Described persistent cookie in PHP?

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 persistent cookies based on their differences:

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

28. How to repair MySQL table?

The syntex for repairing a mysql table is:

REPAIR TABLE tablename
REPAIR TABLE tablename QUICK
REPAIR TABLE tablename EXTENDED

This command will repair the table specified.
If QUICK is given, MySQL will do a repair of only the index tree.
If EXTENDED is given, it will create index row by row.

29. How to know the number of days between two given dates using PHP?

Simple arithmetic:
$date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime() - strtotime()) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";

30. Where PHP configuration settings stored?

PHP stores configuration settings in a file called php.ini in PHP home directory. You can open it with any text editor to your settings.

31. How to replace substring in a given string in PHP?

If you know the position of a substring in a given string, you can replace that substring by another string by using the substr_replace() function. Here is a PHP script on how to use substr_replace():

<?php
$string = "Warning: System will shutdown in NN minutes!";
$pos = strpos($string, "NN");
print(substr_replace($string, "15", $pos, 2)." ");
sleep(10*60);
print(substr_replace($string, "5", $pos, 2)." ");
?>

This script will print:
Warning: System will shutdown in 15 minutes!
(10 minutes later)
Warning: System will shutdown in 5 minutes!
Like substr(), substr_replace() can take negative starting position counted from the end of the string.

32. How you take a substring from a given string in PHP?

If you know the position of a substring in a given string, you can take the substring out by the substr() function. Here is a PHP script on how to use substr():

<?php
$string = "beginning";
print("Position counted from left: ".substr($string,0,5)." ");
print("Position counted form right: ".substr($string,-7,3)." ");
?>

This script will print:
Position counted from left: begin
Position counted form right: gin
substr() can take negative starting position counted from the end of the string.

33. Which is the best way to test the strpos() Return Value in PHP?

Because strpos() could two types of values, Integer and Boolean, you need to be careful about testing the return value. The best way is to use the "Identical(===)" operator. Do not use the "Equal(==)" operator, because it does not differentiate "0" and "false". Check out this PHP script on how to use strpos():

<?php
$haystack = "needle234953413434516504381640386488129";
$pos = strpos($haystack, "needle");
if ($pos==false) {
print("Not found based (==) test ");
} else {
print("Found based (==) test ");
}
if ($pos===false) {
print("Not found based (===) test ");
} else {
print("Found based (===) test ");
}
?>

This script will print:
Not found based (==) test
Found based (===) test
Of course, (===) test is correct.

34. How you find a substring from a given string in PHP?

To find a substring in a given string, you can use the strpos() function. If you call strpos($haystack, $needle), it will try to find the position of the first occurrence of the $needle string in the $haystack string. If found, it will return a non-negative integer represents the position of $needle. Othewise, it will return a Boolean false. Here is a PHP script example of strpos():

<?php
$haystack1 = "2349534134345globalguideline16504381640386488129";
$haystack2 = "globalguideline234953413434516504381640386488129";
$haystack3 = "guideline234953413434516504381640386488129ggl";
$pos1 = strpos($haystack1, "globalguideline");
$pos2 = strpos($haystack2, "globalguideline");
$pos3 = strpos($haystack3, "globalguideline");
print("pos1 = ($pos1); type is " . gettype($pos1) . " ");
print("pos2 = ($pos2); type is " . gettype($pos2) . " ");
print("pos3 = ($pos3); type is " . gettype($pos3) . " ");
?>

This script will print:
pos1 = (13); type is integer
pos2 = (0); type is integer
pos3 = (); type is boolean
"pos3" shows strpos() can return a Boolean value

35. How to get number of characters in a String?

You can use the "strlen()" function to get the number of characters in a string. Here is a PHP script example of strlen():
<?php
print(strlen('It's Friday!'));
?>
This script will print:
12