Which PHP function can be used to count the number of elements in an array?
- count_elements
- length
- array_count
- count
The correct function to count the number of elements in a PHP array is count. It's a versatile function that works with both indexed and associative arrays. It's an essential tool for array manipulation.
Which keyword is used in PHP to inherit a class?
- extends
- inherit
- interface
- extends/implement
In PHP, the 'extends' keyword is used to inherit a class, allowing a new class (the child class) to inherit the properties and methods of an existing class (the parent class). This is a fundamental concept in object-oriented programming (OOP).
Consider a scenario where a user provides input that your PHP script tries to decode as JSON, but it's not valid JSON. Which function can help you identify the nature of the error?
- json_last_error_msg()
- json_encode()
- json_last_error()
- json_decode()
The json_last_error() function in PHP is used to retrieve the last JSON error that occurred during decoding. It helps identify the nature of the error.
Which of the following is NOT a type of array in PHP?
- Associative Array
- Multidimensional Array
- Circular Array
- Indexed Array
Circular arrays are not a type of array in PHP. PHP primarily supports indexed arrays, associative arrays, and multidimensional arrays.
You are developing a PHP application that processes financial transactions. Which PDO feature would be best suited to ensure that multiple database operations are processed as a single unit?
- Error Handling
- Prepared Statements
- Transactions
- Data Fetching
To ensure that multiple database operations are processed as a single unit in a financial application, you should use transactions. This ensures that all operations succeed or fail together, maintaining data integrity.
In the context of web security, what is the primary purpose of Content Security Policy (CSP)?
- Mitigate Cross-Site Scripting (XSS)
- Protect against DDoS attacks
- Prevent SQL Injection Attacks
- Ensure Secure File Uploads
Content Security Policy (CSP) primarily aims to mitigate Cross-Site Scripting (XSS) attacks by specifying which sources of content are considered safe and trusted. CSP helps prevent the execution of malicious scripts injected into web pages.
You are developing a PHP web application and want to ensure that any PHP errors do not reveal sensitive information to the users. Which approach would you take?
- a) Disable error reporting.
- b) Enable error reporting and use custom error handlers.
- c) Use generic error messages for all errors.
- d) Store errors in a public log file.
b) Enabling error reporting and using custom error handlers is the recommended approach. It allows you to handle errors gracefully while not revealing sensitive information to users. Disabling error reporting (a) could make debugging difficult, using generic error messages (c) isn't best practice, and storing errors in a public log file (d) is a security risk.
To specify that a function should return a value of a specific type, you use a ________ before the function name.
- returntype
- returnType
- typehint
- type
In PHP, to specify a function's return type, you use returnType before the function name. This is known as a "return type declaration."
Which PHP function checks if a string is a valid JSON string?
- json_verify()
- json_decode()
- json_check()
- json_is_valid()
To check if a string is a valid JSON string in PHP, you can use the json_is_valid() function. This function returns true if the string is valid JSON and false if it's not. Using json_decode() will attempt to decode the string and return null on invalid input.
How can you retrieve the last inserted ID after inserting a record into a MySQL table using PDO?
- $pdo->lastInsertId()
- $pdo->fetchLastInsertID()
- $pdo->getInsertedID()
- $pdo->retrieveLastID()
To retrieve the last inserted ID, you can use the $pdo->lastInsertId() method. This method returns the auto-incremented value generated for an AUTO_INCREMENT column, which is commonly used as a primary key in a database table.
A user reports that every time they log into the web application from a different device, they are asked to log in again, even if they have selected the "remember me" option. What might be a potential cause for this behavior?
- Cookie expiration settings
- Server-side session data
- Browser cache
- Network connectivity
The "remember me" functionality typically relies on cookies with longer expiration times. If the cookie expires quickly, users will have to log in again. Check cookie settings to extend their lifetime. Server-side session data, browser cache, and network connectivity are less likely causes.
When two or more interfaces have the same method name, it's an example of method ________ in PHP.
- 'polymorphism'
- 'abstraction'
- 'overloading'
- 'inheritance'
When two or more interfaces define a method with the same name but possibly different implementations, it's called method overloading in PHP, allowing multiple ways to use the method with the same name.