php Flashcards

1
Q

Q1: What does PHP stand for?

A

A: PHP stands for Hypertext Preprocessor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Q2: Is PHP a client-side or server-side language?

A

A: PHP is a server-side scripting language.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Q3: What are PHP files saved as?

A

A: PHP files have a .php extension.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Q4: What platforms does PHP support?

A

A: PHP runs on Windows, Linux, Mac, etc.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Q5: Name two popular web servers PHP supports.

A

A: Apache and IIS.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Q6: Can PHP work with databases?

A

A: Yes, PHP supports databases like MySQL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Q7: What can PHP do with files?

A

A: PHP can create, open, read, write, and delete files.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Q8: How is PHP used in forms?

A

A: It collects form data and processes it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Q9: How is PHP code executed?

A

A: On the server, and HTML is sent to the browser.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Q10: What is PHP mainly used for?

A

A: Building dynamic web applications.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Q1: What is the basic syntax for PHP?

A

A: <?php to start, ?> to end.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q2: Are PHP keywords case-sensitive?

A

A: No, but variables are case-sensitive.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Q3: How do you write a single-line comment?

A

A: Using // or #.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Q4: How do you write multi-line comments?

A

A: Using /* */.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Q5: How should a PHP variable start?

A

A: With a $ followed by a letter or underscore.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Q6: Can a PHP variable start with a number?

A

A: No, it must start with a letter or underscore.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Q7: What are valid variable names in PHP?

A

A: $name, $_age, $user123.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

Q8: What happens if PHP code contains an error?

A

A: It may show a blank page or error message.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Q9: What does echo do in PHP?

A

A: Outputs data to the screen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Q10: What symbol is used for variables in PHP?

A

A: The $ sign.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Q1: What are the main data types in PHP?

A

A: String, Integer, Float, Boolean, NULL, Array, Object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Q2: What is a string in PHP?

A

A: A sequence of characters (e.g., “Hello”).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Q7: What is an array in PHP?

A

A: A collection of values stored under one name.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Q8: What are the types of arrays in PHP?

A

A: Indexed, Associative, Multidimensional.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Q9: What is an object in PHP?

A

A: An instance of a class containing data and functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Q

Q10: How do you check a variable’s data type in PHP?

A

A: Using the var_dump() function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
26
Q

Q1: What are the types of conditional statements in PHP?

A

A: if, if-else, if-elseif-else, switch.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
Q

Q2: How does an if statement work in PHP?

A

A: Executes code if a condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
28
Q

Q3: What does an else statement do?

A

A: Runs code if the if condition is false.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

Q4: When do we use elseif in PHP?

A

A: To check multiple conditions after an if.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
30
Q

Q5: What is the purpose of a switch statement?

A

A: To select one of many code blocks to execute.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
31
Q

Q6: What keyword is used to exit a switch case?

A

A: break.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Q7: What happens if you don’t use break in a switch?

A

A: It will continue executing the next case.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
32
Q

Q8: Can you use default in a switch statement?

A

A: Yes, it runs if no cases match.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
33
Q

Q9: What operator is used to compare values in conditions?

A

A: == (equal to).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
34
Q

Q10: What operator checks both value and type?

A

A: === (identical).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
35
Q

Q1: What are the loop types in PHP?

A

A: for, foreach, while, do-while.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
36
Q

Q2: How does a for loop work?

A

A: Loops a set number of times.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
37
Q

Q3: What does a foreach loop do?

A

A: Loops through each element in an array.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
38
Q

Q4: What is the syntax of a while loop?

A

A: Executes as long as a condition is true.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
39
Q

Q5: What is unique about a do-while loop?

A

A: Executes at least once before checking the condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
40
Q

Q6: What keyword is used to exit a loop?

A

A: break.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
41
Q

Q7: What keyword skips the current iteration?

A

A: continue.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
42
Q

Q8: What is an infinite loop?

A

A: A loop that never ends due to a missing exit condition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
43
Q

Q9: Can loops be nested in PHP?

A

A: Yes, loops can be placed inside other loops.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
44
Q

