Friday 10 April 2015

PHP Interview Questions and Answers for 2 year Experience

PHP Interview Questions and Answers for 2 year Experience



Question: What is use of header() function in php?
1. Header is used to redirect from current page to another:
header("Location: newpage.php");

2. Header is used to send HTTP status code.
header("HTTP/1.0 404 Not Found");

3. Header is used to send Send a raw HTTP header
header('Content-Type: application/pdf');



Question: What type of inheritance supports by PHP?
There are following type of inheritance
Single Inheritance - Support by PHP
Multiple Inheritance - Not support
Hierarchical Inheritance - Support by PHP
Multilevel Inheritance - Support by PHP


Question: How do you call a constructor for a parent class?
parent::constructor($value);



Question: What is the difference between the functions unlink and unset?
unlink: It is used to remove the file from server.
unlink('/path/file.phtml');

unset: It is used to remove the variable.
unset($variableName);



Question: What are default session time and path?
Session Time: 1440 seconds
Session Path: /tmp folder in server


Question: What is PEAR?
PHP Extension and Application Repository (PEAR) is a framework and repository for reusable PHP components.


Question: What is MIME?
Full form of MIME is "Multi-purpose Internet Mail Extensions".
It is extension of e-mail protocol helps to exchanges the different kids of data files over the internet.
Data files may be audio, video, images, application programs and ASCII etc.


Question: How to scrape the data from website using CURL?
To scrap the data from website, Website must be public and open for scrapable.
In the blow code, Just update the CURLOPT_URL to which websites data you want to scrap.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://www.web-technology-experts-notes.in/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
curl_close($ch);
echo $output;



Question: How to upload the file using CURL?
You can upload a file using CURL.
See following points.
1. Uploading file size must be less than allowed file by Server.
2. If file size is heady, May take more time.
3. replace "uploadFile.zip" with file which you want to upload WITH full path.
4. replace "http://localhost/test/index2" with URL where server has given functionality to upload file.
$file_name_with_full_path = realpath('uploadFile.zip');        
       $url = "http://localhost/test/index2";
       $post_data = array(
           "foo" => "bar",
           "upload" => "@".$file_name_with_full_path
       );
       try {
           $ch = curl_init();
           curl_setopt($ch, CURLOPT_URL, $url);
           curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
           curl_setopt($ch, CURLOPT_POST, 1);
           curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
           $output = curl_exec($ch);
           curl_close($ch);
       } catch (Exception $e) {
           echo $e->getMessage();
           die;
       }



Question: Can I set the header in CURL?
Yes, you can set the header in CURL using CURLOPT_HEADER.
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: text/xml")); 



Question: How can i execute PHP File using Command Line?
For this, you need PHP CLI(Commnd line interface)
Just login to you command line interface.
You have to prepend the "PHP" and need to mention the full-path/relative of file
Execute the file in following way.
php E://wamp/www/project/myfile.php



Question: How can we get the current session id?
echo session_id();

You can also set the session_id using same above function.


Question: What are different type of sorting functions in PHP?
sort() - sort arrays in ascending order. asort() - sort associative arrays in ascending order, according to the value.
ksort() - sort associative arrays in ascending order, according to the key.
arsort() - sort associative arrays in descending order, according to the value.
rsort() - sort arrays in descending order.
krsort() - sort associative arrays in descending order, according to the key.
array_multisort() - sort the multi dimension array.
usort()- Sort the array using user defined function.


Question: How to save the session data into database?
To maintain the session data, we can use session_set_save_handler function.
session_set_save_handler ( 'openFunction' , 'closeFunction', 'readFunction' , 'writeFunction' , 'destroyFunction', 'gcFunction' )

In this function, we provide 6 callback functions which call automatically.
For Example
openFunction will be called automatically when session start.
closeFunction will be called automatically when session end.
readFunction will be called automatically when you read the session.
writeFunction will be called automatically when you write in the session.
destroyFunction will be called automatically when you destroy in the session.
gcFunction will be called automatically when session inactive for long time.







Question: What are different type of errors?
E_ERROR: A fatal error that causes script termination.
E_WARNING: Run-time warning that does not cause script termination.
E_PARSE: Compile time parse error.
E_NOTICE: Run time notice caused due to error in code.
E_CORE_ERROR: Fatal errors that occur during PHP's initial startup.
E_CORE_WARNING: Warnings that occur during PHP's initial startup.
E_COMPILE_ERROR: Fatal compile-time errors indication problem with script.
E_USER_ERROR: User-generated error message.
E_USER_WARNING: User-generated warning message.
E_USER_NOTICE: User-generated notice message.
E_STRICT: Run-time notices.
E_RECOVERABLE_ERROR: Catchable fatal error indicating a dangerous error E_ALL: Catches all errors and warnings