Basic Interview Questions in PHP

5 Interview questions on PHP you should know if you have mentioned PHP in your resume before going for Interviews. These are the basic but important questions and it is most frequently asked in interviews. So you should be very clear in those concepts.

1) Sessions Variables 

a) Definition: Sessions variables are used to store user information on the internet temporarily so that this user information can be used on multiple pages of the website.

b) Why do we need it? : As the Web server doesn't maintain a state so it doesn't know who you are and so it needs some variable to store your information.

c) What is the user information we are talking about ?: User information are mainly username, Password or any other data user is using on one page and can be used in other web pages as well of the same website.

d) How session variable work? : The session variable basically works by creating a unique session ID for users and then using this ID on different pages of the website. This ID is generally stored in cookies or propagated through the URL of the website

e) How to use session variables in PHP to store information? :

<?php
session_start();          // Start session
$_session['info']=1    // Store value or info in session variable
session_destroy();     // Destroy the session
<?
you can use $_session['info'] across multiple pages of website.

2) Cookie

a) Definition: A cookie is a small file with a user information server stored on the user browser so that each time the user request the same web page through that browser, the server will identify the user.

b) How is it different from the session? : It is different from the session in a way that it stores the user information in client-side such as browser but the session stores user information on the server. The cookie will remain stored in your browser until you delete your cookies but stored info in session will be lost as soon as you closed the website.

c) How to use cookies in PHP? :

setcookie(name,value,expire,path,domain)

<?php
setcookie("info", "123",time()+3600)
echo $_cookie['info'];
?>

Here expire is the time in which you want the cookie to get expire.

3) POST vs GET in PHP form handling

Both POST and GET are method to send information or values from form to other PHP page.

GET : Information sent from GET method is visible to everyone as it embeds values at the back of the URL so it should not be used for sending some secret information like username and password using GET. And also through GET method, you cannot send unlimited information using GET only 2000 characters are allowed to send. As the values are stored in the URL we can bookmark the page.

POST: Information sent through POST is invisible to everyone as it is embedded in the HTTP request body. There is no limit to sending information in POST. We cannot bookmark the page as the information is not stored in the URL.

4) Include vs Require in PHP

Both Include and require are used to include the content or code of the other PHP page on the given page.

Include: Include will give a warning if some fatal error occurs and continue the script. So if you want the output in the end-use include

Require: Require doesn't give a warning if some fatal error comes it stops the script then and there. So if you are making some complex PHP page or application it is advisable to use required.

5) PHP upload 

PHP upload is used to upload files on the server

A simple PHP script to upload the file1 to server displaying its name.type and size

HTML 

<form enctype="multipart/form-data"   action="upload.php"   method="post">
<input type="file" name="file1" id="file1">
<input type="submit" value="submit">
</form>

Here enctype="multipart/form-data" is used to show the type of file.

PHP

upload.php

<?php
if ($_FILES["file1"]["error"] > 0) {
  echo "Error: " . $_FILES["file1"]["error"] . "<br>";
} else {
  echo "Upload: " . $_FILES["file1"]["name"] . "<br>";
  echo "Type: " . $_FILES["file1"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file1"]["size"] / 1024) . " kB<br>";

}
?>

Also, See How to fetch documents from remote addresses using PHP and PHP array Functions

Comments

Popular posts from this blog

Tricky Questions or Puzzles in C

Program to uncompress a string ie a2b3c4 to aabbbcccc

Number series arrangement puzzles