Thursday, June 18, 2009

Php Interview Question

1.what is Register_global.
Whether or not to register the EGPCS (Environment, GET, POST, Cookie, Server) variables as global variables. register globals allows you to have $_GET['test'] and $_POST['test'] equate to $test.

2. How many type of error in php.
notice, warning, fatal error.

3.What is abstraction.
a concept or idea not associated with any specific instance,
Php 5 introduce a abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation..
When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private.

4. Interface and Abstract Class.
Abstract class have a abstract method keyword its inherit with child class with extends keyword

Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

A class cannot implement two interfaces that share function names, since it would cause ambiguity.

Interfaces are a common set of methods those can be implemented across various types of classes dealing with various objectives. In case of an abstract class, its something like a factory containing set of a process.

5.Pattern in PHP.
abstract factory : set of method to make vaious object.
builder : make and return one object various ways.
factory method: methods to make and return components of one object various ways.
prototype: make a object by the cloning of object which u said as a prototype.
singletone : the class distributes the class of itself.

6. find the difference between two dates
$date1 = "2007-03-24";
$date2
= "2009-06-26";

$diff
= abs(strtotime($date2) - strtotime($date1));
//abs absolute value

$years
= floor($diff / (365*60*60*24));
$months
= floor(($diff - $years * 365*60*60*24) / (30*60*60*24));
$days
= floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));

printf
("%d years, %d months, %d days\n", $years, $months, $days);

7.What is Exception Handling.
With php 5 came a new object oriented way to dealing with error.
Exception Handling is used to change the normal flow of code execution if a specified
error is occur.

This is what normally happens when an exception is triggered:

  • The current code state is saved
  • The code execution will switch to a predefined (custom) exception handler function.
  • Depending on the situation, the handler may then resume the execution from the saved code state, terminate the script execution or continue the script from a different location in the code.

We will show different error handling methods:

  • Basic use of Exceptions
  • Creating a custom exception handler
  • Multiple exceptions
  • Re-throwing an exception
  • Setting a top level exception handler

Note: Exceptions should only be used with error conditions, and should not be used to jump to another place in the code at a specified point.


function checkNumber($number){
if($number>1){
throw new Exception ("Value must be 1 or less then 1");
}
return true;
}
try{
checkNumber(2);
print "the try message is display";
}
catch(Exception $number){
echo "Message".$number->getMessage();
}

8.How validate email address except regular expressions.

filter_var($email, FILTER_VALIDATE_EMAIL);

function ($email){

if(!filter_var($email, FILTER_VALIDATE_EMAIL)){

print $email. "is not valid email id";

}

else{

print "email valid";

}


No comments: