Exam Prep Questions
-
Is it possible to pass data from PHP to JavaScript?
-
No, because PHP is server-side, and JavaScript is client-side.
-
No, because PHP is a loosely typed language.
-
Yes, because JavaScript executes before PHP.
-
Yes, because PHP can generate valid JavaScript.
-
Is it possible to pass data from JavaScript to PHP?
-
Yes, but not without sending another HTTP request.
-
Yes, because PHP executes before JavaScript.
-
No, because JavaScript is server-side, and PHP is client-side.
-
No, because JavaScript executes before PHP.
-
Which types of form elements can be excluded from the HTTP request?
-
text, radio, and check box
-
text, submit, and hidden
-
submit and hidden
-
radio and check box
-
When processing the form, what is the difference between a hidden form element and a nonhidden one, such as a text box?
-
The hidden form element does not have a name.
-
There is no difference.
-
The hidden form element does not have a value.
-
The hidden form element is excluded from the request.
-
Which of the following form element names can be used to create an array in PHP?
-
foo
-
[foo]
-
foo[]
-
foo[bar]
-
When an expiration date is given in a Set-Cookie header, what is the resulting behavior in subsequent requests?
-
If the expiration date has expired, the cookie is not included.
-
The behavior is the same; the expiration date is included in the Cookie header, and you can access this information in the $_COOKIE superglobal array.
-
The cookie persists in memory until the browser is closed.
-
The cookie is deleted and therefore not included in subsequent requests.
-
If you set a cookie with either setcookie() or header(), you can immediately check to see whether the client accepted it.
-
True, you can check the $_COOKIE superglobal array to see if it contains the value you set.
-
True, but only if register_globals is enabled.
-
False, you can only use setcookie() if you need to test for acceptance. Using header() does not work.
-
False, you must wait until you receive another HTTP request to determine whether it includes the Cookie header.
Answer D is correct. The response that contains the Set-Cookie header is not sent until PHP finishes executing, so you cannot test for acceptance prior to this. Answers A and B are incorrect because the answer is false. Answer C is incorrect because using setcookie() and header() both result in the same thing: A Set-Cookie header is included in the response.
-
Why must you call session_start() prior to any output?
-
Because it is easy to forget if not placed at the top of your scripts.
-
Because you can no longer access the session data store after there has been output.
-
Because session_start() sets some HTTP headers.
-
Because calling session_start() causes the HTTP headers to be sent.
-
Which of the following represents the proper way to set a session variable?
-
$_SESSION['foo'] = 'bar';
-
session_start();
-
session_set_save_handler ('myopen', 'myclose', 'myread', 'mywrite', 'mydelete', 'mygarbage');
-
$foo = $_SESSION['foo'];
-
Which of the following functions allows you to store session data in a database?
-
session_start();
-
session_set_save_handler();
-
mysql_query();
-
You cannot store session data in a database.
Answer D is correct. JavaScript, like HTML, can be dynamically generated by PHP. Answers A and B are incorrect because the answer is yes. Answer C is incorrect because PHP executes before JavaScript.
Answer A is correct. Although your instincts might lead you to believe that you cannot pass data from JavaScript to PHP, such a thing can be achieved with another HTTP request. Answer B is incorrect because PHP executing before JavaScript is not what makes this possible. This is actually the characteristic that might lead you to believe (incorrectly) that the answer is no. Answers C and D are incorrect because the answer is yes, but also because the explanations given are false.
Answer D is correct. When not selected, both radio buttons and check boxes are excluded from the HTTP request. Answer A and B are incorrect because text boxes are always included in the request. Answer C is incorrect because hidden form elements are always included.
Answer B is correct. When processing a form, each form element is simply a name/value pair within one of the superglobal arrays. Answers A and C are incorrect because hidden form elements can (and should) have both a name and a value. Answer D is incorrect because hidden form elements are only excluded from the user's view, not from the HTTP request.
Answer C is correct. PHP will create an enumerated array called foo that contains the values of all form elements named foo[] in the HTML form. Answers A, B, and D are incorrect because any subsequent form elements of the same name will overwrite the value in previous elements.
Answer A is correct. Answer B is incorrect because only the name and value of the cookie are included in the Cookie header. Answer C is incorrect because setting an expiration date causes a cookie to either be deleted (if the date has expired) or written to disk. Answer D is incorrect because the cookie is only deleted if the date has expired, which isn't necessarily the case.
Answer C is correct. Answer A is incorrect because this is a technical necessity, not a best practice. Answer B is incorrect because accessing the session data store is completely independent of whether there has been any output. Answer D is incorrect because you can set other HTTP headers after a call to session_start().
Answer A is correct. Answer B is incorrect because session_start() only activates PHP sessions for the current script. Answer C is incorrect because session_set_save_handler() allows you to override PHP's default session mechanism with your own custom functions. Answer D is incorrect; session data is being used as the value of a regular variable and is not being manipulated in any way.
Answer B is correct. You can use session_set_save_handler() to override PHP's default session-handling functions and store session data any way you want. Answer A is incorrect because session_start() only activates PHP sessions for the current script. Answer C is incorrect because mysql_query() only executes a query with MySQL and does not affect the behavior of PHP's session mechanism. Answer D is incorrect because this statement is false.