Exam Prep Questions

    1. Is it possible to pass data from PHP to JavaScript?
      1. No, because PHP is server-side, and JavaScript is client-side.
      2. No, because PHP is a loosely typed language.
      3. Yes, because JavaScript executes before PHP.
      4. Yes, because PHP can generate valid JavaScript.

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.

    1. Is it possible to pass data from JavaScript to PHP?
      1. Yes, but not without sending another HTTP request.
      2. Yes, because PHP executes before JavaScript.
      3. No, because JavaScript is server-side, and PHP is client-side.
      4. No, because JavaScript executes before PHP.

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.

    1. Which types of form elements can be excluded from the HTTP request?
      1. text, radio, and check box
      2. text, submit, and hidden
      3. submit and hidden
      4. radio and check box

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.

    1. When processing the form, what is the difference between a hidden form element and a nonhidden one, such as a text box?
      1. The hidden form element does not have a name.
      2. There is no difference.
      3. The hidden form element does not have a value.
      4. The hidden form element is excluded from the request.

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.

    1. Which of the following form element names can be used to create an array in PHP?
      1. foo
      2. [foo]
      3. foo[]
      4. foo[bar]

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.

    1. When an expiration date is given in a Set-Cookie header, what is the resulting behavior in subsequent requests?
      1. If the expiration date has expired, the cookie is not included.
      2. 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.
      3. The cookie persists in memory until the browser is closed.
      4. The cookie is deleted and therefore not included in subsequent requests.

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.

    1. If you set a cookie with either setcookie() or header(), you can immediately check to see whether the client accepted it.
      1. True, you can check the $_COOKIE superglobal array to see if it contains the value you set.
      2. True, but only if register_globals is enabled.
      3. False, you can only use setcookie() if you need to test for acceptance. Using header() does not work.
      4. 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.

    2. Why must you call session_start() prior to any output?
      1. Because it is easy to forget if not placed at the top of your scripts.
      2. Because you can no longer access the session data store after there has been output.
      3. Because session_start() sets some HTTP headers.
      4. Because calling session_start() causes the HTTP headers to be sent.

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().

    1. Which of the following represents the proper way to set a session variable?
      1. $_SESSION['foo'] = 'bar';
      2. session_start();
      3. session_set_save_handler ('myopen', 'myclose', 'myread', 'mywrite', 'mydelete', 'mygarbage');
      4. $foo = $_SESSION['foo'];

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.

    1. Which of the following functions allows you to store session data in a database?
      1. session_start();
      2. session_set_save_handler();
      3. mysql_query();
      4. You cannot store session data in a database.

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.