1. Which of the following is the correct opening tag for PHP code?
A: <?php ?>
B: <php>
C: <? ?>
D: <script>
Answer: A
Explanation: The correct PHP opening tag is <?php ?>, which tells the parser to start interpreting
PHP code.
2. Which PHP command is used to output text to the browser?
A: write
B: echo
C: output
D: display
Answer: B
Explanation: The echo command is used to output one or more strings in PHP.
3. What does XAMPP stand for in PHP development environments?
A: X (Cross-platform), Apache, MySQL, PHP, Perl
B: X (Executable), Apache, MariaDB, PHP, Python
C: X (Cross-platform), Apache, MySQL, PHP, Python
D: X (Experimental), Apache, MySQL, PHP, Perl
Answer: A
Explanation: XAMPP stands for Cross-platform, Apache, MySQL, PHP, and Perl, making it a
popular all-in-one web server solution.
4. Which of the following is not a valid PHP variable name?
A: $variable
B: $_var
C: $1stVar
D: $var_name
Answer: C
Explanation: In PHP, variable names cannot start with a number; they must begin with a letter or
an underscore.
5. What symbol is used to denote a variable in PHP?
A: #
B: $
C: @
D: %
,Answer: B
Explanation: Variables in PHP are prefixed with the $ symbol.
6. Which of the following is a correct way to declare a constant in PHP?
A: constant("PI", 3.14);
B: define("PI", 3.14);
C: const PI = 3.14;
D: Both B and C
Answer: D
Explanation: Constants can be declared using either define() or const, depending on the context.
7. Which data type in PHP is used for storing a sequence of characters?
A: Integer
B: Boolean
C: String
D: Array
Answer: C
Explanation: Strings are used to store text, which is a sequence of characters.
8. How can you embed PHP code within an HTML document?
A: By placing the PHP code between <php> and </php> tags
B: By using <?php and ?> tags
C: By writing PHP code outside HTML
D: By placing the code within <script> tags
Answer: B
Explanation: PHP code is embedded in HTML by enclosing it within <?php and ?> tags.
9. Which operator is used to concatenate two strings in PHP?
A: +
B: . (dot)
C: &
D: , (comma)
Answer: B
Explanation: The dot operator is used to concatenate strings in PHP.
10. Which function is used to display the structure and content of a variable in PHP?
A: print()
B: var_dump()
C: echo()
D: debug()
Answer: B
Explanation: var_dump() outputs detailed information about a variable including its type and
value.
11. Which statement is used for conditional execution in PHP?
A: for
,B: switch
C: if
D: foreach
Answer: C
Explanation: The if statement is used to execute code based on a condition in PHP.
12. What is the purpose of the else statement in PHP?
A: To execute code only if a condition is true
B: To execute code if the preceding if condition is false
C: To iterate over arrays
D: To terminate a loop
Answer: B
Explanation: The else statement executes code when the if condition evaluates to false.
13. Which of the following is used to execute a block of code multiple times?
A: if
B: switch
C: loop
D: for
Answer: D
Explanation: The for loop is designed to execute a block of code repeatedly for a set number of
iterations.
14. Which loop is best suited for iterating over arrays in PHP?
A: for
B: foreach
C: while
D: do-while
Answer: B
Explanation: The foreach loop is specifically designed for iterating over arrays.
15. What is the function of the break statement in PHP loops?
A: To skip the current iteration and continue with the next
B: To terminate the loop entirely
C: To exit from a function
D: To create an infinite loop
Answer: B
Explanation: The break statement terminates the execution of the loop.
16. What does the continue statement do in a PHP loop?
A: Terminates the loop
B: Skips the rest of the code in the current iteration and continues with the next iteration
C: Repeats the current iteration
D: Exits the script
Answer: B
, Explanation: The continue statement skips the remaining code in the current loop iteration and
proceeds with the next iteration.
17. Which keyword is used to declare a function in PHP?
A: function
B: def
C: fun
D: declare
Answer: A
Explanation: Functions in PHP are declared using the keyword function.
18. How do you call a PHP function named myFunction()?
A: call myFunction();
B: myFunction;
C: myFunction();
D: function myFunction();
Answer: C
Explanation: A function is called by using its name followed by parentheses.
19. What is recursion in PHP?
A: A loop that never ends
B: A function that calls itself
C: A method to handle errors
D: A type of array
Answer: B
Explanation: Recursion is a programming technique where a function calls itself until a condition
is met.
20. Which PHP function can be used to merge two arrays?
A: array_combine()
B: array_merge()
C: array_union()
D: array_join()
Answer: B
Explanation: array_merge() is used to combine the elements of two or more arrays.
21. What is an associative array in PHP?
A: An array with numeric keys only
B: An array with string keys only
C: An array that maps keys to values
D: An array with duplicate keys
Answer: C
Explanation: Associative arrays use named keys that you assign to them, mapping keys to values.
22. Which function is used to add an element to the end of an array in PHP?
A: array_push()