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.
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.
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.
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.
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.
Given the PHP expression $a && $b, if $a is false, what can be said about the evaluation of $b?
- $b will not be evaluated
- $b will be evaluated
- $b will always be false
- $b will always be true
In a logical AND (&&) expression, if the left operand ($a) is false, PHP does not evaluate the right operand ($b). This is called "short-circuiting" and is useful for efficiency and avoiding errors.
When accepting user file uploads, one should never trust the file's ________ as it can be easily spoofed.
- Extension
- Size
- Content-Type
- Location
Trusting the file extension is risky, as attackers can easily change it. It's better to verify the file's content and use other security measures.
You are developing a RESTful API in PHP and want to ensure that the response is always in JSON format. Which PHP function would you primarily use to convert the data to this format?
- json_encode()
- json_decode()
- serialize()
- unserialize()
The json_encode() function in PHP is used to convert data to JSON format. It's essential for generating JSON responses in a RESTful API.
In PHP, the ________ function can be used to return a part of a string.
- substr()
- split()
- slice()
- cut()
The 'substr()' function in PHP is used to extract a portion of a string. It takes the original string and specifies the start and length of the substring to be returned.
A developer wants to store multiple values in a single variable. Which of the following data types in PHP should he/she use?
- Array
- String
- Integer
- Object
To store multiple values in a single variable, an array should be used in PHP. Arrays can hold various data types and are versatile for handling collections of data.