PHP Basics - syntax, variables, data types, operators, control structures, mixing PHP with HTML - Question Bank

1. What is the purpose of the `empty()` function in PHP?
A) To check if a variable is empty, false, 0, '0', NULL, or an empty array.
B) To check if a variable is NULL.
C) To check if a variable is a string.
D) To check if a variable is an integer.
2. PHP variables are case-sensitive. Which of the following are different variables?
A) `$myVar`, `$myvar`, `$MYVAR`
B) `$myVar`, `$myVar`
C) `$myVar`, `$MYVAR`
D) All are the same variable.
3. Which PHP construct is used to define a constant?
A) define() function
B) const keyword
C) var keyword
D) let keyword
4. What is the result of the expression `'5 apples' + 2` in PHP?
A) 7
B) '5 apples 2'
C) Error
D) 5
5. What is the result of the expression `'5' + 2` in PHP?
A) 7
B) '7'
C) Error
D) 52
6. What is the result of the expression `5 + '2'` in PHP?
A) 7
B) '7'
C) Error
D) 52
7. Which operator is used for string concatenation in PHP?
A) .
B) +
C) &
D) concat()
8. What is the syntax for a multi-line comment in PHP?
A) /* This is a comment */
B) // This is a comment
C) # This is a comment
D) <!-- This is a comment -->
9. What is the syntax for a single-line comment in PHP?
A) // This is a comment
B) /* This is a comment */
C) # This is a comment
D) <!-- This is a comment -->
10. How do you output the value of a PHP variable `$count` within an HTML paragraph tag?
A) <p><?php echo $count; ?></p>
B) <p>$count</p>
C) <p><?= $count ?></p>
D) Both A and C
11. Which PHP superglobal array contains all the variables from GET, POST and COOKIE?
A) `$_REQUEST`
B) `$_POST`
C) `$_GET`
D) `$_SERVER`
12. If you have an HTML form with a GET method, how can you access the submitted data in PHP?
A) Using the `$_GET` superglobal array.
B) Using the `$_POST` superglobal array.
C) Using the `$_REQUEST` superglobal array.
D) Using the `$_SESSION` superglobal array.
13. If you have an HTML form with a POST method, how can you access the submitted data in PHP?
A) Using the `$_POST` superglobal array.
B) Using the `$_GET` superglobal array.
C) Using the `$_REQUEST` superglobal array.
D) Using the `$_SESSION` superglobal array.
14. Which HTML tag is commonly used to embed PHP code within an HTML document?
A) <p>
B) <div>
C) <span>
D) It can be embedded anywhere, but the PHP tags <?php ?> are essential.
15. What is the PHP function used to output a string and can accept multiple arguments?
A) print
B) echo
C) printf
D) var_dump
16. Consider the following PHP code snippet: `$name = 'World'; echo 'Hello, ' . $name . '!';` What will be the output?
A) Hello, World!
B) Hello, $name!
C) Hello, !
D) Error
17. How can you output a variable's value within an HTML context using PHP?
A) Using the echo statement.
B) Using the print statement.
C) Using the var_dump() function.
D) All of the above.
18. What is the purpose of mixing PHP with HTML?
A) To generate dynamic HTML content based on server-side logic.
B) To create static HTML pages.
C) To style HTML elements.
D) To handle client-side interactions.
19. In a switch statement, what keyword is used to end a case and move to the next?
A) break
B) continue
C) exit
D) stop
20. Which control structure is used to execute different blocks of code based on the value of a variable?
A) switch statement
B) if...else if...else statement
C) for loop
D) while loop
21. Which loop structure is used to iterate over arrays and objects?
A) foreach loop
B) for loop
C) while loop
D) do...while loop
22. Which loop structure executes the code block once, and then continues as long as a condition is true?
A) do...while loop
B) while loop
C) for loop
D) foreach loop
23. Which loop structure continues as long as a condition is true?
A) while loop
B) for loop
C) do...while loop
D) foreach loop
24. Which loop structure is used when you know how many times you want to loop?
A) for loop
B) while loop
C) do...while loop
D) foreach loop
25. Which control structure allows you to execute a block of code multiple times?
A) Loop
B) Conditional
C) Branching
D) Iteration
26. Which control structure is used to execute a block of code if a condition is true, and another block if the condition is false?
A) if...else statement
B) if statement
C) for loop
D) while loop
27. Which control structure is used to execute a block of code only if a specified condition is true?
A) if statement
B) for loop
C) while loop
D) switch statement
28. What is the PHP operator for logical NOT?
A) !
B) NOT
C) ~
D) ¬
29. What is the PHP operator for logical OR?
A) ||
B) OR
C) |
D) |||
30. What is the PHP operator for logical AND?
A) &&
B) AND
C) &
D) &&&
31. What is the PHP operator for comparison (not equal to)?
A) !=
B) ==
C) <>
D) !==
32. What is the PHP operator for comparison (identical to - value and type)?
A) ===
B) ==
C) =
D) !=
33. What is the PHP operator for comparison (equal to)?
A) ==
B) =
C) ===
D) !=
34. What is the PHP operator for assignment?
A) =
B) ==
C) ===
D) :=
35. What is the PHP operator for modulo (remainder of division)?
A) %
B) /
C) *
D) mod
36. What is the PHP operator for division?
A) /
B) *
C) +
D) -
37. What is the PHP operator for multiplication?
A) *
B) +
C) -
D) /
38. What is the PHP operator for subtraction?
A) -
B) +
C) *
D) /
39. What is the PHP operator for addition?
A) +
B) -
C) *
D) /
40. Which PHP data type is used to store arrays?
A) Array
B) List
C) Collection
D) Vector
41. Which PHP data type represents an object that has no value?
A) NULL
B) Void
C) Empty
D) None
42. Which PHP data type can store one of two values: TRUE or FALSE?
A) Boolean
B) Bit
C) Logic
D) Binary
43. Which PHP data type is used to store text?
A) String
B) Text
C) Character
D) Varchar
44. Which PHP data type is used to store decimal numbers?
A) Float
B) Integer
C) Double
D) Decimal
45. Which PHP data type is used to store whole numbers?
A) Integer
B) Float
C) String
D) Boolean
46. What is the default value for a variable that has been declared but not assigned a value in PHP?
A) NULL
B) 0
C) Empty String
D) Boolean false
47. How do you declare a variable in PHP?
A) Using the '$' symbol followed by the variable name.
B) Using the 'var' keyword.
C) Using the 'let' keyword.
D) Using the 'const' keyword.
48. Which symbol is used to end a PHP code block?
A) ?>
B) </script>
C) -->
D) </html>
49. Which symbol is used to start a PHP code block?
A) <?php
B) <!DOCTYPE html>
C) <script>
D) <!--
50. What is the standard file extension for PHP files?
A) .php
B) .html
C) .js
D) .css