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.
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.
You are working on a project where certain methods in your classes should only be defined but not implemented, and the actual implementation should be provided by the derived classes. Which PHP concept helps achieve this?
- Interface
- Trait
- Abstract Class
- Final Class
An interface defines method signatures without implementations. Derived classes that implement the interface must provide the implementation for those methods. This is a common approach in PHP to achieve this requirement.
To check the data type of a variable in PHP, which function is used?
- is_type()
- gettype()
- datatype()
- typeof()
To check the data type of a variable in PHP, you use the gettype() function. This function returns a string indicating the data type of the variable, such as "integer," "string," or "array."
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.