Saturday, May 7, 2011

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 ',';

No comments:

Post a Comment