Which PHP superglobal holds server and execution environment information?

  • $GLOBALS
  • $_SERVER
  • $_ENV
  • $_SESSION
The $_SERVER superglobal in PHP holds information about the server and execution environment, such as headers and paths.

Which keyword is used to execute a block of code regardless of whether an exception was thrown?

  • try...except
  • finally
  • always
  • unless
The finally keyword is used to execute a block of code regardless of whether an exception was thrown. This is useful for cleanup operations.

When using transactions in PDO, the ________ method is used to roll back the current transaction.

  • rollback()
  • commit()
  • begin()
  • execute()
In PDO, the rollback() method is used to roll back the current transaction when using transactions.

You are tasked with creating a report that combines data from a "sales" table and a "products" table. The requirement is to list all products, even those that haven't made a sale. Which JOIN operation would you choose?

  • LEFT JOIN
  • INNER JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
A LEFT JOIN retrieves all rows from the left table (products) and the matching rows from the right table (sales). It includes products with no matching sales, meeting the requirement.

Consider a scenario where you need to execute a block of code for each element in an array. Which control structure is most suitable for this purpose?

  • for loop
  • if statement
  • switch statement
  • while loop
A 'for' loop is the most suitable control structure for iterating through each element in an array. It allows you to specify the start and end conditions and easily iterate through array elements.

In PHP, which function is used to encode a value into JSON format?

  • json_decode()
  • json_serialize()
  • encode_json()
  • json_encode()
The json_encode() function in PHP is used to encode a value into JSON format, making it suitable for data interchange.

You are developing an e-commerce site and need to store and retrieve user cart data during their browsing session. Which PHP superglobal would be most appropriate for this use-case?

  • $_SESSION
  • $_COOKIE
  • $_POST
  • $_GET
The $_SESSION superglobal is ideal for storing user-specific data across multiple pages during a browsing session, making it suitable for cart data.

To ensure session data is only transferred over secure connections, you can enable the ________ configuration directive in PHP.

  • session.use_trans_sid
  • session.cookie_secure
  • session.use_cookies
  • session.use_only_cookies
The 'session.cookie_secure' configuration directive ensures that session cookies are only transmitted over secure (HTTPS) connections, enhancing security.

Which PHP function can be used to write a string to a file?

  • fwrite()
  • file_put_contents()
  • file_get_contents()
  • file_open()
The file_put_contents() function in PHP is used to write a string to a file. It simplifies the process of opening a file, writing data, and closing the file, making it a convenient option for file writing operations.

Imagine you are developing a PHP application that needs to frequently insert user data into a database. To ensure security and performance, which approach would be most appropriate?

  • Use PDO prepared statements
  • Use raw SQL statements with user input directly in the query
  • Use MySQLi extension with prepared statements
  • Use raw SQL statements with hardcoded values
Using PDO prepared statements is the recommended approach as it's secure against SQL injection and offers good performance.