Saturday, August 30, 2008

Interview Questions in PHP and General informations

1. How old PHP language is?
- PHP began in 1994, so 14 years old.

2. What are different names of PHP?
- PHP originally stood for Personal Home Page.
- At the formation of PHP 3, name changed into Hypertext Preprocessor by Zeev Suraski and Andi Gutmans, two Israeli developers

3. What is latest name of PHP?
- Hypertext Preprocessor

4. Which databases you can use with PHP?
- * Abstraction Layers
o DBA — Database (dbm-style) Abstraction Layer
o dbx
o ODBC — ODBC (Unified)
o PDO — PHP Data Objects

* Vendor Specific Database Extensions
o dBase
o DB++
o FrontBase
o filePro
o Firebird/InterBase
o Informix
o IBM DB2 — IBM DB2, Cloudscape and Apache Derby
o Ingres II
o MaxDB
o mSQL
o Mssql — Microsoft SQL Server
o MySQL
o Mysqli — MySQL Improved Extension
o OCI8 — Oracle OCI8
o Ovrimos SQL
o Paradox — Paradox File Access
o PostgreSQL
o SQLite
o SQLite3
o Sybase

5. Why PHP is called a scripting language?
A scripting language, script language or extension language, is a programming language that controls a software application. "Scripts" are often treated as distinct from "programs", which execute independently from any other application. At the same time they are distinct from the core code of the application, which is usually written in a different language, and by being accessible to the end user they enable the behavior of the application to be adapted to the user's needs. Scripts are often, but not always, interpreted from the source code or "semi-compiled" to bytecode which is interpreted, unlike the applications they are associated with, which are traditionally compiled to native machine code for the system on which they run. Scripting languages are nearly always embedded in the application with which they are associated.

6. Is PHP object oriented language?
- Yes, PHP 5 and above is an OOPs

7. What is different between PHP & ASP.NET?
- PHP is Open Source Software
- ASP.NET is Payment Software

- PHP is closely with MySQL Database because both are FREE softwares, MySQL is an Open Source SQL database
- ASP.NET is closely with MSSQL (Microsoft Sql Server) but it also payment

- PHP is very easy to learn and implement, style like C language
- ASP.NET using like ASP, also we need basic knowledge about .NET framework

- PHP having no Web services, to access the 3rd party controls
- ASP.NET we can use Web Services easily

- PHP uses both IIS server and Apache Server
- ASP.NET uses IIS Server

- ASP.NET being object oriented is more "organized" and maintainable than scripted PHP. Besides being fully compiled, ASP.NET platform offers loads of pure OO features like inheritance, polymorphism, overloading etc. Newer versions of PHP support OOP but its very limited compared to ASP.NET.

- Example program : #1
< %
' ASP Example
myFormVal = request.form("myInputField")
myQSval = request.querystring("myQSitem")
myVal = request.item("myFormOrQSitem")
%>

// PHP 4.1+ Example
$myFormVal = $_POST['myInputField'];
$myQSval = $_REQUEST['myQSitem'];

// PHP 3+ Example
$myFormVal = $HTTP_POST_VARS['myInputField'];

// If register_globals = on
$myVal = $myFormOrQSitem;
?>

- Example program : #2

< %
' VBScript Example
Option Explicit
myVar = 1
myOtherVar = myVar
myVar = 2

' myResult will be 3
myResult = myVar + myOtherVar
%>
// PHP Example
$myVar = 1;
'Use the ampersand to make a reference
$myOtherVar = &$myVar;
$myVar = 2;

// $myResult will be 4
$myResult = $myVar + $myOtherVar;
?>


8. What is the difference between include & require?
- The require() function is identical to include(), except that it handles errors differently.

- The include() function generates a warning (but the script will continue execution) while the require() function generates a fatal error (and the script execution will stop after the error).

- Example for include() function
/* Example for include */
include("wrongFile.php");
echo "Hello World!";

/* If the file 'wrongFile.php' is not available then Warning error and below will happen */
?>
Warning: include(wrongFile.php) [function.include]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5

Warning: include() [function.include]:
Failed opening 'wrongFile.php' for inclusion
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5

Hello World!

- Example for require() function
/* Example for require */
include("wrongFile.php");
echo "Hello World!";

/* If the file 'wrongFile.php' is not available then Fatal error and below will happen */
?>

Warning: require(wrongFile.php) [function.require]:
failed to open stream:
No such file or directory in C:\home\website\test.php on line 5

Fatal error: require() [function.require]:
Failed opening required 'wrongFile.php'
(include_path='.;C:\php5\pear')
in C:\home\website\test.php on line 5


9. What is the difference between include_once & require_once?

- both are similar like above but with one major difference
- both the functions checks the given file is already loaded or not, if the file is already loaded then the given file is not
loaded again or else it will load the file.

