php interview questions and answers
1. What's PHP ?
The PHP Hypertext Preprocessor is a programming language that allows web developers to create dynamic content that interacts with databases. PHP is basically used for developing web based software applications.
2. How can we know the number of days between two given dates using PHP?
$date1 = date('Y-m-d');
$date2 = '2006-07-01';
$days = (strtotime($date1) - strtotime($date2)) / (60 * 60 * 24);
echo "Number of days since '2006-07-01': $days";
3. How do you define a constant?
define ("MYCONSTANT", 100);
4. What is meant by urlencode and urldecode?
urlencode() returns the URL encoded version of the given string. URL coding converts special characters into % signs followed by two hex digits.
urlencode("10.00%") will return "10%2E00%25". URL encoded strings are safe to be used as part of URLs.
urldecode() returns the URL decoded version of the given string.
5. How To Get the Uploaded File Information in the Receiving Script?
Uploaded file information is organized in $_FILES as a two-dimensional array as:
$_FILES[$fieldName]['name'] - The Original file name on the browser system.
$_FILES[$fieldName]['type'] - The file type determined by the browser.
$_FILES[$fieldName]['size'] - The Number of bytes of the file content.
$_FILES[$fieldName]['tmp_name'] - The temporary filename of the file in which the uploaded file was stored on the server.
$_FILES[$fieldName]['error'] - The error code associated with this file upload.
6. What is the difference between mysql_fetch_object and mysql_fetch_array?
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array
7. How do you pass a variable by value?
Just like in C++, put an ampersand in front of it, like $a = &$b.
|Top 50 php/mysql interview questions and answers|
9. What is the difference between ereg_replace() and eregi_replace()?
eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.
10. How do I find out the number of parameters passed into function ?
func_num_args() function returns the number of parameters passed in.
11. Are objects passed by value or by reference?
Everything is passed by value.
12. What are the differences between DROP a table and TRUNCATE a table?
DROP TABLE table_name - This will delete the table and its data.
TRUNCATE TABLE table_name - This will delete the data of the table, but not the table definition.
13. How do you call a constructor for a parent class?
14. How can we submit a form without a submit button?
15. How can we extract string 'abc.com ' from a string http://firstname.lastname@example.org using regular expression of php?
We can use the preg_match() function with "/.*@(.*)$/" as the regular expression pattern.
16. What is the difference between the functions unlink and unset?
unlink() is a function for file system handling. It will simply delete the file in context.
unset() is a function for variable management. It will make a variable undefined.
17. What is the difference between characters \047 and \x47?
The first one is octal 47, the second is hex 47.
18. How can we create a database using PHP and mysql?
We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.
19. How can we destroy the session, how can we unset the variable of a session?
session_unregister() - Unregister a global variable from the current session
session_unset() - Free all session variables
20. How can we know the count/number of elements of an array?
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
21. How many values can the SET function of MySQL take?
MySQL SET function can take zero or more values, but at the maximum it can take 64 values.
22. What are the other commands to know the structure of a table using MySQL commands except EXPLAIN command?
23. How can we find the number of rows in a table using MySQL?
SELECT COUNT(*) FROM table_name;
24. How can we find the number of rows in a result set using PHP?
$result = mysql_query($any_valid_sql, $database_link);
$num_rows = mysql_num_rows($result);
echo "$num_rows rows found";
25. What is the difference between CHAR and VARCHAR data types?
CHAR is a fixed length data type. CHAR(n) will take n characters of storage even if you enter less than n characters to that column. For example, "Hello!" will be stored as "Hello! " in CHAR(10) column.
VARCHAR is a variable length data type. VARCHAR(n) will take only the required storage for the actual number of characters entered to that column. For example, "Hello!" will be stored as "Hello!" in VARCHAR(10) column.
26. 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.
27. What is the difference between htmlentities() and htmlspecialchars()?
htmlspecialchars() - Convert some special characters to HTML entities (Only the most widely used)
htmlentities() - Convert ALL special characters to HTML entities
28. How can we get the properties (size, type, width, height) of an image using php image functions?
image size use getimagesize() function
image width use imagesx() function
image height use imagesy() function
29. How can we increase the execution time of a php script?
By the use of void set_time_limit(int seconds)
30. What are the difference between abstract class and interface?
Abstract class: abstract classes are the class where one or more methods are abstract but not necessarily all method has to be abstract. Abstract methods are the methods, which are declare in its class but not define. The definition of those methods must be in its extending class.
Interface: Interfaces are one type of class where all the methods are abstract. That means all the methods only declared but not defined. All the methods must be define by its implemented class.
31. What is the maximum size of a file that can be uploaded using PHP and how can we change this?
change maximum size of a file set upload_max_filesize variable in php.ini file
32. Explain the ternary conditional operator in PHP?
Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : is executed, otherwise, the expression following : is executed.
33. What’s the difference between include and require?
It’s how they handle failures. If the file is not found by require(), it will cause a fatal error and halt the execution of the script. If the file is not found by include(), a warning will be issued, but execution will continue.
34. How many ways can we get the value of current session id?
session_id() returns the session id for the current session.
35. What is the difference between $message and $$message?
They are both variables. But $message is a variable with a fixed name. $$message is a variable who's name is stored in $message. For example, if $message contains "var", $$message is the same as $var.
36. How can we get the browser properties using php?
echo $_SERVER['HTTP_USER_AGENT'] . "\n\n";
$browser = get_browser(null, true);
37. 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().
38. What is the use of obj_start()?
Its intializing the object buffer, so that the whole page will be first parsed (instead of parsing in parts and thrown to browser gradually) and stored in output buffer so that after complete page is executed, it is thrown to the browser once at a time.
39. What is the difference between Split and Explode?
Here the Example
$x="PHP is a ServerSide Scripting Language";
list($month, $day, $year) = split('[/.-]', $date);
40. Which will execute faster on php POST or GET?
Both are same while performing the action but using POST security is there.
Because using GET method in the action, form field values send along with URL, so at the time of sending password, problem will occur means password also will shown in the URL.
Using of POST there is no problem.
GET method has a limit of sending parameters 100 characters but POST method does not have a limit of sending data
GET is faster than POST. Because GET fetch the data directly from the URL but POST method fetch the encrypted data from the page.
41. What is the use of sprintf() function?
The sprintf() function writes a formatted string to a variable.
42. What Is a Session?
Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.
43. What is the use of header() function in php?
The header() function is used for redirect the page.if you want to redirect one page to another we can use it.
44. How can i get ip address?
REMOTE_ADDR - the IP address of the client
REMOTE_HOST - the host address of the client
45. What is htaccess?
.htaccess files (or "distributed configuration files") provide a way to make configuration changes on a per-directory basis.
46. What is the diffrence between Notify URL and Return URL?
Notify URL is used to just notify the status while processing.
Return URL is used to return after processing.
47. What is the difference between ucfirst and ucwords?
ucfirst() to convert the first letter of every string to uppercase, and ucwords(), to convert the first letter of every word in the string to uppercase.
48. What is meant by nl2br()?
nl2br() inserts a HTML tag
before all new line characters \n in a string.
49. How To Read the Entire File into a Single String?
$file = file_get_contents("/windows/system32/drivers/etc/services");
print("Size of the file: ".strlen($file)."n");
50. What are the different functions in sorting an array?
Sorting functions in PHP: