Thursday, February 28, 2013

PHP Questions and Answers


PHP Developer Questions & Answers

Q) What is PHP?

ANS:PHP is a server side scripting language commonly used for web applications

Q) How to include a file to a php page?

ANS: we can include a file using "include() " or "require()" function with as its parameter.

Q) What Is a Session?

ANS: It can be used to store information on the server for future use

Q) What are the correct and the most two common way to start and finish a PHP block of code? 

ANS: The two most  common ways to start and finish a PHP script are: <?php [   ---  PHP code---- ] ?> and <? [---  PHP code  ---] ?>
 
Q) How can PHP and HTML interact?

ANS: It is possible to generate HTML through PHP scripts, and it is possible to pass informations from HTML to PHP.

Q) How can I display text with a PHP script?

ANS: Two methods are possible:
?
1
2
3
4
<?php
echo "Method 1";
print "Method 2";
?>
 
Q) How can we connect to a MySQL database from a PHP script?

ANS: To be able to connect to a MySQL database, we must use mysql_connect() function as follows:
?
1
2
3
4
<?php
$database = mysql_connect("HOST", "USER_NAME", "PASSWORD");
mysql_select_db("DATABASE_NAME",$database);
?>

Q) How can I display text with a PHP script?

ANS: Two methods are possible:
?
1
2
3
4
<?php
echo "Method 1";
print "Method 2";
?>

Q) What does the PHP error ‘Parse error in PHP – unexpected T_variable at line x’ means?

ANS: This is a PHP syntax error expressing that a mistake at the line x stops parsing and executing the program.

Q) what is the static variable in function useful for?

A static variable is defined within a function only the first time and its value can be modified during function calls as follows:
?
1
2
3
4
5
6
7
8
9
<?php
function testFunction() { static $testVariable = 1;
echo $testVariable;
$testVariable++;
}
testFunction();        //1
testFunction();        //2
testFunction();        //3
?>
 
Q) How is it possible to cast types in PHP?

ANS: The name of the output type have to be specified in parentheses before the variable which is to be cast as follows:
* (int), (integer) – cast to integer
* (bool), (boolean) – cast to boolean
* (float), (double), (real) – cast to float
* (string) – cast to string
* (array) – cast to array
* (object) – cast to object