10. What is error control operator?

- Reporting Errors
To report errors from an internal function, you should call the php3_error() function. This takes at least two parameters -- the first is the level of the error, the second is the format string for the error message (as in a standard printf() call), and any following arguments are the parameters for the format string. The error levels are:

- E_NOTICE
Notices are not printed by default, and indicate that the script encountered something that could indicate an error, but could also happen in the normal course of running a script. For example, trying to access the value of a variable which has not been set, or calling stat() on a file that doesn't exist.

- E_WARNING
Warnings are printed by default, but do not interrupt script execution. These indicate a problem that should have been trapped by the script before the call was made. For example, calling ereg() with an invalid regular expression.

- E_ERROR
Errors are also printed by default, and execution of the script is halted after the function returns. These indicate errors that can not be recovered from, such as a memory allocation problem.

- E_PARSE
Parse errors should only be generated by the parser. The code is listed here only for the sake of completeness.

- E_CORE_ERROR
This is like an E_ERROR, except it is generated by the core of PHP. Functions should not generate this type of error.

- E_CORE_WARNING
This is like an E_WARNING, except it is generated by the core of PHP. Functions should not generate this type of error.

- E_COMPILE_ERROR
This is like an E_ERROR, except it is generated by the Zend Scripting Engine. Functions should not generate this type of error.

- E_COMPILE_WARNING
This is like an E_WARNING, except it is generated by the Zend Scripting Engine. Functions should not generate this type of error.

- E_USER_ERROR
This is like an E_ERROR, except it is generated in PHP code by using the PHP function trigger_error(). Functions should not generate this type of error.

- E_USER_WARNING
This is like an E_WARNING, except it is generated by using the PHP function trigger_error(). Functions should not generate this type of error.

- E_USER_NOTICE
This is like an E_NOTICE, except it is generated by using the PHP function trigger_error(). Functions should not generate this type of error.

- E_ALL
All of the above. Using this error_reporting level will show all error types.


11. What will these functions do? (explode(), file(), mysql_insert_id(), time() )
- Example for explode()
- explode() -- Split a string by string

/* Example for explode() */
$pizza = "piece1-piece2-piece3-piece4-piece5-piece6";
$pieces = explode("-", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
echo $pieces[4]; // piece5
?>

- Example for file()
- file() -- Reads entire file into an array
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.example.com/');

// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #{$line_num} : " . htmlspecialchars($line) . "
\n";
}
?>

- Example for mysql_insert_id()
- mysql_insert_id() -- Get the ID generated from the previous INSERT operation
- $link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db('mydb');

mysql_query("INSERT INTO mytable (product) values ('kossu')");
printf("Last inserted record has id %d\n", mysql_insert_id());
?>

12. Which function gives us absolute path of a file on the server?
- getcwd() -- gets the current working directory
-
/* current directory *
echo getcwd() . "\n"; /* /home/didou */

?>

13. Can we do automatic FTP using PHP? How?
- Yes, lots of FTP function available in PHP
- Ex: ftp_login(), ftp_mkdir (), ftp_ssl_connect() etc.,
-
// set up basic ssl connection
$conn_id = ftp_ssl_connect($ftp_server);

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass);

echo ftp_pwd($conn_id); // /

// close the ssl connection
ftp_close($conn_id);
?>

14. Can we do Flash programming using PHP? How?
- Yes, lots of Flash programming functions available in PHP
- Ex: swf_actiongeturl(), swf_endbutton() etc.,

15. Can we create dynamic PDF document using PHP? How?
- Yes, lots of functions available in PHP
- The PDF functions in PHP can create PDF files using the PDFlib library created by Thomas Merz.
- Example :

$pdf = pdf_new();
pdf_open_file($pdf, "test.pdf");
pdf_set_info($pdf, "Author", "Uwe Steinmann");
pdf_set_info($pdf, "Title", "Test for PHP wrapper of PDFlib 2.0");
pdf_set_info($pdf, "Creator", "See Author");
pdf_set_info($pdf, "Subject", "Testing");
pdf_begin_page($pdf, 595, 842);
pdf_add_outline($pdf, "Page 1");
$font = pdf_findfont($pdf, "Times New Roman", "winansi", 1);
pdf_setfont($pdf, $font, 10);
pdf_set_value($pdf, "textrendering", 1);
pdf_show_xy($pdf, "Times Roman outlined", 50, 750);
pdf_moveto($pdf, 50, 740);
pdf_lineto($pdf, 330, 740);
pdf_stroke($pdf);
pdf_end_page($pdf);
pdf_close($pdf);
pdf_delete($pdf);
echo "Finished";
?>

16. In which language PHP code is written?
- PHP's Common Gateway Interface binaries written in the C programming language

Popular Posts