Writing web page with PHP Questions with Answers

17 practice questions on Writing web page with PHP (PHP) with answers and step-by-step solutions. Try 10 samples below, then practise all 17 free in the AptiNest app.

Sample questions

Question 1Medium

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. Yes, because PHP can generate valid JavaScript

Explanation

Answer D is correct. JavaScript, like HTML, can be dynamically generated by PHP. PHP executes before JavaScript.

Step-by-step solution

  1. PHP runs on the server and generates HTML or JavaScript code.
  2. Generated JavaScript is sent to the browser where it runs.
  3. PHP can create valid JavaScript code dynamically.
  4. JavaScript executes in the browser after receiving data from PHP.
  5. So, PHP can pass data to JavaScript through generated code.
  6. Final answer: Yes, because PHP can generate valid JavaScript.

Why the other options are wrong

  • A. The statement is wrong because PHP can pass data to JavaScript by generating script code.
  • B. PHP being loosely typed does not stop it passing data to JavaScript.
  • C. JavaScript does not execute before PHP in a way that enables this; the server generates the code first.

Tip: Use PHP to dynamically write JavaScript code for client-side data handling.

Question 2Medium

When you use the $_POST variable to collect data, the data is visible to....

  1. none
  2. only you
  3. everyone
  4. selected few

Answer: B. only you

Explanation

With POST, the data is sent in the request body, not in the URL, so it does not show in the address bar or browser history. Of the choices, 'only you' is the one that fits.

Step-by-step solution

  1. Data sent with $_POST goes in the request body, not in the URL.
  2. So it is not shown in the address bar, unlike GET.
  3. Following the given key, it is visible only to you.

Why the other options are wrong

  • A. The sender still sees the data.
  • C. It is not shown in the URL.
  • D. It is not meant for a chosen few.

Tip: GET shows data in the URL; POST does not.

Question 3Medium

Which of the following functions can be used to escape data such that it can be displayed without altering the appearance of the original data?

  1. htmlspecialchars()
  2. addslashes()
  3. escapeshellargs()
  4. urlencode()

Answer: A. htmlspecialchars()

Explanation

Answer A is correct because htmlspecialchars() will convert special characters to HTML entities that will display correctly in any Web client.
addslashes() only escapes single quotes. escapeshellargs() is only helpful when dealing with shell command arguments.
URL encoding is not interpreted by Web clients except in the context of URLs.

Step-by-step solution

  1. The question asks for a function that lets data show on a web page without changing how it looks.
  2. Htmlspecialchars converts special characters such as the less-than sign and the ampersand into HTML entities.
  3. Those entities display as the original characters in any browser.
  4. The correct answer is option A, htmlspecialchars.

Why the other options are wrong

  • B. Addslashes is for escaping quotes in code strings, not for HTML display.
  • C. Escapeshellarg is for shell command arguments, not for web page display.
  • D. Urlencode is for building URLs, not for displaying text on a page.

Tip: For showing text safely in HTML, think of htmlspecialchars.

Question 4Medium

By default, PHP stores session data in ___.

  1. The filesystem
  2. A database
  3. Virtual memory
  4. Shared memory

Answer: A. The filesystem

Explanation

By default PHP stores session data as files on the filesystem, in the folder set by session.save_path. Databases, virtual memory and shared memory are not the default.

Step-by-step solution

  1. PHP uses the filesystem by default for storing session data.
  2. Session files are saved in the /tmp directory on Unix-based systems.
  3. On Windows, the session.save_path must be set to a valid directory like C:\Temp.
  4. This confirms that the default storage is the filesystem.
  5. The correct answer is The filesystem.

Why the other options are wrong

  • B. PHP does not use a database by default; it stores sessions in files unless configured otherwise.
  • C. Virtual memory is not where PHP stores sessions by default.
  • D. Shared memory is not the default; that would need a different session handler, not files.

Tip: Remember: PHP defaults to using the filesystem for sessions unless configured otherwise.

Question 5Medium

How do we submit form data without a submit button?

  1. Using header() function
  2. Using Javascript
  3. Using fdf_set_submit_form_action() fucntion
  4. Both a and b above

Answer: B. Using Javascript

Explanation

header() only redirects and does not send form data; fdf_set_submit_form_action is for PDF forms; JavaScript submit() works

Step-by-step solution

  1. A form can be sent without a submit button by running script, such as JavaScript.
  2. JavaScript can call the form's submit() method to send the data.
  3. header() only sends HTTP headers and redirects, so it does not submit form data.
  4. So the answer is option B, Using JavaScript.

Why the other options are wrong

  • A. header() does not submit form data; it only sends headers or redirects.
  • C. fdf_set_submit_form_action() is for PDF forms, not HTML forms.
  • D. Option a is wrong, so 'both a and b' is wrong.

Tip: To send a form from code, use JavaScript submit() or a server-side request.

Question 6Medium

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. Yes, but not without sending another HTTP request.

Explanation

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.

Step-by-step solution

  1. JavaScript runs on the client-side, PHP runs on the server-side.
  2. Data can be sent from JavaScript to PHP using an HTTP POST or GET request.
  3. PHP can receive and process the data sent by JavaScript.
  4. This requires another HTTP request to send the data.
  5. Final answer: Yes, but not without sending another HTTP request.

