9.5.08

PHP interview question

Is it possible to set a time expire page in PHP.?
header("Expires: Mon, 26 Jul 2007 05:00:00 GMT");
?>


How can we save an image from a remote web server to my web server using PHP?
$file_rimg = fopen("http://w3answers /image23.jpg",'rb');
$newfile_name_img = "/tmp/tutorial.file";
$file_wnew = fopen($newfile_name_img,'wb');
while (!feof($file_rimg)) {
$chunk_rd = fread($file_rimg,1024);
fwrite($file_wnew,$chunk_rd);
}
fclose($file_wnew);
fclose(file_rimg);
?>


What is the output of 2^2 in php?
The answer is 0 (Zero)
Everyone expected answer would be 4.But answer is zero.
The ^ operator is different in each language.In PHP ^
means the bitwise exlusive or of the two numbers.

Output of this script ??

$x = 3;
switch ($x) {
case 2: echo 'line 1'; break;
case 3:
case 4: echo 'line 2'; break;
default: echo 'line 3';
}
?>

a. echo 'line 3';
b. echo 'line 2';
c. Error
d. None of the above

Ans: b (Answer is line2)


Output of this script ??

$list = array("clock","scissors","pen","yak");
$list[] = "cow";
$list[] = "dragon";
print "$list[4]";
?>

a. Error
b. cow
c. yak
d. nothing

ANS: b (cow)


Output of this script ??

echo 5+FALSE;
?>

a. 5
b. nothing
c. parse error
d. T_ECHO error

ANS: 5


Output of this script ??

$x = '';
switch ($x) {
case "0": echo "String"; break;
case 0: echo "Integer"; break;
case NULL: echo "NULL"; break;
case FALSE: echo "Boolean"; break;
case "": echo "Empty string"; break;
default: echo "Something else"; break;
}
?>

a. Something else
b. Empty string
c. Integer
d. String


ANS: Integer

Output of this script ??

function x ($y)
{

function y ($z)
{

return ($z*2);
}

return($y+3);
}

$y = 4;
$y = x($y)*y($y);
echo "$y";
?>

a. None
b. 55
c. 56
d. 57

ANS:56


Which function will give a list of system specific hashing algorithms that are supported by php ?
use the following funcion: hash_algos()
This function is only available in php 5.1.2 and above


What changes you will have to do in php.ini file for file uploading?
do the following line uncomment:
; Whether to allow HTTP file uploads.
file_uploads = On
; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
upload_tmp_dir = C:\apache2triad\temp
; Maximum allowed size for uploaded files.
upload_max_filesize = 2M


How can you get round the stateless nature of HTTP using PHP?

The top Most important techniques are sessions and cookies. To access a session, you will need to have session_start() at the top of each page (if you are getting errors while you use in other places of the page.put ob_start() after ), and then you will use the $_SESSION hash to access and store your session variables. For cookies, You must use the set_cookie function before any output is started in your PHP script. From then on you can use the $_COOKIE has to access your cookie variables and values.

Important:
HTTP is a stateless protocol. The advantage of a stateless protocol is that hosts do not need to retain information about users between requests, but this forces web developers to use alternative methods for maintaining users' states. For example, when a host would like to customize content for a user while visiting a website, the web application must be written to track the user's progress from page to page. A common method for solving this problem involves sending and requesting cookies. Other methods include server side sessions, hidden variables (when current page is a form), and URL encoded parameters (such as /index.php?session_id=some_unique_session_code).

What is CAPTCHA?

CAPTCHA stands for Completely Automated Public Turing Test to tell Computers and Humans Apart. To prevent spammers from using bots to automatically fill out forms, CAPTCHA programmers will generate an image containing distorted images of a string of numbers and letters. Computers cannot determine what the numbers and letters are from the image but humans have great pattern recognition abilities and will be able to fairly accurately determine the string of numbers and letters. By entering the numbers and letters from the image in the validation field, the application can be fairly assured that there is a human client using it. To read more look here:
http://en.wikipedia.org/wiki/Captcha


What is difference between require_once(), require(), include().?

Difference between require() and require_once(): require() includes and evaluates a specific file, while require_once() does that only if it has not been included before (on the same page). So, require_once() is recommended to use when you want to include a file where you have a lot of functions for example. This way you make sure you don't include the file more times and you will not get the "function re-declared" error. Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING. There is also include_once() which is the same as include(), but the difference between them is the same as the difference between require() and require_once().

If you have to work with dates in the following format: "Tuesday, February 14, 2006 @ 10:39 am", how can you convert them to another format, that is easier to use?

The strtotime function can convert a string to a timestamp. A timestamp can be converted to date format. So it is best to store the dates as timestamp in the database, and just output them in the format you like.


So let's say we have
$date = "Tuesday, February 14, 2006 @ 10:39 am";
In order to convert that to a timestamp, we need to get rid of the "@" sign, and we can use the remaining string as a parameter for the strtotime function.

So we have
$date = str_replace("@ ","",$date);
$date = strtotime($date);

