The ________ function in PHP returns an array of files and directories from the specified directory.
- file_get_contents
- dir()
- scandir()
- list_dir()
The scandir() function is used in PHP to return an array of files and directories in a specified directory. It's commonly used for directory listing operations.
Consider you are building a search functionality for your website. Which function would you use to determine if a particular keyword exists within a content string?
- strpos()
- str_replace()
- str_word_count()
- str_split()
The strpos() function is used to find the position of a substring (keyword) within a string. It returns the position or false if not found, making it suitable for searching within a content string.
Which of the following PHP functions is used to check if a given key or index exists in an array?
- key_exists
- in_array
- array_key_exists
- index_exists
To check if a given key or index exists in a PHP array, you should use the array_key_exists function. This function checks if a specific key exists in an array, which is particularly useful for associative arrays.
Consider a scenario where you're building a CMS and you want to log errors to a specific file. Which PHP functions would be most suitable to open and write to this file?
- fopen() and fwrite()
- readfile() and copy()
- file_get_contents() and file_put_contents()
- file() and fwrite()
To open and write to a specific file in PHP, you should use fopen() to open the file and fwrite() to write to it. These functions offer fine-grained control over file handling, crucial for logging errors.
In the context of form validation, what does the term "sanitization" refer to?
- Removing malicious code
- Validating user credentials
- Cleaning and formatting data
- Authenticating the user
Sanitization in form validation involves cleaning and formatting user input data to remove harmful or unwanted characters, ensuring it's safe for processing.
In the context of password hashing in PHP, what does "salting" mean?
- Adding salt to food
- Increasing the password length
- Mixing a unique value with the password
- Encrypting the password using SHA-256
"Salting" in password hashing involves mixing a unique value with the user's password before hashing to enhance security and prevent rainbow table attacks.
When uploading a file in PHP, which array key of the $_FILES superglobal gives the temporary location of the uploaded file?
- tmp_name
- name
- temp_location
- uploaded_file
The 'tmp_name' key in the $_FILES superglobal array contains the temporary location of the uploaded file on the server. This is important for processing and moving the file.
If a transaction encounters an issue and needs to be terminated without saving the changes, it should be ________.
- Rolled Back
- Committed
- Saved
- Isolated
To terminate a transaction without saving any changes, you should use the 'Rollback' operation. This undoes all changes made within the transaction, ensuring data consistency and integrity. 'Committed' is the opposite, indicating that the changes should be saved.
Imagine you are creating a login system for a web application. To ensure the security of user credentials, which of the following methods would be the best practice?
- Use HTTPS with Secure Cookies
- Store Passwords in Plain Text Files
- Apply Weak Encryption
- Implementing User-Friendly Passwords
Using HTTPS with secure cookies ensures data transmission security, protecting user credentials. Storing passwords in plain text files is highly insecure. Weak encryption is also insufficient. User-friendly passwords, while a good practice, do not directly ensure security; the infrastructure does.
Once a constant is defined in PHP, can its value be changed later in the script?
- Yes
- No
- Only within functions
- Only within classes
In PHP, once a constant is defined, its value cannot be changed later in the script. Constants are intended to hold values that do not change during the script's execution.
What is the significance of the E_ALL constant in PHP error reporting?
- Report all error types
- Ignore all errors
- Log errors to a file
- Display errors on the webpage
The E_ALL constant in PHP error reporting is significant because it represents a bitmask that includes all error types. When used, it tells PHP to report and handle all types of errors in your code.
How can you prevent the inheritance of an exception class in PHP?
- Declare the exception class as 'final'
- Use 'private' inheritance
- Implement 'protected' inheritance
- Use 'static' keyword inheritance
In PHP, you can prevent inheritance of a class by declaring it as 'final.' This ensures that no other class can extend it. Exception classes are often made final to prevent further customization or accidental overriding of their behavior.