PHP Interview Questions And Answers

It is really important to get a good preparation before any interview. In case of PHP interview or exam it is highly recommended to read the important topics from PHP manual. The basic PHP knowledge is vital for PHP interview or exam. Once you have the basic knowledge of PHP you can start looking into some PHP interview questions to check your expertise.

I have came up with few PHP interview questions with answers that might help you in your exam

Q1. What will be the result if we add the following two arrays

$malePopStars   = array('Justin Bieber', -0.0 =>'Justin Timberlake', 'Bruno Mars');
$femalePopStars = array(0.1 => 'Selena Gomez', 1.5 => 'Taylor Swift', 0.09 => 'Katy Perry');
print_r($femalePopStars + $malePopStars);
a) array()
b) array('Justin Bieber', 'Katy Perry')
c) array('Katy Perry', 'Taylor Swift')
d) array('Justin Timberlake', 'Bruno Mars')
e) array('Justin Bieber', 'Selena Gomez')

Answer of Q1: 

c) array('Katy Perry', 'Taylor Swift')


Q2. Which of the following is not a valid PHP variable?

a) ${float}
b) @$variable;
c) ${1}{'_ff'}
d) $5lines
e) ${2 + 2.9}


Answer of Q2: 

d) $5lines


Q3. What will be the output of the following code segment?

class Movies{
   public static function getBestMovie(){
      return static::getMovieName();
   }
   public static function getMovieName(){
      return 'Shawshank Redemption';
   }
}   
class Theater extends Movies{
   public static function getMovieName(){
      return 'The Godfather';
   }
}
echo Theater::getBestMovie();
a) Fatal Error
b) NULL
c) Shawshank Redemption
d) The Godfather
e) Shawshank Redemption, The Godfather

Answer of Q3: 

d) The Godfather


Q4. What is the value of the following expression if $value = 5 ?

(ord($value) & 1) + $value;
a) 1
b) 0
c) 5
d) 6
e) 10

Answer of Q4: 

d) 6


Q5. What will be the output of the following code segment?

$apple = 10;
function getFruits(&$fruit) {
   global $apple;
   $apple = 20;
   $result = $fruit + $apple;
   return $result++;
}
echo getFruits($apple);
a) 21
b) 30
c) 31
d) 40
e) 41

Answer of Q5: 

d) 40


Q6. What will be the output of the following code segment?

function getWidth($width)
{
  $widthRef = &$width;  
  return (++$widthRef * $width);
}
echo getWidth(7);
a) 0
b) 7
c) 49
d) 56
e) 64

Answer of Q6: 

e) 64


Q7. What will be the output of the following code segment?

function checkReference(&$param){
  $copyVar = &$param;
  $param  += 1;
  return $copyVar;
}
$initialValue = 100;
checkReference($initialValue);
echo $initialValue;
a) 100
b) 101
c) null
d) false
e) 102

Answer of Q7: 

b) 101


Q8. What will be the name field if we submit the following form?

<form>
   <input name="first.name" />
   <input type="submit">
 </form>
a. $_REQUEST['first.name']
b. $_REQUEST['first_name']
c. $_REQUEST['first&name']
d. $_REQUEST['first']['name']

Answer of Q8:

b. $_REQUEST['first_name']



Q9. What is the size limit of http GET method?


a. 2000
b. 3000
c. 5000
d. No limit as of PHP 5.4.0

Answer of Q9:

a. 2000


Q10. What the following line will print?

echo "2 little kitty" + "1 little kitty";
a. 3 little kitty
b. 0
c. 3
d. Strict standard: illegal access of operator

Answer of Q10:

c. 3


Q11. What will be the output of the following code sinppet?

class Car
{
  public function wheel(){
     $obj1 = new self;
     $obj2 = new static;
     echo ($obj1 == $obj2) ? true : false;
  }
}
$obj = new Car();
$obj->wheel();
a. true
b. false
c. Warning: static cannot be used with new operator
d. Strict standard: illegal access of operator

Answer of Q11:

a. true


Q12. What will be the output of the following code sinppet?

class B extends A{
}
class A
{
  function checkClass(){
    echo get_class($this);
    echo get_class();
    echo static::class;
    echo self::class;
  }
}
$a = new B();
$a->checkClass();
a. ABAB
b. BABA
c. BBBA
d. BBAB

Answer of Q12:

b. BABA


Q13. What will be the value of the following expression?

echo "\061";
a. \061
b. 61
c. 1
d. 061

