PHP Questions

Q:

For printing out strings, there are echo, print and printf. Explain the differences.

Answer - echo is the most primitive of them, and just outputs the contents following the construct to the screen. print is also a construct (so parentheses are optional when calling it), but it returns TRUE on successful output and FALSE if it was unable to print out the string. However, you can pass multiple parameters to echo, like:

and it will output the string "Welcome to TechInterviews!" print does not take multiple parameters. It is also generally argued that echo is faster, but usually the speed advantage is negligible, and might not be there for future versions of PHP. printf is a function, not a construct, and allows such advantages as formatted output, but it’s the slowest way to print out data out of echo, print and printf.
Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2981
Q:

What’s the special meaning of __sleep and __wakeup?

Answer __sleep returns the array of all the variables than need to be saved, while __wakeup retrieves them.
Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2947
Q:

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

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

View answer Workspace Report Error Discuss

Subject: PHP

0 2885
Q:

What Is a Session in PHP?

Answer

A PHP Session persist the user information to be used later. For example, user name, password, shopping item details. The session is temporary and will be removed soon after the user has left the web site. The session can be persisted for long term usage on databases like MySQL. Each session is identified by a unique Id number for every visitor. The first step to use PHP session is to start the session. The starting session must precede the operations like HTML or the other.


The statement session_start() starts the PHP session and registers the user’s information on the server.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2832
Q:

Explain include(), include_once, require() and require_once.

Answer

include()
The include() function takes all the content in a specified file and includes it in the current file. If an error occurs, the include() function generates a warning, but the script will continue execution.

include_once()
File will not be included more than once. If we want to include a file once only and further calling of the file will be ignored then we have to use the PHP function include_once().

require()
The require() function is identical to include(), except that it handles errors differently. The require() generates a fatal error, and the script will stop.

require_once()
The required file is called only once when a page is open and further calling of the file will be ignored.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2812
Q:

How do you destroy a particular or all Sessions?

Answer

<?php
session_start();
// store session data
$_SESSION['views']=1;
unset($_SESSION['views']); // If you wish to delete some session data, you can use the unset()
session_destroy(); // You can also completely destroy the session by calling the session_destroy() function. session_destroy() will reset your session and you will lose all your stored session data.
?>

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2744
Q:

How can we increase the execution time of a php script?

Answer

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.

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2731
Q:

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

Answer

$result = mysql_query($sql, $db_link);
$num_rows = mysql_num_rows($result);
echo "$num_rows rows found";

Report Error

View answer Workspace Report Error Discuss

Subject: PHP

0 2719