Adsense1


Wednesday, December 15, 2010

PHP job interview questions --technical


1. Write a simple login script without using MySQL.

index.php
<html>
<body>
<form action='index.php' method='post'>
Username: <input type='text'  name='user'>
Password: <input type='password'  name='pass'>
<input type='submit'  value='Login'>
</form>
<?php
$user=$_POST['user'];
$pass=$_POST['pass'];
if
(($user=="jiansen") && ($pass=="jiansenpass")) echo " Access Granted !";
 else if (($user=="") && ($pass=="")) echo " ";
 else echo " Access Denied!";
?>
</body>
</html>


2.Write a simple logout php script.
 <?php
session_start();
session_destroy();
header('Location:  index.php');
?>

3. Write a script to connect to MySQL database.

<?php
$dbhost = '129.128.7.83';
$dbuser = 'jiansen';
$dbpass = 'jiansenpass';
$conn = mysql_pconnect($dbhost, $dbuser, $dbpass,true) or die( mysql_error() );
$dbname = 'bedrock';
if (!mysql_select_db('dbname')) {
    die('Could not select database: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM employees",$dbname);
printf("First Name: %s\n", mysql_result($result,0,"first"));
printf("Last Name: %s\n", mysql_result($result,0,"last"));
printf("Address: %s\n", mysql_result($result,0,"address"));
printf("Position: %s\n", mysql_result($result,0,"position"));
mysql_close($conn);
?>

4. How to print out php information on your server?
<?php
 phpinfo();
?>

5. Write a php script to send comments to your email.

<html>
<form action="mail.php" method="post">
Your Name: <input type="text" name="name"><br>:
E-mail: <input type="text" name ="email"><br><br>
Comments<br><textarea name="comments"></textarea><br><br>
<input type="submit" value="Submit">
</form>

<?
$name=$_POST['name'];
$email=$_POST['email'];
$comments=$_POST['comments'];
$to="me.me@gmail.com";
$message="$name just filled in your comments form. They said:\n$comments\n\n Their e-mail address was: $email";
if(mail($to,"Comments From Your Site",$message,"From: $email\n")) {
echo "Thanks for your comments.";
} else {
echo "There was a problem sending the mail. Please check that you filled in the form correctly.";
}
?>
<html>

6. Upload a file to a directory.
upload.html
<html>
<body>
<form action="myupload.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> <br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>


myupload.php
<?php
$uploadDir = '/home/jiansen/html/';
$uploadFile = $uploadDir.$_FILES['file']['name'];
print($uploadFile);
print  "\n<p>";
print($_FILES['file']['tmp_name']);
if
($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
 
{
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
 
}
print "<pre>";
if($_FILES["file"]["size"] < 20000){
if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadFile))
{
    print "File is valid, and was successfully uploaded. ";
    print "Here's some more  debugging info:\n";
    print_r($_FILES);
}
else
if($_FILES['file']['tmp_name'])
{
    print "Possible file upload attack!  Here's some debugging info:\n";
   
print_r($_FILES);
}}
else echo "file size greater than 20k";
print  "</pre>";
?>

7. How automatically refresh webpage in every second?
< meta  http-equiv ="refresh"  content="1" >

8. what is difference between require and include in PHP?
The two functions are identical in every way, except how they handle errors:

    * include() generates a warning, but the script will continue execution
    * require() generates a fatal error, and the script will stop

9.  Difference between mysql_connect() and mysql_pconnect()

mysql_connect() and mysql_pconnect() both are working for database connection but with little difference. In mysql_pconnect(), ‘p’ stands for persistance connection.

When we are using mysql_connect() function, every time it is opening and closing the database connection, depending on the request .

But in case of mysql_pconnect() function,
First, when connecting, the function would try to find a (persistent) connection that’s already open with the same host, username and password. If one is found, an identifier for it will be returned instead of opening a new connection.
Second, the connection to the SQL server will not be closed when the execution of the script ends. Instead, the connection will remain open for future use (mysql_close() will not close connection established by mysql_pconnect()).

mysql_pconncet() is useful when you have a lot of traffice on your site. At that time for every request it will not open a connection but will take it from the pool. This will increase the efficiency of your site. But for general use mysql_connect() is best.

10.  What is the difference between $_REQUEST, $_GET and $_POST
$_GET retrieves variables from the querystring, or your URL.
$_POST retrieves variables from a POST method, such as (generally) forms.
$_REQUEST is a merging of $_GET and $_POST where $_POST overrides $_GET. Good to use $_REQUEST on self refrential forms for validations. Hope that helps you out!

11. Why  use mysql_real_escape_string()?
mysql_real_escape_string() calls MySQL's library function mysql_real_escape_string, which prepends backslashes to the following characters: \x00, \n, \r, \, ', " and \x1a.

This function must always (with few exceptions) be used to make data safe before sending a query to MySQL.  Using mysql_real_escape_string() to prevent MySQL injection.

12. How many kinds of array in PHP?
In PHP, there are three kind of arrays:

* Numeric array - An array with a numeric index
* Associative array - An array where each ID key is associated with a value
* Multidimensional array - An array containing one or more arrays

13.  Difference between mysql_fetch_assoc,mysql_fetch_array & mysql_fetch_object ?
$rows = mysql_fetch_array( "select name, address from people");

Means you then get each of the row results as a straight array, meaning you dont necessarily need to know in advance the order elements arrive via the select.

foreach( $rows as $row )
echo $row[0] . ' lives at ' . $row[1] ;

$rows = mysql_fetch_assoc( "select name, address from people" );

Means you then get each of the row results as a named array elements, an associative array.

foreach( $rows as $row )
echo $row['name'] . ' lives at ' . $row['address'];

Which is now dependent on you knowing what the result columns are named.

$rows = mysql_fetch_object( "select name, address from people" );

Means you then get each of the row results using the object notation.

foreach( $rows as $row )
echo $row->name . ' lives at ' . $row->address ;

14 What is Object Oriented Design in PHP? Write  a simple class in PHP.
An object  in PHP classes  has following properties::
  • Abstract data types and information hiding
  • Inheritance
  • Polymorphism
Example:
<?php

class Something {
// In OOP classes are usually named starting with a cap letter.
var $x;

function setX($v) {
// Methods start in lowercase then use lowercase to separate
// words in the method name example getValueOfArea()
$this->x=$v;
}

function getX() {
return $this->x;
}
}

?>

15. Using php to produce pdf file.
We can use R&OS Ltd's free PHP PDF creation library (classpdf). The library has two advantages over other approaches: it's free and it doesn't require any additional PHP configuration. What's more, it's powerful and you can do most things you need, including producing tables containing results from database queries and inserting images into a document.
You can find out more about classpdf (php-pdf,class.ezpdf.php, class.pdf.php) from http://www.ros.co.nz/pdf and you can download the source, documentation, and get involved in the project at http://sourceforge.net/projects/pdf-php/. Instructions for installing classpdf are included in Appendix A through Appendix C.

16. Replace string and ignore html tag in php.
//Replace < with a blank space
$header = str_replace('<',' ',$header);
//ignore all html tags in string $body
$body = strip_tags($body); 
free PHP script download site


17. How to include PHP file  in html?
<html>

<?
include ('header.html');
include ('phpfilename.php');

?>
</html>

1 comment:

  1. Thank you so much for illustrating this very nice information with us, i am looking forward for more useful topic soon. Best Vancouver Mortgage Broker

    ReplyDelete