PHP MCQs
PHP MCQs
PHP MCQs
2. <?php
3. $y = 2;
4. if ($y-- == ++$y)
5. {
6. echo $y;
7. }
8. ?>
A. 2
B. 1
C. 3
D. no output
Answer: Option A
1. <?php
2. $x = 4;
3. $y = -3;
4. $z = 11;
5. echo 4 + $y * $z / $x;
6. ?>
A. 4.25
B. 3.25
C. -3.25
D. -4.25
Answer: Option D
Answer: Option C
4. Which of the following php statement/statements will store 111 in variable num?
i. int $num = 111;
ii. int mum = 111;
iii. $num = 111;
iv. 111 = $num;
A. Both (i) and (ii)
B. All of the mentioned.
C. Only (iii)
D. Only (i)
Answer: Option C
Answer: Option B
Answer: Option C
Answer: Option D
8. What will be the output of the following PHP code?
< ?php
$color = "maroon";
$var = $color[2];
echo "$var" ;
?>
A. a
B. Error
C. $var
D. r
Answer: Option D
Answer: Option A
Answer: Option A
11. Which of the following variables does PHP use to authenticate a user?
i) $_SERVER['PHP_AUTH_USER'].
ii) $_SERVER['PHP_AUTH_USERS'].
iii) $_SERVER['PHP_AUTH_PU'].
iv) $_SERVER['PHP_AUTH_PW'].
A. and ii)
B. and iv)
C. and iv)
D. and iii)
Answer: Option C
1. <?php
2. $cars = array("Volvo", "BMW", "Toyota");
3. echo "I like " . $cars[2] . ", " . $cars[1] . " and " . $cars[0] . ".";
4. ?>
Answer: Option D
1. class Person
2. {
3. function getName() { return "Bob"; }
4. function getAge() { return 44; }
5. function __toString() {
6. $desc = $this->getName();
7. $desc .= " (age ".$this->getAge().")";
8. return $desc;
9. }
10. }
11. $person = new Person();
12. print $person;
Answer: Option C
14. What will be the output of the following PHP code?
1. <?php
2. $a1 = array_fill(1, 4, "hello");
3. $b1 = array_fill(5, 1, "php");
4. $a2 = array_merge($a1, $a2);
5. print_r($a2);
6. echo "<br>";
7. print_r($b1);
8. ?>
A. Array ( [1] => hello [4] => hello [5] => php )
Array ( [5] => php )
B. Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [5] => php )
C. Array ( [1] => hello [2] => hello [3] => hello [4] => hello [5] => php )
Array ( [5] => php )
D. Array ( [1] => hello [2] => hello [3] => hello [4] => hello )
Array ( [1] => php )
Answer: Option C
1. <?php
2. $names = array("Sam", "Bob", "Jack");
3. echo $names[0]."is the brother of ".$names[1]." and ".$names[1].".".$brother;
4. ?>
Answer: Option D
https://www.sanfoundry.com/1000-php-questions-answers/
16. What will be the value of $a and $b after the function call in the following PHP code?
1. <?php
2. function doSomething( &$arg ) {
3. $return = $arg;
4. $arg += 1;
5. return $return;
6. }
7. $a = 3;
8. $b = doSomething( $a );
9. ?>
A. a is 3 and b is 4
B. a is 4 and b is 3
C. Both are 3
D. Both are 4
Answer: Option B
A. mango
B. Error
C. peach
D. 0
Answer: Option B
18. Which function will return true if a variable is an array or false if it is not?
A. this_array()
B. is_array()
C. do_array()
D. in_array()
Answer: Option B
Answer: Option D
1. <?php
2. $x = 75;
3. $y = 25;
4. function addition()
5. {
6. $GLOBALS['z'] = $GLOBALS['x'] + $GLOBALS['y'];
7. }
8. addition();
9. echo $z;
10. ?>
A. 100
B. error
C. 75
D. 25
Answer: Option A
A. AshleyBaleBlank
B. AshleyBale
C. AshleyBaleShrek
D. No output
Answer: Option A
1. <?php
2. for ($i = 0; $i % ++$i; $i++)
3. {
4. print"i";
5. }
6. ?>
A. error
B. infinite loop
C. no output
D. 0
Answer: Option B
1. <?php
2. $color1 = "red";
3. $color2 = "1";
4. echo "$color1" + "$color2";
5. ?>
A. red1
B. red 1
C. 0
D. 1
Answer: Option D
A. <?php
B. $i = 0;$j = 1;$k = 2;
C. print !(( + + $i + $j) > ($j - $k));
D. ?>
A. 1
B. no output
C. error
D. 0
Answer: Option B
1. <?php
2. $a = 'a' ;
3. print $a * 2;
4. ?>
A. 192
B. 2
C. error
D. 0
Answer: Option D
27. Which of the following method sends input to a script via a URL?
A. Get
B. Post
C. Both
D. None
Answer: Option A
28. Which of the following function returns a text in title case from a variable?
A. ucwords($var)
B. upper($var)
C. toupper($var)
D. ucword($var)
Answer: Option A
29. Which one of the following functions finds the last occurrence of a string, returning its
numerical position?
A. strlastpos()
B. strpos()
C. strlast()
D. strrpos()
Answer: Option D
30. Which one of the following regular expression matches any string containing zero or one
p?
A. p+
B. p*
C. P?
D. p#
Answer: Option C
31. What will be the output of the following PHP code?
1. <?php
2. echo ord ("hi");
3. ?>
A. 106
B. 103
C. 104
D. 209
Answer: Option C