Saturday, May 7, 2011

PHP INTERVIEW QUESTIONS 40-50

41. How can we optimize or increase the speed of a mysql select query?


For increase speed of select query, there are some factor  like...
1. By using Limit in query
2. By using Index on table

3. By using Primary key

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



We can get the value of current session with the use of session_id() returns the session id for

the current session.Apart from session_id, we can retrieve it using SID constant in PHP6.


43. How can we destroy the cookie?

set the cookie in the past setcookie("name","value",time()-3600); 

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

session_destroy() - destroys all data registered to current session 
unset($_session['name']) - destroy varible specified with session


45. How many ways we can pass the variable through the navigation between the pages?
  1. Put the variable into session in the first page, and get it back from session in the next page.

  2. Put the variable into cookie in the first page, and get it back from the cookie in the next page.

  1. Put the variable into a hidden form field, and get it back from the form in the next page.

  2. Using post

  1. Using get

  2. Using request


46. What is the difference between ereg_replace() and eregi_replace()?


ereg_replace() is case sensitive like “test” cannot be treated as “TeSt” and

eregi_replace() is not case sensitive that means insensitive where “test” could be treated as “TeSt”.


47. What are the different functions in sorting an array?

Asort() arsort() ksort() krsort() uksort() sort() natsort() rsort()


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

count(array) function returns the total number of elements of specified array. 

49. What is the PHP predefined variable that tells the What types of images that PHP supports?

int imagetypes ( void ) This function returns a bit-field corresponding to the image formats
supported by the version of GD linked into PHP. The following bits are returned,
IMG_GIF | IMG_JPG | IMG_PNG |IMG_WBMP | IMG_XPM.

50. How can I know that a variable is a number or not, using a JavaScript?

isNaN(value)




PHP INTERVIEW QUESTIONS 30-40

31. How can we get second of the current time using date function?

date("s") 

32. How can we convert the time zones using PHP?


for date and time as well
$date=gmdate("Y-m-d H:i:s", time() + 3600*($timezone+date("I")));
for date
$date=gmdate("Y-m-d H:i:s", time() + 3600*($timezone+date("I")));
for time
$date=gmdate("H:i:s", time() + 3600*($timezone+date("I")));


Below is a simple method to convert between time zones. It simply uses the built-in PHP function gmstrftime function which is available in PHP 4.x and higher.

To use the ScheduleTime feature of the AddItem call, you need to indicate the scheduled start time for your item using GMT. If your user inputs schedule start time in local time, you need to convert to GMT. This sample shows how to make such a conversion.

Code

'<'?php

// Assume local schedule start time is 2008-09-05T08:05:00 (Pacific)
// Corresponding GMT start time is 2008-09-05T15:05:00

setlocale(LC_TIME, 'en_US');
echo "Pacific Time : ", strftime("%Y-%m-%dT%H:%M:%S", mktime(8, 5, 0, 9, 5, 2008)) . "\n";
echo "Greenwich Time : ", gmstrftime("%Y-%m-%dT%H:%M:%S", mktime(8, 5, 0, 9, 5, 2008)) . "\n";
?>

Sample Run

C:\>php TimeZone.php

Pacific Time : 2008-09-05T08:05:00
Greenwich Time : 2008-09-05T15:05:00

33. What is meant by urlencode and urldocode?


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.



34. What is the difference between the functions unlink and unset?



unlink: is used to delete a file – unlink(file path)  
unset:
is used to destroy an eralier declared variable – unset($userid)

35. How can we register the variables into a session?

you want to register a username as a session variable

means, There are 3 ways done in a job

First way

session_start();

$username=$_GET['username'];

session_register('username');


Second Way

session_start();

$username=$_GET['username'];

$HTTP_SESSION_VARS['username']=$username;

Third Way

session_start();

$username=$_GET['username'];

$_SESSION['username']=$username;


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


exif_imagetype()- for getting type of the image 
getimagesize() - for getting size of the image

imagesx - for width imagesy - for height

37. How can we get the browser properties using PHP?


$_SERVER['HTTP_USER_AGENT'];


38. What is the maximum size of a file that can be uploaded using PHP and how can we change this?


By default it is 2Mb. But you can change this limitation in php.ini file.
There is a variable'upload_max_filesize'


write .htaccess file:

php_value upload_max_filesize 50M
php_value max_execution_time 800

39. How can we increase the execution time of a PHP script?


By default PHP script execution time is set for 30 seconds

This maximum execution time limit is set inside the php.ini file like this. max_execution_time = 30 ; Maximum execution time of each script, in seconds

put sleep function in php script

set_time_limit ( 60 ) ;
$t1=time();
sleep(50);
$t2=time();
$t_lapsed=$t2-$t1;
echo "Total time lapsed = $t_lapsed";


Now the script will execute fine without any error message. We can see the total time taken by the script.

We can set the time to unlimited value by making it to 0 value. Like this


set_time_limit (0);



40. How can we take a backup of a mysql table and how can we restore it. ?

Databases:

backup: # mysqldump -u root -p[root_password] [database_name] > dumpfilename.sql 
restore:
# mysql -u root -p[root_password] [database_name] < dumpfilename.sql

Table: query
We can use below query for exporting data to a .txt file.
mysql> SELECT * FROM INTO OUTFILE '' FIELDS TERMINATED BY ',';
We can use below query for importing data from .txt file
mysql> LOAD DATA INFILE INTO TABLE '' FIELDS TERMINATED BY ',';