Why the other options are wrong

  • B. PHP runs on the server after the request, so it does not run before JavaScript in the browser.
  • C. JavaScript is client-side and PHP is server-side, the reverse of what this option says.
  • D. Timing is not the reason; data still needs a new HTTP request to reach PHP.

Tip: Use HTTP POST/GET to pass data from JavaScript to PHP.

Question 7Medium

When is cross-site scripting a heightened risk?

  1. When storing data submitted by the user.
  2. When displaying foreign data.
  3. When executing a shell command.
  4. When opening a remote URL.

Answer: B. When displaying foreign data.

Explanation

Answer B is correct. When displaying foreign data that is not properly escaped, you can inadvertently expose your users to significant risk.
storing data poses no immediate threat, even though this might result in a cross-site scripting vulnerability later.

Step-by-step solution

  1. Cross-site scripting (XSS) happens when foreign data is injected into web pages.
  2. Foreign data that isn't properly escaped can execute malicious scripts in browsers.
  3. Displaying untrusted data without escaping creates a direct XSS attack vector.
  4. Storing user data doesn't immediately expose users to XSS unless it's later displayed.
  5. Executing shell commands or opening URLs are separate risks, not directly linked to XSS.
  6. The correct answer is When displaying foreign data.

Why the other options are wrong

  • A. Storing user data is not the main risk; it becomes one only when the data is displayed without escaping.
  • C. Executing a shell command is a separate risk, not cross-site scripting.
  • D. Opening a remote URL is a separate risk, not the cause of cross-site scripting.

Tip: Always sanitize and escape any data you display to prevent XSS attacks.

Question 8Medium

If no expiration time is explicitly set for a cookie, what happens to it?

  1. It expires right away
  2. It never expires
  3. It is not set
  4. It expires at the end of the user's browser session

Answer: D. It expires at the end of the user's browser session

Explanation

Cookies automatically expire at the end of the user's browser session if no explicit expiration time is set. Cookies are not necessary to maintain a server-side session, so answer D is correct.

Step-by-step solution

  1. Cookies without an expiration time expire when the browser session ends.
  2. This means the cookie is deleted once the user closes the browser.
  3. No data is stored permanently if no expiration time is set.
  4. The cookie is not saved beyond the current session.
  5. Final answer: It expires at the end of the user's browser session.

Why the other options are wrong

  • A. A cookie without an expiry does not expire right away; it lasts until the browser session ends.
  • B. A cookie without an expiry does not last forever; it ends when the browser session ends.
  • C. The cookie is still set; it just has no expiry time, so it is not left unset.

Tip: Remember, cookies without expiry dates last only for the session.

Question 9Medium

In an HTTPS transaction, how are URLs and query strings passed from the browser to the web server?

  1. They are passed in clear text, and the subsequent transaction is encrypted
  2. They are encrypted
  3. The URL is left in clear text, while the query string is encrypted
  4. The URL is encrypted, while the query string is passed in clear text

Answer: B. They are encrypted

Explanation

When an HTTPS transaction takes place, the browser and the server immediately negotiate an encryption mechanism so that any subsequent data is not passed in clear text including the URL and query string, which are otherwise passed the same way as with a traditional HTTP transaction.
Answer B is, therefore, correct.

Step-by-step solution

  1. The question asks what happens to the URL and query string in an HTTPS transaction.
  2. The browser and server agree on encryption first, so later data is not sent in clear text.
  3. The URL and query string are part of that data, so they are encrypted too.
  4. The correct answer is option B, They are encrypted.

Why the other options are wrong

  • A. The URL and query string are not sent in clear text under HTTPS; they are encrypted.
  • C. The URL is not left in clear text; both parts are encrypted.
  • D. The query string is not sent in clear text; it is encrypted too.

Tip: Under HTTPS, everything after the connection is set up is encrypted, including the URL.

Question 10Medium

Assuming that the client browser is never restarted, how long after the last access will a session 'expire' and be subject to garbage collection?

  1. After exactly 1,440 seconds
  2. After the number of seconds specified in the session.gc_maxlifetime INI setting
  3. It will never expire unless it is manually deleted
  4. It will only expire when the browser is restarted

Answer: B. After the number of seconds specified in the session.gc_maxlifetime INI setting

Explanation

The session.gc_maxlifetime INI setting regulates the amount of time since the last access after which the session handler considers a session data file garbage and marks it for deletion by the garbage handler. Once this has happened, any subsequent access to the session will be considered invalid, even if the data file still exists. Coincidentally, the session.gc_maxlifetime is set to 1,440 seconds, but you can't rely on that number as it might have been changed without your knowledge by the system administrator. Answer B is, therefore, correct.

Step-by-step solution

  1. Session data remains active until no access occurs for the time set in session.gc_maxlifetime.
  2. This setting defines how long a session stays valid after last activity.
  3. If no access happens within this time, the session is marked for deletion.
  4. The exact duration depends on the value of session.gc_maxlifetime INI setting.
  5. Garbage collection removes the session data once it's marked as expired.
  6. Final answer: After the number of seconds specified in the session.gc_maxlifetime INI setting.

Why the other options are wrong

  • A. The time is not fixed at 1,440 seconds; it depends on the session.gc_maxlifetime setting.
  • C. Sessions do expire without manual deletion, once the gc_maxlifetime time passes with no access.
  • D. The session does not wait for a browser restart; expiry is based on the last access time.

Tip: Remember, session timeout is set on the server, not the browser.

Practise all 17 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all PHP topics · All topics