PHP-Interview Questions

Posted: April 6, 2010 in Uncategorized

Given a line of text $string, how would you write a regular expression to strip all the HTML tags from it?

$stringOfText = “<p>This is a test</p>”; $expression = “/<(.*?)>(.*?)<\/(.*?)>/”; echo preg_replace($expression, “\\2”, $stringOfText); ….!

How can you block certain IP Addresses from accessing your site?

<?php $banned = array(“”, “”); if (in_array($_SERVER[‘REMOTE_ADDR’], $banned)) { echo “You have been banned.”; exit; } ?>   ….!

which is faster mysql_unbuffered_query or mysql_query ?
when we do the select queries that retrieve large data sets from MySQL, mysql_unbuffered_query in PHP is likely to give better performance than mysql_query. PHP manual says, it “sends a SQL query query to MySQL, without fetching and buffering the result rows automatically”.
What is the difference between char and varchar data types?
Set char to occupy n bytes and it will take n bytes even if u r storing avalue of n-m bytes Set varchar to occupy n bytes and it will take only the required space and will not use the n bytes eg. name char(10) will waste 5 bytes if we store ‘testname’, if each char takes a byteeg. name varchar(10) will just use 5 bytes if we store ‘testname’, if each char takes a byte. rest 5 bytes will be free. ….!

What will be the following script output?
<?php$a = 1;$a = $a— + 1;echo $a;?> A. 2B. 1C. 3D. 0E. Null Answer B is correct. ….!
What is the functionality of md5 function in PHP?
Calculate the md5 hash of a string. The hash is a 32-character hexadecimal number. ….!
Is it possible to set a time expire page in PHP.?
Yes it is Using header(“Expires: Mon, 26 Jul 2007 05:00:00 GMT”); <?php header(“Expires: Mon, 26 Jul 2007 05:00:00 GMT”); ?>
What are the different functions in sorting an array?
Ans: asort() arsort() ksort() krsort() uksort() sort() natsort() rsort() ….!

What is meant by Session Clustering?
The Session Manager session support allows multiple server instances to share a common pool of sessions, known as a session cluster Session clustering setting up methods : #1)First methods, is to have a NFS shared where session will be store. Setting this is quite easy, just a little modification on php.ini file to change the “session.save_path ? directive to point to the NFS share. The main problem with NFS is on high traffic, NFS share is really slow. So synchronisation and data corruption can arrive and ….!

What is Phishing?

In computing, phishing is a form of criminal activity using social engineering techniques. It is characterized by attempts to fraudulently acquire sensitive information, such as passwords and credit card details, by masquerading as a trustworthy person or business in an apparently official electronic communication. Phishing is typically carried out using email or an instant message. The term phishing derives from password harvesting and the use of increasingly sophisticated lures to “fish” for users’ financial information and passwords.

How can we know that a session is started or not?
A session starts by session_start() function.
This session_start() is always declared in header portion. it always declares first. then we write session_register().

What are the differences between  mysql_fetch_array(),  mysql_fetch_object(),  mysql_fetch_row()?
Answer 1:
mysql_fetch_array() -> Fetch a result row as a combination of associative array and regular array.
mysql_fetch_object() -> Fetch a result row as an object.
mysql_fetch_row() -> Fetch a result set as a regular array().

Answer 2:
The difference between mysql_fetch_row() and mysql_fetch_array() is that the first returns the results in a numeric array ($row[0], $row[1], etc.), while the latter returns a the results an array containing both numeric and associative keys ($row[‘name’], $row[’email’], etc.). mysql_fetch_object() returns an object ($row->name, $row->email, etc.).

If we login more than one browser windows at the same time with same user and after that we close one window, then is the session is exist to other windows or not? And if yes then why? If no then why?
Session depends on browser. If browser is closed then session is lost. The session data will be deleted after session time out. If connection is lost and you recreate connection, then session will continue in the browser.

What are the MySQL database files stored in system ?
Data is stored in name.myd
Table structure is stored in name.frm
Index is stored in name.myi

What is the difference between PHP4 and PHP5?
PHP4 cannot support oops concepts and Zend engine 1 is used.

PHP5 supports oops concepts and Zend engine 2 is used.
Error supporting is increased in PHP5.
XML and SQLLite will is increased in PHP5.

Can we use include(abc.PHP) two times in a PHP page makeit.PHP”?
Yes we can include that many times we want, but here are some things to make sure of:
(including abc.PHP, the file names are case-sensitive)
there shouldn’t be any duplicate function names, means there should not be functions or classes or variables with the same name in abc.PHP and makeit.php

What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?
mysql_fetch_array – Fetch a result row as an associative array and a numeric array.

mysql_fetch_object – Returns an object with properties that correspond to the fetched row and moves the internal data pointer ahead. Returns an object with properties that correspond to the fetched row, or FALSE if there are no more rows

mysql_fetch_row() – Fetches one row of data from the result associated with the specified result identifier. The row is returned as an array. Each result column is stored in an array offset, starting at offset 0.

What is meant by nl2br()?
nl2br() inserts a HTML tag <br> before all new line characters \n in a string.

echo nl2br(“god bless \n you”);

god bless<br>

How can we encrypt and decrypt a data presented in a table using MySQL?
You can use functions: AES_ENCRYPT() and AES_DECRYPT() like:

AES_ENCRYPT(str, key_str)
AES_DECRYPT(crypt_str, key_str)

How can I retrieve values from one database server and store them in other database server using PHP?
For this purpose, you can first read the data from one server into session variables. Then connect to other server and simply insert the data into the database.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.