How to increase or decrease the maximum execution time of a php script ?What are the different ways to login to a remote server? Explain the means, advantages and disadvantages?How many ways I can redirect a PHP page?How do I find out the number of parameters passed into function ?How many ways I can register the variables into session? ~ Interview Questions & Answers

Tuesday, June 23, 2009

How to increase or decrease the maximum execution time of a php script ?

By default PHP script execution time is set for 30 seconds at php.ini file. This is the time limit set for all the files to finish its execution. If the file takes more than this set time then the execution of the script will be stopped and error message will be displayed like this.
Fatal error: Maximum execution time of 30 seconds exceeded in C:\wamp\test.php on line 10
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
 
We will use 'time stamp' to find out the delay between starting and ending of a script execution. We will store the 'time stamp' value at the starting of the script and then again we will record at the end of the script . The difference of these two values will give us idea how much time the script has taken to execute. Note that here between two values of time we will try to add some delay by using 'sleep() function'. This sleep function takes numeric value in seconds and delay the execution for that many seconds. We can also create delay by using 'for loop' but this is a better solution. Here is the code.
$t1=time();
sleep(20);
$t2=time(); $t_lapsed=$t2-$t1;
echo "Total time lapsed = $t_lapsed";
We have introduced a delay of 20 seconds and this will execute without any problem, now increase the sleep value to 50 seconds and on execution you will get the error message saying maximum execution time exceeded. This is because by default the maximum execution time set at php.ini is 30 seconds. Now let us change the above code by adding one line set_time_limit(60). Here is the code
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);

Monday, June 22, 2009

What are the different ways to login to a remote server? Explain the means, advantages and disadvantages?

There is at least 3 ways to logon to a remote server:
Use ssh or telnet if you concern with security
You can also use rlogin to logon to a remote server.

Friday, June 5, 2009

How many ways I can redirect a PHP page?

a. Header function in PHP redirects to a new URL
Example:
<?php
header("Location: http://www.redirecturl.com/");
?>
b. http_redirect() is also used to redirect to a new page or URL.
Syntax
void http_redirect ( [string url [, array params [, bool session = FALSE [, int status]]]] )
Here, the URL is the path of the new page. params can be some query parameters followed by whether the session information needs to be passed and the custom response status code.

How do I find out the number of parameters passed into function ?

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

Wednesday, June 3, 2009

How many ways I can register the variables into session?

Global variables in PHP can be registered using the session_register() function. It accepts different number of arguments, any of which can be either a string holding the name of a variable or an array consisting of variable names or other arrays
Example:
Session_register(“simple”);
$_session can also be used for registering variables.
Example:
$_SESSION['count'] = 0;