Friday 22 May 2015

PHP interview questions and answers for 5 year experienced

PHP interview questions and answers for 5 year experienced


Question: How array_walk function works in PHP?
Purpose: It is used to update the elements/index of original array.
How: in array_walk, two parameter are required.
1st: original array
2nd: An callback function, with use of we update the array.



Question: Difference Between Zend framework 2 VS Zend framework 1?
http://www.web-technology-experts-notes.in/2014/03/zend-framework-2-vs-zend-framework-1.html


Question: How to achieve Multilevel inheritance in PHP?
//base class
class grandParent{}

//parent class extend the base class
class parent extends grandParent{}

//chid class extend the parent class
class child extends parent{}



Question: What is Routing? How it works in Zend?
Routing is the process of taking a URI endpoint and decomposing it into parameters to determine which module, controller and action to that controller should receive the request.
This values of the module, controller, action and parameters are packaged into a Zend_Controller_Request_Http object which is then processed by Zend_Controller_Dispatcher_Standard.




Question: What is Zend_Controller_Front?
Front Controller is design pattern.
Zend framework use the Zend_Controller_Front which is based on Front controller design pattern. Zend_Controller_Front purpose is to initialize the request environment, route the incoming request and then dispatch any discovered actions.
It aggregates any responses and returns them when the process is complete.


Question: What is difference between TDD and BDD?
TDD is Test-driven development
BDD is Behaviour-driven development



Question: What is Zend_Translate? Where it is used?
It is component of Zend Framework. Responsible for converting the text data from one language to another languages.
It is used in multilingual website where we are showing content display in multilingual.


Question: What are different Adapter of zend_translate?
Following are main Adapter of zend translator
Zend_Translate_Adapter_Array
Zend_Translate_Adapter_Csv
Zend_Translate_Adapter_Gettext
Zend_Translate_Adapter_Ini
Zend_Translate_Adapter_Tbx
Zend_Translate_Adapter_Tmx
Zend_Translate_Adapter_Qt
Zend_Translate_Adapter_Xliff
Zend_Translate_Adapter_XmlTm


Question: How to get 2nd highest salary of employee, if two employee may have same salary?
select salary from employee group by salary order by salary limit 1,1



Question:How to find duplicate email records in users table?
SELECT u1.first_name, u1.last_name, u1.email FROM users as u1
INNER JOIN (
    SELECT email FROM users GROUP BY email HAVING count(id) > 1
    ) u2 ON u1.email = u2.email;



Question: How to pass data in header while using CURL?
$url='http://www.web-technology-experts-notes.in';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
  'headerKey: HeaderValue ',
  'Content-Type: application/json',
  "HTTP_X_FORWARDED_FOR: xxx.xxx.x.xx"
));
echo curl_exec($ch);
curl_close($ch);



Question: How to pass JSON Data in CURL?
$url='http://www.web-technology-experts-notes.in';
$jsonData='{"name":"Web Technology Experts","email":"contact@web-technology-experts-notes.in"}';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);
curl_close($ch);



Question: What is Final Keyword in PHP?
PHP introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final keyword.


Question: What are Properties of Object Oriented Systems?
Inheritance
Encapsulation of data
Extensibility of existing data types and classes
Support for complex data types
Aggregation
Association



Question: How can we prevent SQL-injection in PHP? Sanitize the user data before Storing in database.
While displaying the data in browser Convert all applicable characters to HTML entities using htmlentities functions.



Question: How to redirect https to http URL and vice versa in .htaccess?
Redirect https to http
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Redirect http to https
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]



Question: What are benefits of htaccess?
We can do following benefits stuff with htaccess.
Routing the URL
Mange Error Pages for Better SEO
Redirection pages
Detect OS (like Mobile/Laptop/Ios/Android etc)
Set PHP Config variable
Set Environment variable
Allow/Deny visitors by IP Address
Password protection for File/Directory
Optimize Performance of website
Improve Site Security

Question: What is Reference Transaction in Paypal?
In Reference transaction,
With use of first transaction done by customer, we can do mutiple transaction without need another approvel.
In this, we store the transaction_id(First transaction), and use this transaction_id as reference_id for net payments.

It has two methods.
1) DoDirect payment. (Accept the credit card by website)
2) Express Checkout. (website doesn't accept credit card in own website, customer pay on paypal.com)
Click to Know More



Question: What is the validate a credit card format in PHP?
MASTERCARD Prefix=51-55, Length=16
VISA Prefix=4, Length=13 or 16
AMEX Prefix=34 or 37, Length=15
Diners Club/Carte Prefix=300-305, 36 or 38, Length=14
Discover Prefix=6011,622126-622925,644-649,65, Length=16

PHP Function to validate
function validateCreditCardFormat($number) {
    //Only number will accept 
    $number = preg_replace('/\D/', '', $number);

    // Set the string length and parity
    $number_length = strlen($number);
    $parity = $number_length % 2;

    // Loop through each digit and do the maths
    $total = 0;
    for ($i = 0; $i < $number_length; $i++) {
        $digit = $number[$i];        
        if ($i % 2 == $parity) {
            $digit*=2;            
            if ($digit > 9) {
                $digit-=9;
            }
        }
        
        $total+=$digit;
    }
    if($total%10==0){
        return true;
    }else{
        return false;
    }    
}
var_dump(validateCreditCardFormat('5105105105105100')); //true
var_dump(validateCreditCardFormat('4111111111111111'));//true
var_dump(validateCreditCardFormat('4012888888881881'));//true
var_dump(validateCreditCardFormat('40128888'));//false



Question: Difference between Apache and Nginx?
Nginx is based on event-driven architecture.
Apache is based on process-driven architecture.

Nginx development started only in 2002.
Apache initial release was in 1995.

Nginx doesn't create a new process for a new request.
Apache creates a new process for each request.

In Nginx, memory consumption is very low for serving static pages.
Apache’s nature of creating new process for each request, that's why it  increases the memory consumption.

Nginx is extremely fast for serving static pages as compare to Apache.

In complex configurations apache can be configured easily as compare to Nginx.

Apache has excellent documentation as compare to Nginx.

Nginx does not support Operating Systems like OpenVMS and IBMi where as Apache supports much wider range of Operating Systems.

The performance and scalability of Nginx is not completely dependent on hardware resources.
The Performance and scalability of the Apache is dependent on underlying hardware resources like memory and CPU.


Question: When to use self over $this?
Use $this to refer to the current object.
Use self to refer to the current class.

Example of use of $this
class MyClass1 {
    private $nonStaticMember = 'nonStaticMember  member Called.';        
    
    public function funcName() {
        return $this->nonStaticMember;
    }
}

$classObj = new MyClass1();
echo $classObj->funcName();//nonStaticMember  member Called.


Example of use of self
class MyClass2 {
    private static $staticMember = 'staticMember  member Called.';        
    
    public function funcName() {
        return $this::$staticMember;
    }
}

$classObj = new MyClass2();
echo $classObj->funcName(); //staticMember  member Called.