Interview Questions and Answers for Freshers

February 16th, 2013 by Laeeq | No comments

Here are common questions with answer that every fresher PHP programmer is expected to know, I will add few more questions with answers in coming days.

Q- What is the difference between include and require

Difference between require() and include() is that require() produces a FATAL ERROR if the file you want to include is not found, while include() only produces a WARNING.

Q- What is the difference between Session and Cookie

Sessions are stored in the server side whereas cookies are stored in the client side.

Q- How do you create a cookie, add data to it, and remove data from a cookie

Here we have created cookie “username” and assign value “phpzag” with expiration duration of 10 seconds to the timestamp.

setcookie(‘username’, ‘phpzag’, time()+10);

Now we get value of cookie “username”

echo $_COOKIE['username'];

Now we remove the data of cokie. When deleting a cookie you should assure that the expiration date is in the past. Examples follow show how to delete cookies sent in previous example:

setcookie (“username”, “”, time() – 10);

Q- How do you create a session, add data to a session and remove data from a session

First we start session by inintialzing session_start();

Now we will create session “USERNAME” and add data to our created session.

$_SESSION['USERNAME']=”phpzag”;

Now we get the data from our session:

$session_data=$_SESSION['USERNAME'];

Q- If cookies are disabled will the sessions work? Why?

Session will work if cokkies are disabled. If cookie is enabled on client machine then php will use cookies to handle sessions else if cookie is disabled on client machine check is made to ensure that if php5 is configured with

–enable-trans-sid and
–enable-track-vars option.

If yes then php will pass session id using post method automatically and we do not need to worry about it. we just have to use sesssion functions regularly. If No then we can pass session id using SID.

Q- What is the difference between $var and $$var

$var is a variable and $$variable is a Reference or Dynamic variable.

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

  • How much memory do PHP variables use?
  • Manipulating PHP arrays: push, pop, shift, unshift
  • 10 Super Useful PHP Code Snippets
  • Let’s play with php.ini file Configuration
  • 15+ regular expressions for PHP developers
  •  

     

    1. No comments yet.
    1. No trackbacks yet.