now $date is a timestamp
and we can say:
echo date("d M Y",$date);

How we know browser properties?

get_browser() attempts to determine the capabilities of the user's browser. This is done by looking up the browser's information in the browscap.ini file.
echo $_SERVER['HTTP_USER_AGENT'] . "\n";
$browser = get_browser();
foreach ($browser as $name => $value)
{
echo "$name $value\n";
}

How i will check that user is, logged in or not. i want to make it a function and i want to use in each page and after login i want to go in current page(same page. where i was working)?

For this we can use the session objec($_SESSION)t. When the user login with his/ her user name and password, usually we check those to ensure for correctness. If that user name and password are valid one then we can store that user name in a session and then we can very that session variable has been set or not in a single files and we can include that file in all pages.


How i can get ip address?

We can use SERVER var $_SERVER['SERVER_ADDR'] and getenv("REMOTE_ADDR") functions to get the IP address.


What is differenc between mysql_connect and mysql_pconnec?

mysql_pconnect establishes a persistent connection. If you don't need one (such as a website that is mostly HTML files or PHP files that don't call the db) then you don't need to use it. mysql_connect establishes a connection for the duration of the script that access the db. Once the script has finished executing it closes the connection. The only time you need to close the connection manually is if you jump out of the script for any reason.

If you do use mysql_pconnect. You only need to call it once for the session. That's the beauty of it. It will hold open a connection to the db that you can use over and over again simply by calling the resource ID whenever you need to interact with the db.


What is the difference between echo and print statement?

There is a slight difference between print and echo which would depend on how you want to use the outcome. Using the print method can return a true/false value. This may be helpful during a script execution of somesort. Echo does not return a value, but has been considered as a faster executed command. All this can get into a rather complicated discussion, so for now, you can just use whichever one you prefer.


How to make a download page in own site, which i can know that how many file has been loaded by particular user or particular ipaddress?

We can use hyperlink having URL where file are kept. and we only allow regisetered user to download. from session of user we can get the user detail.

What are the Technology to Use for Parsing the XML ?

There are two types of XML parsers the we can use.
1. DOM (Document Object Module).
2. SAX (Simple API for XML).

DOM

The DOM (Document Object Model) extension allows to operate on XML documents through the DOM API with PHP 5. DOM is a standard defined by the W3C for accessing XML documents.

In PHP 4, DOM xml extension is not following the standard method names.

As per the new W3C compatibility, the old domxml-based scripts won't work anymore. The API is quite different in PHP 5. But if we used the "almost W3C compatible" method names available in PHP 4.3, We only need to change the loading and saving methods, and remove the underscore in the method names. Other adjustments here and there may be necessary, but the main logic can stay the same. Though we have not used earlier so these will not be a problem for us.

The easiest way to read a well-formed XML file is to use the DOM library . The DOM library reads the entire XML document into an object and represents it as a tree of nodes,

SAX

SAX stands for Simple API for XML. It's a callback-based interface for parsing XML documents. SAX support has been available since PHP 3 and hasn't changed a lot since then. For PHP 5 the API is unchanged, The only difference is that it's not based on the expat library anymore, but on the libxml2 library.

Rather than treating an XML document as a tree-like structure, SAX treats it as a series of events such as startDocument or endElement. To accomplish this, a SAX appllication consists of a parser that sends these events to "handlers," methods or functions designated to handle them.

If you need a superfast forward only XML parser, you can use XMLReader. However you probably don\'t want a forward only parser.

If you want to read data from an XML file, the simple XML extension is probably the nicest solution. Also for creating XML, it might work well for you. Have a look at this article: http://devzone.zend.com/node/view/id/688.

If you want a superfast forward only XML writer, use XMLWriter. You could also do this by just outputting the XML of sticking it all together in a string. But the XMLWriter will make your code clearer, automatically escape stuff and give some error indications.



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


what is Magic methods in php?

There are seven special methods, and they are as follows:

__construct( )
Called when instantiating an object

__destruct( )
Called when deleting an object

__get( )
Called when reading from a nonexistent property

__set( )
Called when writing to a nonexistent property

__call( )
Called when invoking a nonexistent method

__toString( )
Called when printing an object (for eg: converting an object to strings)

__clone( )
Called when cloning an object (copying object)

There are actually 12 special methods at last count
The function names __construct, __destruct (see Constructors and Destructors), __call, __get, __set, __isset, __unset (see Overloading), __sleep, __wakeup, __toString, __set_state and __clone

http://us2.php.net/oop5.magic


what is scandir() ?

List files and directories inside the specified path
By default files order will be ascending
$f = scandir($direct, 1); it will display the files as descending order


Which types of form elements can be excluded from the HTTP request?
A. text, radio, and check box
B. text, submit, and hidden
C. submit and hidden
D. radio and check box

Answer D is correct.

When not selected, both radio buttons and check boxes are excluded from the HTTP request. Answer A, C, B are incorrect because they are always included in the request.

No comments: