PHP Interview Questions and Answers
Question - 51 : - How can we extract string "abc.com" from a string "mailto:info@abc.com?subject=Feedback" using regular expression of PHP?
Answer - 51 : - $text = "mailto:info@abc.com?subject=Feedback";
preg_match('|.*@([^?]*)|', $text, $output);
echo $output[1];
Note that the second index of $output, $output[1], gives the match, not the first one, $output[0].
Question - 52 : - if md5() generates the most secure hash, why would you ever use the less secure crc32() and sha1()?
Answer - 52 : - Crypto usage in PHP is simple, but that doesn’t mean it’s free. First off, depending on the data that you’re encrypting, you might have reasons to store a 32-bit value in the database instead of the 160-bit value to save on space. Second, the more secure the crypto is, the longer is the computation time to deliver the hash value. A high volume site might be significantly slowed down, if frequent md5() generation is required.
Question - 53 : - What are the different functions in sorting an array?
Answer - 53 : - Sorting functions in PHP:
asort()
arsort()
ksort()
krsort()
uksort()
sort()
natsort()
rsort()
Question - 54 : - How can we know the count/number of elements of an array?
Answer - 54 : - a) sizeof($array) - This function is an alias of count()
b) count($urarray) - This function returns the number of elements in an array.
Interestingly if you just pass a simple var instead of an array, count() will return 1.
Question - 55 : - How many ways we can pass the variable through the navigation between the pages?
Answer - 55 : - 1. Put the variable into session in the first page, and get it back from session in the next page.
2. Put the variable into cookie in the first page, and get it back from the cookie in the next page.
3. Put the variable into a hidden form field, and get it back from the form in the next page.
Question - 56 : - What is the maximum length of a table name, a database name, or a field name in MySQL?
Answer - 56 : - Database name: 64 characters
Table name: 64 characters
Column name: 64 characters
Question - 57 : - How many values can the SET function of MySQL take?
Answer - 57 : - MySQL SET function can take zero or more values, but at the maximum it can take 64 values.
Question - 58 : - What are the other commands to know the structure of a table using MySQL commands except EXPLAIN command?
Answer - 58 : - DESCRIBE table_name;
Question - 59 : - How can we find the number of rows in a table using MySQL?
Answer - 59 : - Use this for MySQL
SELECT COUNT(*) FROM table_name;
Question - 60 : - What’s the difference between md5(), crc32() and sha1() crypto on PHP?
Answer - 60 : - The major difference is the length of the hash generated. CRC32 is, evidently, 32 bits, while sha1() returns a 128 bit value, and md5() returns a 160 bit value. This is important when avoiding collisions.