You are building a web application that needs to process user-uploaded images. Which predefined PHP function can be used to get the width and height of an image?
- getimagesize()
- imagecreatefromjpeg()
- imagecopy()
- imagesetpixel()
The getimagesize() function in PHP is used to retrieve the width and height of an image, making it suitable for processing user-uploaded images.
To mitigate the risk of XSS attacks, developers should ________ any data that is output to the browser.
- Escape
- Encrypt
- Validate
- Sanitize
Developers should "Escape" any data that is output to the browser to protect against XSS (Cross-Site Scripting) attacks. Escaping ensures that user-generated content is displayed as plain text, preventing script execution.
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.
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.
How can you differentiate between a 'directory' and a 'file' using PHP functions?
- is_dir() checks for directory
- file_exists() checks for a file
- scandir() lists the directory's contents
- is_file() checks for a file
The is_dir() function in PHP is used to determine if a given path is a directory. It's a key function for distinguishing directories from files.
Using ________ ensures that even if two users have the same password, their hashed values will be different.
- Salted hashing
- Hash collision prevention
- Unique salting
- Hashed password variation
Utilizing salted hashing in password storage involves adding a unique random "salt" to each user's password before hashing it. This prevents identical passwords from having the same hashed value.
What is the result of the expression 5%35%3 in PHP?
- 1
- 0
- 2
- 3
In PHP, the % operator is used for modulus. In this expression, 5%35 first results in 5, then 5%3 results in 2. So the answer is 2.
In PHP OOP, the concept of defining multiple methods with the same name but different parameters is called ________.
- Method Overloading
- Method Overriding
- Method Polymorphism
- Method Encapsulation
In PHP OOP, method overloading allows you to define multiple methods with the same name but different parameters, promoting code reusability.
Imagine you're working on an e-commerce platform, and you need to format a number to two decimal places for displaying prices. Which of the following PHP functions would be most appropriate?
- number_format()
- round()
- floor()
- ceil()
The number_format() function is the most suitable for formatting numbers with two decimal places, making it ideal for displaying prices in an e-commerce context.
Which predefined PHP function can be used to reverse the order of the elements in an array?
- array_reverse()
- array_flip()
- array_rotate()
- array_shuffle()
The array_reverse() function is used to reverse the order of elements in an array. array_flip() flips the keys and values, while array_rotate() and array_shuffle() are not valid functions.
To prevent session hijacking, it's a good practice to bind the session to the user's ________.
- IP Address
- Hardware Token
- Browser Fingerprint
- User Credentials
To prevent session hijacking, binding the session to the user's "Browser Fingerprint" is recommended. This involves tracking unique characteristics of the user's browser to ensure session continuity and security.
You have two arrays, one with keys and another with values. You need to combine them such that keys from the first array map to values from the second. Which PHP function would you use?
- array_merge()
- array_combine()
- array_diff()
- array_search()
The array_combine() function takes two arrays and creates a new array where the keys are from the first array and values from the second array.