Explain when to use GET or POST. Provide example for both the ways.What are encryption functions in PHP?In how many ways we can retrieve data in the result set of mysql using php?How do you define a constant?How can you access a COM object from a PHP page?How can we know the count/number of elements of an array?How can we get the properties (size, type, width, height) of an image using php image functions?What does a special set of tags <?= and ?> do in PHP?How many ways can we get the value of current session id?How can we increase the execution time of a php script?How can we find the number of rows in a result set using PHP?What are the various methods to pass data from one web page to another web page?How many ways can we get the value of current session id?How to set cookies?How can we know that a session is started or not?How can we encrypt the username and password using PHP?How can we destroy the session, how can we unset the variable of a session?How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php?How can we extract string "abc.com" from a string "mailto:info@abc.com ?subject= Feedback" using regular expression of PHP? ~ Interview Questions & Answers

Saturday, August 16, 2008

Explain when to use GET or POST. Provide example for both the ways.

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:
Name:
Age:
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"]; ?>.
You are <?php echo $_POST["age"]; ?> years old!
GET EXAMPLE
Name:
Age:
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"]; ?>.
You are <?php echo $_GET["age"]; ?> years old!

Tuesday, July 8, 2008

What are encryption functions in PHP?

CRYPT()
MD5()

Monday, June 23, 2008

In how many ways we can retrieve data in the result set of mysql using php?

mysql_fetch_array - Fetch a result row as an associative array, a numeric array, or both
mysql_fetch_assoc - Fetch a result row as an associative array
mysql_fetch_object - Fetch a result row as an object
mysql_fetch_row —- Get a result row as an enumerated array

Sunday, June 22, 2008

How do you define a constant?

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

How can you access a COM object from a PHP page?

By utilizing the com_load and the dotnet_load respectively
you can incorporate COM objects and .NET libraries into your
PHP code, thus anytime you see "library code" in a question,
make sure you remember these two functions.
com_load — Creates a new reference to a COM component [deprecated]
Deprecated, use the OO syntax instead.
<?php
// do this
$obj = new COM($module);
// instead of this:
$obj = com_load($module);
?>

How can we know the count/number of elements of an array?

2 ways:
a) sizeof($array_name) - This function is an alias of count()
b) count($array_name) - This function returns the number of elements in an array.
Interestingly if you just pass a simple var instead of an array, count() will return 1.

How can we get the properties (size, type, width, height) of an image using php image functions?

To know the image size use getimagesize() function
To know the image width use imagesx() function
To know the image height use imagesy() function

Wednesday, June 18, 2008

What does a special set of tags do in PHP?

The output is displayed directly to the browser.

Friday, June 13, 2008

How many ways can we get the value of current session id?

Using session_id() function, the current value of the session can be found.
Syntax:
String session_id(string $id);

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.

How can we find the number of rows in a result set using PHP?

Here is how can you find the number of rows in a result set in PHP:
$result = mysql_query($any_valid_sql, $database_link);
$num_rows = mysql_num_rows($result);
echo "$num_rows rows found";

Sunday, June 8, 2008

What are the various methods to pass data from one web page to another web page?

Different methods to pass data from one web page to another:
1. Store the data in a session variable. By storing the data, the data can be passed from one page to another.
2. Store data in a cookie: By storing data in a persistent cookie, data can be passed from one form to another.
3. Set the data in a hidden field and post the data using a submit button.

Thursday, June 5, 2008

How many ways can we get the value of current session id?

Using session_id() function, the current value of the session can be found.
Syntax:
String session_id(string $id)

Tuesday, June 3, 2008

How to set cookies?

setcookie('variable','value','time');
variable - name of the cookie variable
value - value of the cookie variable
time - expiry time
Example: setcookie('Test',$i,time()+3600);
Test - cookie variable name
$i - value of the variable 'Test'
time()+3600 - denotes that the cookie will expire after an one hour

Syntax:
bool setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

How can we know that a session is started or not?

A session starts by session_start() function.
This session_start() is always declared in header portion. it always declares first. then we write session_register().

Monday, March 3, 2008

How can we encrypt the username and password using PHP?

User names and passwords in PHP can be encrypted using md5 function.
MD5 function calculates the md5 hash of a string. It is basically used for encryption. It is also used for digital signature applications, where a large file must be "compressed" in a secure manner.
Example:
Md5($str);
Crypt() function can also be used to encrypt a string,. It used MD5, DES or blow fish algorithms for encryption.
Syntax:
Crypt(str, salt)
Salt is an optional parameter used to increase the number of characters encoded, to make the encoding more secure

How can we destroy the session, how can we unset the variable of a session?

session_unset() function frees all session variables currently registered.
If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use unset() to unregister a session variable.
i.e. unset ($_SESSION['varname']);.

Sunday, February 3, 2008

How can we extract string 'abc.com ' from a string http://info@abc.com using regular expression of php?

We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern.
For example:
preg_match("/.*@(.*)$/","http://info@abc.com",$data);
echo $data[1];

How can we extract string "abc.com" from a string "mailto:info@abc.com ?subject= Feedback" using regular expression of PHP?

$text = "mailto:info@abc.com?subject=Feedback";
preg_match('|.*@([^?]*)|', $text, $output);
echo $output[1];
Note that the second index of $output, $output[1], gives the match, not the first one, $output[0].