Unit 2 Flashcards

1
Q

What keyword is used to access a global variable inside a PHP function?
A. public
B. super
C. global
D. access

A

Answer: C

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

Which PHP superglobal is used to access global variables from anywhere in the script?
A. $_GLOBAL
B. $GLOBALS
C. global[]
D. superglobal[]

A

Answer: B

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

Which of the following is NOT a PHP superglobal?
A. $_POST
B. $_SESSION
C. $_CLIENT
D. $_GET

A

Answer: C

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

What is the main difference between $_POST and $_GET methods?
A. POST can’t be used with forms
B. GET stores data in the body, POST in the URL
C. GET stores data in URL, POST stores it in the request body
D. POST encrypts data by default

A

Answer: C

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

What does htmlentities() do in PHP?
A. Converts a string to uppercase
B. Converts HTML tags into their encoded form
C. Removes numbers from strings
D. Displays HTML code as raw text

A

Answer: B

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

What is the benefit of using htmlspecialchars() over htmlentities()?
A. It is more secure
B. It encodes more characters
C. It is faster and sufficient for most use cases
D. It doesn’t encode anything

A

Answer: C

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

You want to display a user’s name submitted via a form using the name field. What is the correct code?
A. echo $_REQUEST[“name”];
B. echo $_POST[“name”];
C. echo $_GET[“name”];
D. All of the above (depends on form method)

A

Answer: D

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

Which of the following will correctly check if a $_GET[‘color’] value exists before using it?
A. isset($_GET[‘color’])
B. $_GET->color
C. if ($_GET[‘color’] == NULL)
D. defined($_GET[‘color’])

A

Answer: A

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

Which form element should be used to allow a user to upload a file?

A. <input type="text">
B. <input type="upload">
C. <input type="file">
D. <input type="submit">
A

Answer: C

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

What is the default HTTP method used by forms if none is specified?
A. GET
B. POST
C. PUT
D. DELETE

A

Answer: A

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

Which of the following functions best combines trimming and escaping input for HTML output?
A. htmlentities(trim($data))
B. sanitizeInput($data)
C. strip_tags($data)
D. htmlspecialchars(strip_tags($data))

A

Answer: B

Explanation: This refers to the custom sanitizeInput() function shown in the slides: htmlspecialchars(trim($data)).

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

What is the purpose of using foreach ($_POST as $key => $value) in form processing?
A. It sanitizes all inputs
B. It loops through all submitted form field name/value pairs
C. It resets the form values
D. It checks if a form is valid

A

Answer: B

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

What is the output of var_dump($_POST) typically used for?
A. Clearing form values
B. Encrypting POST data
C. Debugging and displaying structure and contents of POST data
D. Displaying the page source

A

Answer: C

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

What does the required attribute in an HTML form field do?
A. Limits input length
B. Submits form without validation
C. Ensures the field is not empty before submission
D. Checks if input matches a number

A

Answer: C

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
Which HTML input type would you use for validating an email format automatically?
A. <input type="text".>
B. <input type="validate".>
C. <input type="email".>
D. <input type="mail".>
A

Answer: C

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

In client-side JavaScript validation, which method stops a form from submitting?
A. form.stop()
B. return false;
C. event.preventDefault()
D. preventSubmission()

17
Q

What does the JavaScript checkValidity() method do?
A. Checks for duplicate form entries
B. Confirms browser compatibility
C. Verifies all form field constraints
D. Encrypts form data

18
Q

What is the purpose of setCustomValidity() in JavaScript form validation?
A. It logs validation errors
B. It resets the form
C. It overrides default error messages with a custom one
D. It disables the submit button

19
Q

Which PHP variable holds the request type used to determine if a form was submitted via POST?
A. $_POST[“type”]
B. $_SERVER[“METHOD”]
C. $_REQUEST[“type”]
D. $_SERVER[“REQUEST_METHOD”]

20
Q

What is the correct way to validate that a name field has more than 3 characters in PHP?
A. strlen($name) > 3
B. strlen($name) < 3
C. $name > 3
D. strlen(name) > 3

21
Q

Which of the following is true about server-side form validation in PHP?
A. It is optional if client-side JS is enabled
B. It only runs if JavaScript fails
C. It should always be used for secure validation
D. It runs after the browser reloads

22
Q

What would this JavaScript code check for?
if (message === “”) {
alert(“Message field cannot be empty.”);
}
```
A. That the message field is valid JSON
B. That the message is under 20 characters
C. That the message input is not empty
D. That the message is an integer

23
Q

Which method of validation is safest to rely on for security?
A. JavaScript only
B. HTML5 pattern matching
C. Server-side PHP validation
D. CSS validation

24
Q

In PHP, how can you display all validation errors stored in the $errors array?
A. echo $errors;
B. implode($errors);
C. foreach ($errors as $error) { echo $error; }
D. print($errors[])

25
Q

What is the purpose of calling .reportValidity() in JavaScript Constraint Validation API?
A. It refreshes the page
B. It logs input to console
C. It shows the browser’s validation error message to the user
D. It prevents form fields from being validated

26
Q

You validate a form on the client side using JavaScript, but users can still submit invalid data. Why?
A. JavaScript is not enough — it can be disabled by the user
B. The form is missing the required attribute
C. action is not defined in the form
D. HTML input fields must be <form:input> to be secure</form:input>