Quiz in PHP
Sure! Here's a PHP quiz to test your knowledge of PHP concepts:
PHP Quiz
1. What does PHP stand for?
A) Personal Hypertext Processor
B) Private Home Page
C) PHP: Hypertext Preprocessor
D) Public Hypertext Processor
2. Which of the following is a valid way to declare a variable in PHP?
A) var $name;
B) name = $var;
C) $name;
D) var name;
3. How do you define a constant in PHP?
A) const PI = 3.14;
B) define("PI", 3.14);
C) Both A and B
D) constant PI = 3.14;
4. Which PHP function is used to send headers to the browser?
A) header()
B) send_headers()
C) output_header()
D) set_header()
5. What is the correct way to open a file in PHP?
A) open("filename.txt");
B) fopen("filename.txt", "r");
C) file_open("filename.txt");
D) open_file("filename.txt", "r");
6. How can you retrieve the value of a query string in PHP?
A) $_GET['name'];
B) $_POST['name'];
C) $_REQUEST['name'];
D) All of the above
7. Which of the following is a valid method to check if a variable is set in PHP?
A) isset($variable);
B) check($variable);
C) var_set($variable);
D) is_set($variable);
8. Which of the following functions will read the contents of a file in PHP?
A) fopen()
B) read_file()
C) fread()
D) file_get_contents()
9. What does the following code output?
$var = 5;echo ++$var;A) 4
B) 5
C) 6
D) Error
10. Which superglobal array contains session variables in PHP?
A) $_SESSION
B) $_POST
C) $_REQUEST
D) $_COOKIE
11. Which PHP keyword is used to define a class?
A) function
B) class
C) define
D) public
12. Which function is used to include a file in PHP?
A) include()
B) require()
C) include_once()
D) All of the above
13. How do you comment in PHP?
A) // comment for single-line comments, /* comment */ for multi-line comments
B) # comment for single-line comments, <!-- comment --> for multi-line comments
C) /* comment */ for single-line comments, // comment for multi-line comments
D) // comment for single-line comments, <!-- comment --> for multi-line comments
14. What is the default session save path in PHP?
A) /tmp
B) session_save_path()
C) php_sessions
D) C:\PHP\sessions
15. Which of the following is NOT a valid PHP loop?
A) for
B) while
C) do-while
D) foreach
E) repeat-until
Answer Key:
C - PHP: Hypertext Preprocessor
A -
var $name;is the correct way to declare a variable in PHP.C - Both
constanddefine()are valid ways to declare constants.A -
header()is used to send HTTP headers.B -
fopen("filename.txt", "r");is the correct way to open a file.D - All of the above.
A -
isset($variable)is used to check if a variable is set.D -
file_get_contents()is used to read the contents of a file.C - The
++$varis a pre-increment operator, so it will increment the value before outputting it, resulting in 6.A -
$_SESSIONis used to store and access session variables.B -
classis the keyword used to define a class in PHP.D - All of the above functions are used to include files in PHP.
A -
// commentfor single-line and/* comment */for multi-line comments.B -
session_save_path()returns the default session save path.E -
repeat-untilis not a valid loop in PHP.
Let me know how you did! If you need further explanations or additional questions, feel free to ask!