Q10: How do you loop through an associative array?

A

A: Using the foreach loop.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
45
Q

Q1: What is a function in PHP?

A

A: A block of code that performs a specific task.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
46
Q

Q2: How do you define a function in PHP?

A

A: Using the function keyword.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
47
Q

Q3: What is the syntax for defining a function?

A

A: function functionName() { //code }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
48
Q

Q4: How do you call a function?

A

A: By using its name followed by parentheses functionName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
49
Q

Q5: What are PHP function types?

A

A: Built-in and user-defined functions.

50
Q

Q6: How do you pass arguments to a function?

A

A: Inside parentheses, e.g. function greet($name).

51
Q

Q7: Can a function return a value?

A

A: Yes, using the return keyword.

52
Q

Q8: What is a default function argument?

A

A: A preset value if no argument is passed.

53
Q

Q9: Are function names case-sensitive?

A

A: No, function names are not case-sensitive.

54
Q

Q10: What does declare(strict_types=1); do?

A

A: Enforces strict type checking in PHP 7+.

55
Q

Q1: What is an array in PHP?

A

A: A collection of values stored under a single name.

56
Q

Q2: What are the types of arrays in PHP?

A

A: Indexed, Associative, Multidimensional.

57
Q

Q3: How do you create an indexed array?

A

A: $arr = array(“apple”, “banana”, “cherry”);

58
Q

Q4: How do you access an indexed array element?

A

A: Using its index, e.g. $arr[0].

59
Q

Q5: How do you create an associative array?

A

A: $arr = array(“name” => “Salam”, “age” => 22);

60
Q

Q6: How do you access an associative array value?

A

A: By its key, e.g. $arr[“name”].

60
Q

Q7: What is a multidimensional array?

A

A: An array containing other arrays.

61
Q

Q8: How do you loop through an array?

A

A: Using a foreach loop.

62
Q

Q9: Name two functions to sort an array?

A

A: sort() and rsort().

63
Q

Q10: How do you count elements in an array?

A

A: Using count($array);.

64
Q

Q1: What are PHP superglobals?

A

A: Predefined variables accessible from anywhere in code.

65
Q

Q2: What does $_GET do?

A

A: Collects form data sent via URL.

66
Q

Q3: What does $_POST do?

A

A: Collects form data submitted via POST method.

67
Q

Q4: What does $_SERVER do?

A

A: Holds server and execution environment information.

68
Q

Q5: What does $_SESSION store?

A

A: User session data across pages.

69
Q

Q6: What does $_COOKIE store?

A

A: Stores small pieces of data on the user’s device.

70
Q

Q7: What does $_FILES handle?

A

A: File uploads in forms.

71
Q

Q8: What does $_REQUEST do?

A

A: Collects data from both GET and POST requests.

72
Q

Q9: How do you check if a form field is set?

A

A: Using isset($_POST[‘fieldname’]);.

73
Q

Q10: What does $_GLOBALS store?

A

A: All global variables in an array.

74
Q

Q1: Why is form validation important?

A

A: To protect against spam and security threats.

75
Q

Q2: What function removes whitespace from input?

A

A: trim().

76
Q

Q3: What function removes backslashes from input?

A

A: trim().

77
Q

Q3: What function removes backslashes from input?

A

A: stripslashes().

78
Q

Q5: How do you check if a field is empty?

A

A: Using empty().

79
Q

Q4: What function converts special characters?

A

A: htmlspecialchars().

80
Q

Q6: How do you validate an email in PHP?

A

A: Using filter_var($email, FILTER_VALIDATE_EMAIL).

81
Q

Q7: What is cross-site scripting (XSS)?

A

A: A security attack where attackers inject scripts into web forms.

82
Q

Q8: How can PHP prevent XSS attacks?

A

A: By sanitizing input with htmlspecialchars().

83
Q

Q9: What function filters input data?

A

A: filter_input().

84
Q

Q10: How do you handle errors in form validation?

A

A: By checking conditions and showing error messages.

85
Q

Q1: What is a cookie in PHP?

A

A: A small file stored on the user’s computer.

86
Q

Q2: How do you set a cookie in PHP?

A

A: Using setcookie(name, value, expiry);.

87
Q

Q3: How do you access a cookie?

A

A: Using $_COOKIE[‘name’];.

88
Q

Q4: How do you delete a cookie?

A

A: Set an expired time with setcookie(name, “”, time() - 3600);.

89
Q

Q5: What is a session in PHP?

A

A: A way to store user data across multiple pages.

90
Q

Q6: How do you start a session?

A

A: Using session_start();.

91
Q

Q7: How do you store session data?

A

A: Using $_SESSION[‘key’] = ‘value’;.

92
Q

Q8: How do you remove session data?

A

A: Using unset($_SESSION[‘key’]);.

93
Q

Q9: How do you destroy an entire session?

A

A: Using session_destroy();.

94
Q

Q10: What is the difference between a cookie and a session?

A

A: Cookies are stored on the client, sessions on the server.

95
Q

Q1: What is MySQL?

A

A: A free, fast, and reliable database system.

96
Q

Q2: How do you connect PHP to MySQL?

A

A: Using MySQLi or PDO extensions.

97
Q

Q3: What does SQL stand for?

A

A: Structured Query Language.

98
Q

Q4: How do you create a MySQL connection in PHP?

A

A: Using mysqli_connect(server, user, password, dbname);.

99
Q

Q5: What function is used to close a MySQL connection?

A

A: mysqli_close($conn);.

100
Q

Q6: How do you insert data into a MySQL table?

A

A: Using INSERT INTO table (columns) VALUES (values);.

101
Q

Q7: How do you retrieve data from a MySQL table?

A

A: Using SELECT * FROM table;.

102
Q

Q8: How do you update records in MySQL?

A

A: Using UPDATE table SET column=value WHERE condition;.

103
Q

Q9: How do you delete records from MySQL?

A

A: Using DELETE FROM table WHERE condition;.

104
Q

Q10: What function is used to execute SQL queries in PHP?

A

A: mysqli_query($conn, $query);.

105
Q

Q1: What does MySQLi stand for?

A

A: MySQL Improved.

106
Q

Q2: What is PDO?

A

A: PHP Data Objects, a database access layer.

107
Q

Q3: Which extension works only with MySQL?

A

A: MySQLi.

108
Q

Q4: Which extension supports multiple databases?

109
Q

Q5: Which is better for prepared statements, MySQLi or PDO?

A

A: Both support prepared statements.

110
Q

Q6: What is an advantage of MySQLi?

A

A: Supports both procedural and object-oriented programming.

111
Q

Q7: How do you connect to MySQL using PDO?

A

A: new PDO(“mysql:host=host;dbname=db”, “user”, “password”);.

112
Q

Q8: Can you switch databases easily with PDO?

A

A: Yes, it supports 12+ databases.

113
Q

Q9: Does MySQLi support exception handling?

A

A: No, but PDO does.

114
Q

Q10: Which one is more secure, MySQLi or PDO?

A

A: Both are secure when using prepared statements.

115
Q

Q1: What is a prepared statement?

A

A: A precompiled SQL statement to prevent SQL injection.

116
Q

Q2: Why are prepared statements important?

A

A: They protect against SQL injection attacks.

117
Q

Q3: How do you create a prepared statement in MySQLi?

A

A: Using $stmt = $conn->prepare(“SQL query”);.

118
Q

Q4: How do you bind parameters in a prepared statement?

A

A: Using $stmt->bind_param(“s”, $var);.

119
Q

Q5: What does the s in bind_param mean?

A

A: String; it defines the data type.

120
Q

Q6: How do you execute a prepared statement?

A

A: Using $stmt->execute();.

121
Q

Q7: How do you fetch results from a prepared statement?

A

A: Using $stmt->get_result();.

122
Q

Q8: How do you close a prepared statement?

A

A: Using $stmt->close();.

123
Q

Q9: What data types can be bound in prepared statements?

A

A: i (integer), d (double), s (string), b (blob).

124
Q

Q10: What is an advantage of using prepared statements?

A

A: Faster execution and enhanced security.