PHP Online Test - MCQ Questions

Welcome to the PHP Programming Online Test! Here, we will present 35+ MCQs (Multiple-Choice Questions) containing code snippets to test your PHP basics, coding, and logical skills.

You can select the correct answer for each question and submit the test. You will get your online test score after finishing the complete test.

1. What does the PHP echo statement do?

a) Outputs one or more strings
b) Returns one or more strings
c) Saves one or more strings to a file
d) Converts integers to strings

2. Which superglobal variable is used to collect form data sent via GET in PHP?

a) $_GET
b) $_POST
c) $_REQUEST
d) $_SESSION

3. How do you create a function in PHP?

function sayHello() {
    echo "Hello!";
}
sayHello();
a) function sayHello() { echo "Hello!"; }
b) create sayHello() { echo "Hello!"; }
c) new function sayHello() { echo "Hello!"; }
d) method sayHello() { echo "Hello!"; }

4. What is the correct way to include a file in PHP?

a) include "file.php";
b) #include "file.php";
c) require "file.php";
d) Both a) and c) are correct

5. What does the isset() function check?

a) If a variable is of type SET
b) If a variable has been set and is not NULL
c) If a set contains a specific value
d) If a variable is an array

6. Which of the following is the correct way to start a session in PHP?

a) session_start();
b) session_begin();
c) start_session();
d) begin_session();

7. How do you send a user to a new page in PHP?

a) header("Location: http://www.newpage.com");
b) redirect("http://www.newpage.com");
c) goto("http://www.newpage.com");
d) move("http://www.newpage.com");

8. What is the purpose of the explode() function in PHP?

a) To combine array elements into a string
b) To split a string by a delimiter into an array
c) To search for a string inside another string
d) To destroy a variable

9. How do you declare a constant in PHP?

a) const CONSTANT = 'value';
b) define('CONSTANT', 'value');
c) CONSTANT = 'value';
d) Both a) and b) are correct

10. Which function can be used to parse a string containing JSON in PHP?

a) json_encode()
b) json_decode()
c) json_parse()
d) parse_json()

11. What is the result of this PHP function?

$x = "Hello";
if (isset($x)) {
    echo "Variable is set";
    } else {
        echo "Variable is not set";
    }
a) Variable is set
b) Variable is not set
c) Error
d) None

12. Which function is used to find the number of elements in an array?

a) length()
b) count()
c) size()
d) sizeof()

13. What does the file_get_contents() function do in PHP?

a) Writes content to a file
b) Reads content from a file into a string
c) Deletes content from a file
d) Copies content from one file to another

14. How do you add elements to an associative array in PHP?

a) $array[] = 'value';
b) $array('key' => 'value');
c) $array['key'] = 'value';
d) add($array, 'key', 'value');

15. What does the following PHP code snippet output?

$number = 123;
printf("Value: %f", $number);
a) Value: 123
b) Value: 123.000000
c) Value: %f
d) Error

16. What will the following PHP code output?

echo 10 + "20 apples";
a) 30
b) 20 apples
c) 1020
d) Error

17. What does the following PHP code print?

$x = 5;
$y = $x++;
echo $y;
a) 6
b) 5
c) 1
d) Error

18. What will the following PHP code output?

$a = '1';
$b = &$a;
$b = "2$b";
echo $a;
a) 1
b) 2
c) 12
d) 21

19. What is the result of the following PHP expression?

echo intval('01') + intval('02');
a) 3
b) 2
c) 102
d) 12

20. What does this PHP code output?

$a = true;
$b = false;
echo ($a and $b);
a) 1
b) 0
c) true
d) false

21. What will the following PHP snippet print?

$str = "Hello, World!";
echo substr($str, 7);
a) World!
b) Hello, W
c) Hello,
d) Hello, World!

22. What is the output of the following PHP code?

function test() {
    static $count = 0;
    $count++;
    echo $count;
}
test();
test();
test();
a) 111
b) 123
c) 333
d) 000

23. What does the following PHP code print?

$x = "7 people";
$y = "3 people";
echo $x - $y;
a) 4
b) 10
c) 7 people3 people
d) Error

24. What will be output by the following PHP code?

echo 014 + 2;
a) 16
b) 14
c) 12
d) 8

25. What is the result of this PHP code?

echo "12 cats" + "3 dogs";
a) 15
b) 123 dogs
c) 12 cats3
d) Error

26. What will the following PHP function output?

function foo($num) {
    return $num * 10;
}
echo foo(5);
a) 50
b) 5
c) 10
d) Error

27. What is the output of the following PHP code?

$var = "PHP";
$var[0] = "J";
echo $var;
a) PHP
b) JHP
c) JP
d) Error

28. What does the following PHP code return?

$x = array("a" => "apple", "b" => "banana");
echo isset($x['a']);
a) 1
b) apple
c) true
d) false

29. What is the output of this PHP program?

function add(&$value) {
    $value += 5;
}
$num = 2;
add($num);
echo $num;
a) 2
b) 5
c) 7
d) Error

30. What will the following PHP code snippet output?

$x = 5;
echo $x++ + ++$x;
a) 11
b) 12
c) 13
d) 10

31. What does this PHP code print?

$x = "Hello";
$y = &$x;
$y .= " World";
echo $x;
a) Hello
b) World
c) Hello World
d) Error

32. What is the outcome of the following PHP code?

echo strpos("Hello world!", "world");
a) 6
b) 7
c) true
d) false

33. What will this PHP code output?

$x = array("red", "green", "blue");
array_pop($x);
array_pop($x);
echo end($x);
a) red
b) green
c) blue
d) Error

34. What does the following PHP code return?

$x = 10;
$y = '10';
echo $x === $y;
a) 1
b) true
c) false
d) Error

35. What is the result of executing the following PHP code?

echo count(null);
a) 0
b) 1
c) null
d) Error

36. What will the following PHP code snippet output?

function multiply($a, $b) {
    return $a * $b;
}
echo multiply(5, "3 pigs");
a) 15
b) 5
c) 3 pigs
d) Error

37. What is the output of the following PHP code?

$fruit = array('a' => 'apple', 'b' => 'banana');
$fruit = array_reverse($fruit);
print_r($fruit);
a) Array ( [a] => banana [b] => apple )
b) Array ( [b] => banana [a] => apple )
c) Array ( [b] => apple [a] => banana )
d) Array ( [a] => apple [b] => banana )

38. What will be the output of the following PHP program?

$x = "10" + 20;
echo gettype($x);
a) integer
b) double
c) string
d) null

39. What does this PHP code output?

function test() {
    static $count = 0;
    $count++;
    echo $count;
}
test();
test();
test();
a) 1
b) 2
c) 3
d) 123

Comments