Answer of Q13:

c. 1


Q14. What is the maximum string size in PHP?


a. 1GB
b. 2GB
c. 3GB
d. 4GB

Answer of Q14:

b. 2GB


Q15. What will be the value of the following variable?

$country[$index == 0] = 'US';
print_r($country);

a. array([1] => US)
b. array([0] => US)
c. array()
d. Fatal Error

Answer of Q15:

a. array([1] => US)


Q16. What will be the value of the following expression?

$precedence = true AND false;
echo $precedence;

a. flase
b. true
c. Null
d. Fatal Error

Answer of Q16:

b. true


Q17. What will be the value of the following code snippet?

$serial = '4302621';
$serial[$serial[2]] = 99;
echo $serial;
a. 4392621
b. 4302621
c. 9302621
d. 43992621

Answer of Q17:

c. 9302621


Q18. What will be the value of the following code snippet?

interface SmartPhone {
  public function showAppList();
}
class Phone implements SmartPhone{
  public function showAppList(){
  }
}
$obj = new Phone;
var_dump($obj instanceof SmartPhone);
a. bool(true)
b. bool(false)
c. Fatal error: instanceof cannot be used with interface
d. none of the above

Answer of Q18:

a. bool(true)


Q19. What it will show if null is checked with instanceof operator?

var_dump(null instanceof stdClass);
a. bool(true)
b. bool(false)
c. Fatal error: instanceof expects an object instance, constant given
d. none of the above

Answer of Q19:

c. Fatal error: instanceof expects an object instance, constant given


Q20. What will be the value of following code segment?

function increase() {
  static $c = 0;
  $c = $c + 1;
  return ord($c) & 1;
}
echo increase() . ' & ' . increase();
a. 1 & 1
b. 1 & 2
c. 1 & 0
d. 49 & 50

Answer of Q20:

c. 1 & 0


Q21. What will be the value of following string?

$var1 = "foo";
$var2 = "bar";
echo "Hello ${var1}\{$var2}";
Escape sequences needs to be considered here.

a. Hello foobar
b. Hello foo\bar
c. Hello foo\{bar}
d. Hello foo{bar}

Answer of Q21:

c. Hello foo\{bar}


Q22. Consider the following code segment and find out the right syntax to access the object property?

$carModel = array('5423100' => 'BMW', '0055X56' => 'Tesla');
$obj      = (object)$carModel;
a. $obj['0055X56']
b. $obj->0055X56
c. $obj->'0055X56'
d. $obj->{'0055X56'}

Answer of Q22:

d. $obj->{'0055X56'}


Q23. What will be the summation if getSummation function is called with value 100?

function getSummation($num){
  return $num + increase($num);
}
function increase(&$var){
  ++$var;
}
echo getSummation(100);
a. 201
b. 202
c. 100
d. 101

Answer of Q23:

d. 101


Q24. What the following code segment will return if we call doMath function?

function doMath() {
  $array = array_map('sqrt', func_get_args());
  return $array[func_get_arg(1)];
}
echo doMath(2, 3, 4, 9);
a. 2
b. 3
c. 4
d. 9

Answer of Q24:

b. 3


Q25. What will be the output of the following code block?

function getMacbookCount(&$mac = null){
  $mac = $mac + 'M' . $mac;
}
$laptop = 100;
getMacbookCount($laptop);
echo $laptop;
a. 100
b. 100100
c. 200
d. M100

Answer of Q25:

b. 100100


Q26. What the following code will show when the recursion function is called?

function animal($animalArr){
  static $animalStr;
  if(count($animalArr) >= 1){
    $animalStr .= array_pop($animalArr);
    animal($animalArr);
  }
  else{
    return $animalStr;
  }
}
var_dump(animal(array("Dog", "Cat", 'Cow')));
a. NULL
b. CowCatDog
c. DogCatCow
d. Fatal error: static can not be used inside recursion

Answer of Q26:

a. NULL


Q27. What will be the output of the following recursion function?

function animal($animalArr){
  static $animalStr;
  if(count($animalArr) >= 1){
    $animalStr .= array_shift($animalArr);
    return animal($animalArr);
  }
  else{
    return $animalStr;
  }
}
var_dump(animal(array("Dog", "Cat", 'Cow')));
a. NULL
b. CowCatDog
c. DogCatCow
d. Fatal error: static can not be used inside recursion

Answer of Q27:

c. DogCatCow


Comments