PHP objective practice questions for interviews. These output questions test how well you understand PHP and it’s programming methodology.
PHP Objective Practice Questions
Q1. What’s the output of following code.
1 2 3 |
$a = 1; $a = $a-- +1; echo $a; |
a) 1
b) 2
c) 0
d)-1
Q2.
1 2 |
$array = array(true=>'1',1=>'b'); var_dump($array); |
a) array(1) { [1]=> string(1) “b” }
b) array(1) { [1]=>1 ,[2]=> string(1) “b” }
c) array(1) { true=>1 ,[1]=> string(1) “b” }
d) None
Q3.
1 2 3 4 5 6 7 8 |
$array = array(1,2,3,5,8,13,21,34,55); $sum = 0; for($i=0;$i<5;$i++){ $sum +=$array[$array[$i]]; } echo $sum; |
a) 19
b) 5
c) NULL
d) 78
Q4. Which datatype will $a have at the end of the script
1 2 |
$a = "1"; var_dump($a); |
a) integer
b) string
c) char
d) bool
Q5. What’s the output ?
1 2 |
$result = 5 - 3 % 2; echo $result; |
a) 0
b) 3
c) 4
d) 2
Q6. How to set Cookie in PHP. Choose the correct one.
a) cookie(name,value);
b) setcookie(name,value);
c) $cookie(name,value,time);
d) All of the above
Q7. Which of the following statement are correct.
a) time() + 60*60*100 returns the current date and time plus two hour.
b) time() + 60*60 returns the current date and time plus one hour.
c) time() + 24*60*60 returns the current date and time plus one day.
d) Both b and c.
Q8.
1 2 |
$names = array ("john", "nathan", array ("vikash", "ankit"),"ankesh"); echo (count($names,1)); |
a) 4
b) 6
c) 5
d) 3
Answers
Q1. b) 2
Explanation
1 2 3 4 5 6 7 8 9 10 11 |
/* Initial value of a is 1. */ $a = 1; /* First value of $a is assigned and then decrement. $a = 1 + 1; */ $a = $a-- +1; // $a = 1 + 1; echo $a; // Final value is 2 |
Q2. a) array(1) { [1]=> string(1) “b” }
Q3. d) 78
Q4. b) string
Q5. c) 4
Q6. b) setcookie(name,value);
Read explanation of How to read,set,delete cookie in PHP .
Q7. d) Both b and c.
time() returns current Unix Timestamp. So when we add 60*60 then it’s current date and time plus one hour, similarly for c.
Q8 b) 6
If mode parameter in count is set to true, count() will recursively count the array.