1. What does PHP stand for?
a) Personal Home Page
b) Preprocessor Hypertext Page
c) PHP: Hypertext Preprocessor
d) Private Home Page
Ans: c) PHP: Hypertext Preprocessor
2. Which symbol is used to declare a variable in PHP?
a) @
b) #
c) $
d) %
Ans: c) $
3. Which of the following is a valid PHP variable name?
a) 2var
b) _varName
c) var-name
d) var name
Ans: b) _varName
4. PHP scripts are executed on the ____.
a) Client side
b) Server side
c) Both Client & Server side
d) None of the above
Ans: b) Server side
5. How do you start a PHP block in a file?
a) <?php ?>
b) <? ?>
c) <script> </script>
d) Both a & b
Ans: d) Both a & b
MCQs on Data Types & Operators
6. Which of the following is NOT a PHP data type?
a) String
b) Float
c) Integer
d) Character
Ans: d) Character
7. What is the output of echo 5 + "5"; in PHP?
a) 55
b) 10
c) Error
d) None of the above
Ans: b) 10
8. Which operator is used for concatenation in PHP?
a) +
b) . (dot)
c) &
d) , (comma)
Ans: b) . (dot)
9. What will be the output of echo (5 == "5") ? "True" : "False";?
a) True
b) False
c) Error
d) None of the above
Ans: a) True
10. Which operator is used to check if two variables are identical in both value and type?
a) ==
b) ===
c) =
d) !=
Ans: b) ===
11. Which of the following loops is used to execute a block of code a specific number of
times?
a) while
b) for
c) do-while
d) foreach
Ans: b) for
12. What is the correct syntax of an if statement in PHP?
a) if condition { }
b) if (condition) { }
c) if {condition}
d) None of the above
Ans: b) if (condition) { }
13. The while loop executes as long as the condition is ____.
a) False
b) True
c) Either True or False
d) None of the above
Ans: b) True
14. How many times is the do-while loop guaranteed to execute at least?
a) 0
b) 1
c) Unlimited
d) None of the above
Ans: b) 1
15. Which loop is best used for iterating over an array?
a) while
b) for
c) foreach
d) do-while
Ans: c) foreach
16. Which keyword is used to define a function in PHP?
a) function
b) define
c) method
d) fun
Ans: a) function
17. What is the correct way to return a value from a PHP function?
a) output(value)
b) return value;
c) send(value);
d) exit(value);
Ans: b) return value;
18. What will be the output of the following function?
function add($a, $b) {
return $a + $b;
}
echo add(2,3);
19. a) 5
b) 23
c) Error
d) None of the above
Ans: a) 5
20. In PHP, functions can return ____.
a) Only integers
b) Only strings
c) Any data type
d) No values
Ans: c) Any data type
21. A recursive function in PHP is a function that ____.
a) Calls itself
b) Loops infinitely
c) Calls another function
d) Cannot return a value
Ans: a) Calls itself
22. Which function is used to find the length of a string in PHP?
a) strlen()
b) str_length()
c) length()
d) str_len()
Ans: a) strlen()
23. Which function is used to replace a substring in a string?
a) replace()
b) str_replace()
c) substr_replace()
d) change_string()
Ans: b) str_replace()
24. What will be the output of echo strtoupper("hello world"); ?
a) HELLO WORLD
b) hello world
c) Hello World
d) Error
Ans: a) HELLO WORLD
25. Which function is used to split a string into an array?
a) split()
b) explode()
c) str_split()
d) Both b & c
Ans: d) Both b & c
26. What is the output of echo strrev("Hello"); ?
a) Hello
b) olleH
c) hELLO
d) None of the above
Ans: b) olleH
27. Which of the following is used to create an array in PHP?
a) array()
b) create_array()
c) new Array()
d) list()
Ans: a) array()
28. What is the default index of an indexed array in PHP?
a) 1
b) 0
c) -1
d) None of the above
Ans: b) 0
29. What will count(array(1, 2, 3, 4)); return?
a) 3
b) 4
c) 5
d) Error
Ans: b) 4
30. How do you loop through an associative array?
a) foreach()
b) for()
c) while()
d) do-while()
Ans: a) foreach()
31. How do you check if a variable is an array?
a) is_array()
b) check_array()
c) isset()
d) array_check()
Ans: a) is_array()
32. What is an associative array in PHP?
a) An array with automatic numeric keys
b) An array with custom named keys
c) A two-dimensional array
d) A list of functions
Ans: b) An array with custom named keys
33. How do you create an associative array in PHP?
a) $arr = array("name" => "John", "age" => 25);
b) $arr = array("John", "25");
c) $arr = ("name" => "John", "age" => 25);
d) $arr = list("name" => "John", "age" => 25);
Ans: a) $arr = array("name" => "John", "age" => 25);
34. How do you access the value of the key "age" from the following array?
$arr = array("name" => "Alice", "age" => 22);
a) $arr["age"]
b) $arr->age
c) $arr[1]
d) $arr("age")
Ans: a) $arr["age"]
35. Which of the following is a multidimensional array?
a) $arr = array( array(1,2), array(3,4) );
b) $arr = array(1, 2, 3, 4);
c) $arr = array("name" => "John");
d) $arr = "array"
Ans: a) $arr = array( array(1,2), array(3,4) );
36. How do you access an element in a two-dimensional array?
$arr = array(array(1,2), array(3,4));
a) $arr[1,1]
b) $arr[1][1]
c) $arr{1}{1}
d) $arr(1)(1)
Ans: b) $arr[1][1]
37. What is the best loop for iterating over an indexed array?
a) foreach
b) while
c) for
d) do-while
Ans: c) for
38. What is the best loop for iterating over an associative array?
a) for
b) while
c) foreach
d) do-while
Ans: c) foreach
39. What will be the output of the following PHP code?
$arr = array(10, 20, 30);
foreach ($arr as $val) {
echo $val . " ";
}
40. a) 10, 20, 30
b) 10 20 30
c) Error
d) None of the above
Ans: b) 10 20 30
41. The each() function is used for ____.
a) Iterating through an indexed array
b) Iterating through an associative array
c) Checking the size of an array
d) Sorting an array
Ans: b) Iterating through an associative array
42. What will count(array(10, 20, array(30, 40))); return?
a) 2
b) 3
c) 4
d) 5
Ans: b) 3
43. Which of the following is true about PHP?
a) PHP is a compiled language
b) PHP is case-sensitive for variable names
c) PHP does not support object-oriented programming
d) PHP cannot connect to databases
Ans: b) PHP is case-sensitive for variable names
44. Which function is used to terminate script execution in PHP?
a) stop()
b) exit()
c) terminate()
d) end()
Ans: b) exit()
45. What is the default file extension of a PHP script?
a) .html
b) .php
c) .js
d) .py
Ans: b) .php
46. How do you add a single-line comment in PHP?
a) // This is a comment
b) /* This is a comment */
c) # This is a comment
d) Both a and c
Ans: d) Both a and c
47. What function is used to get information about the PHP version?
a) phpinfo()
b) get_version()
c) php_version()
d) version()
Ans: a) phpinfo()
48. What is the default data type of an uninitialized variable in PHP?
a) NULL
b) Integer
c) Boolean
d) String
Ans: a) NULL
49. How do you define a constant in PHP?
a) define("PI", 3.14);
b) const PI = 3.14;
c) $PI = 3.14;
d) Both a & b
Ans: d) Both a & b
50. What will be the output of the following code?
$x = "10";
$y = 10;
echo ($x === $y) ? "Equal" : "Not Equal";
a) Equal
b) Not Equal
Ans: b) Not Equal
51. What is the output of var_dump(0 == "0")?
a) true
b) false
c) Error
d) NULL
Ans: a) true
52. Which function is used to convert a string to an integer?
a) (int)
b) intval()
c) str_to_int()
d) Both a & b
Ans: d) Both a & b
53. What is the correct way to write a switch statement in PHP?
a) switch (expression) { case 1: ... break; }
b) if (expression) { case 1: ... break; }
c) select case (expression) { ... }
d) None of the above
Ans: a) switch (expression) { case 1: ... break; }
54. How do you skip the current iteration of a loop?
a) exit
b) continue
c) break
d) return
Ans: b) continue
55. What is the correct syntax of a ternary operator?
a) condition ? true_value : false_value;
b) if (condition) ? true_value : false_value;
c) condition : true_value ? false_value;
d) None of the above
Ans: a) condition ? true_value : false_value;
56. What will the following code output?
for($i=1; $i<=3; $i++) {
echo $i . " ";
57. a) 1 2 3
b) 1 2 3 4
c) 0 1 2 3
d) None of the above
Ans: a) 1 2 3
58. Which loop is guaranteed to execute at least once?
a) for
b) while
c) do-while
d) foreach
Ans: c) do-while
59. What function is used to convert a string into an array?
a) explode()
b) str_split()
c) split_string()
d) Both a & b
Ans: d) Both a & b
60. Which function removes white spaces from a string?
a) trim()
b) strip()
c) remove_spaces()
d) None of the above
Ans: a) trim()
61. How do you get the position of a substring in a string?
a) strpos()
b) substr_position()
c) find_string()
d) search()
Ans: a) strpos()
62. How do you check if a key exists in an array?
a) array_key_exists()
b) key_exists()
c) isset()
d) Both a & c
Ans: d) Both a & c
63. How do you merge two arrays in PHP?
a) array_merge()
b) merge_array()
c) array_combine()
d) Both a & c
Ans: a) array_merge()
64. What is the purpose of include() in PHP?
a) To include the content of another file
b) To execute another PHP script
c) To define a new function
d) None of the above
Ans: a) To include the content of another file
65. What is the difference between include and require?
a) require stops script execution if the file is missing, include does not
b) Both are identical
c) include loads the file faster
d) None of the above
Ans: a) require stops script execution if the file is missing, include does not
66. How do you read the contents of a file in PHP?
a) file_get_contents()
b) read_file()
c) fopen()
d) Both a & c
Ans: d) Both a & c
67. What is the correct way to create a session in PHP?
a) session_start()
b) start_session()
c) create_session()
d) None of the above
Ans: a) session_start()
68. How do you destroy a session?
a) session_destroy()
b) unset($_SESSION)
c) delete_session()
d) Both a & b
Ans: d) Both a & b
69. What is the default superglobal array for form data in PHP?
a) $_POST
b) $_GET
c) $_REQUEST
d) All of the above
Ans: d) All of the above
70. How do you redirect a page in PHP?
a) header("Location: [Link]");
b) redirect("[Link]");
c) go("[Link]");
d) None of the above
Ans: a) header("Location: [Link]");
71. Which function is used to connect to MySQL in PHP?
a) mysqli_connect()
b) connect()
c) mysql_connect()
d) db_connect()
Ans: a) mysqli_connect()
72. How do you hash a password in PHP?
a) password_hash()
b) hash_password()
c) encrypt()
d) md5()
Ans: a) password_hash()
73. Which keyword is used to define a function in PHP?
a) function
b) define
c) method
d) fun
Ans: a) function
74. Which function is used to terminate script execution in PHP?
a) stop()
b) exit()
c) terminate()
d) end()
Ans: b) exit()
75. How do you loop through an associative array?
a) foreach()
b) for()
c) while()
d) do-while()
Ans: a) foreach()