PHP Interview Questions and Answers for Freshers pdf & Experienced Candidates

1. What's PHP?
Answer
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. What Is a Session?
Answer
A session is a logical object created by the PHP engine to allow you to preserve data across subsequent HTTP requests.

There is only one session object available to your PHP scripts at any time. Data saved to the session by a script can be retrieved by the same script or another script when requested from the same visitor.

Sessions are commonly used to store temporary data to allow multiple PHP pages to offer a complete functional transaction for the same visitor.

 3. What is meant by PEAR in php?
Answer
PEAR is the next revolution in PHP. This repository is bringing higher level programming to PHP. PEAR is a framework and distribution system for reusable PHP components. It eases installation by bringing an automated wizard, and packing the strength and experience of PHP users into a nicely organised OOP library. PEAR also provides a command-line interface that can be used to automatically install "packages".

4. What are the differences between require and include, include_once?
Answer
require_once() and include_once() are both the functions to include and evaluate the specified file only once. If the specified file is included previous to the present call occurrence, it will not be done again.

But require() and include() will do it as many times they are asked to do.


5. What is the difference between mysql_fetch_object and mysql_fetch_array?
Answer
MySQL fetch object will collect first single matching record where mysql_fetch_array will collect all matching records from the table in an array.

6. How do you pass a variable by value?
Answer
Just like in C++, put an ampersand in front of it, like $a = &$b.


7. What is the difference between ereg_replace() and eregi_replace()?
Answer
eregi_replace() function is identical to ereg_replace() except that it ignores case distinction when matching alphabetic characters.


8. What are the differences between DROP a table and TRUNCATE a table?
Answer
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.


9. How do you call a constructor for a parent class?
Answer
parent::constructor($value).


10. WHAT ARE THE DIFFERENT TYPES OF ERRORS IN PHP?
Answer
Here are three basic types of runtime errors in PHP:

    Notices: These are trivial, non-critical errors that PHP encounters while executing a script - for example, accessing a variable that has not yet been defined. By default, such errors are not displayed to the user at all - although you can change this default behavior.
    Warnings: These are more serious errors - for example, attempting to include() a file which does not exist. By default, these errors are displayed to the user, but they do not result in script termination.
    Fatal errors: These are critical errors - for example, instantiating an object of a nonexistent class, or calling a non-existent function. These errors cause the immediate termination of the script, and PHP's default behavior is to display them to the user when they take place.

Internally, these variations are represented by twelve different error types.


11. 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.


12. How can we submit a form without a submit button?
Answer
If you don't want to use the Submit button to submit a form, you can use normal hyper links to submit a form. But you need to use some JavaScript code in the URL of the link. For example:
<a href="javascript: document.myform.submit();">Submit Me</a>.


13. What is the difference between the functions unlink and unset?
Answer
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.

14. How can we register the variables into a session?
Answer
session_register($session_var);
$_SESSION['var'] = 'value';

15. How can we create a database using PHP and mysql?
Answer
We can create MySQL database with the use of mysql_create_db($databaseName) to create a database.

16. How many ways we can retrieve the date in result set of mysql using php?
Answer
As individual objects so single record or as a set or arrays.

17. How many ways can we get the value of current session id?
Answer
session_id() returns the session id for the current session.


18. Can we use include ("abc.php") two times in a php page "makeit.php"?
Answer
Yes.

19. What's the difference between include and require?
Answer
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.

20. Explain the ternary conditional operator in PHP?
Answer
Expression preceding the ? is evaluated, if it’s true, then the expression preceding the : is executed, otherwise, the expression following : is executed.

21. What's the difference between htmlentities() and htmlspecialchars()?
Answer
htmlspecialchars only takes care of <, >, single quote ‘, double quote " and ampersand.
htmlentities translates all occurrences of character sequences that have different meaning in HTML.


22. What is the difference between Reply-to and Return-path in the headers of a mail function?
Answer
Reply-to : Reply-to is where to delivery the reply of the mail.
Return-path : Return path is when there is a mail delivery failure occurs then where to delivery the failure notification.

23. How can we destroy the session, how can we unset the variable of a session?
Answer
session_unregister() - Unregister a global variable from the current session.
session_unset() - Free all session variables.

24. What are the different functions in sorting an array?
Answer
Sorting functions in PHP:
  • asort()
  • arsort()
  • ksort()
  • krsort()
  • uksort()
  • sort()
  • natsort()
  • rsort()


25. How can we know the count / number of elements of an array?
Answer
2 ways:
sizeof($array) - This function is an alias of count().
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.

26. How many ways I can redirect a PHP page?
Answer
Here are the possible ways of php page redirection.
1. Using Java script:
'; echo 'window.location.href="'.$filename.'";'; echo ''; echo ''; echo ''; echo ''; } }
redirect
2. Using php function: header


27. What is the maximum length of a table name, a database name, or a field name in MySQL?
Answer
Database name: 64 characters
Table name: 64 characters
Column name: 64 characters


28. How many values can the SET function of MySQL take?
Answer
MySQL SET function can take zero or more values, but at the maximum it can take 64 values.

29. What are the other commands to know the structure of a table using MySQL commands except EXPLAIN command?
Answer
DESCRIBE table_name;

30. How can we find the number of rows in a table using MySQL?
Answer
Use this for MySQL
SELECT COUNT(*) FROM table_name;

31. How can we find the number of rows in a result set using PHP?
Answer
Here is how can you find the number of rows in a result set in PHP:

$result = mysql_query($any_valid_sql, $database_link);
$num_rows = mysql_num_rows($result);
echo "$num_rows rows found";


32. What is the default session time in php and how can I change it?
Answer
The default session time in php is until closing of browser.


33. How many ways we can we find the current date using MySQL?
Answer
SELECT CURDATE();
SELECT CURRENT_DATE();
SELECT CURTIME();
SELECT CURRENT_TIME();


34. What is the difference between CHAR and VARCHAR data types?
Answer
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.


35. How can we encrypt and decrypt a data present in a mysql table using mysql?
Answer
AES_ENCRYPT() and AES_DECRYPT().

36. How can we change the name of a column of a table?
Answer
MySQL query to rename table: RENAME TABLE tbl_name TO new_tbl_name
or,
ALTER TABLE tableName CHANGE OldName newName.

37. How can increase the performance of MySQL select query?
Answer
We can use LIMIT to stop MySql for further search in table after we have received our required no. of records, also we can use LEFT JOIN or RIGHT JOIN instead of full join in cases we have related data in two or more tables.


38. What is the functionality of MD5 function in PHP?
Answer
string md5(string)
It calculates the MD5 hash of a string. The hash is a 32-character hexadecimal number.


39. How can we know that a session is started or not?
Answer
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().

40. What is the difference between PHP4 and PHP5?
Answer
  • 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.

41. What is the maximum size of a file that can be uploaded using PHP and how can we change this?
Answer
You can change maximum size of a file set upload_max_filesize variable in php.ini file.


42. What are the differences between mysql_fetch_array(), mysql_fetch_object(), mysql_fetch_row()?
Answer
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.

0 comments: