PHP Interview Questions asked by Infosys Company

1. Question: What is the significance of the Spaceship Operator (<=>) introduced in PHP 7?

Answer: The Spaceship Operator is used for combined comparison. It returns 0 if both operands are equal, 1 if the left operand is greater, and -1 if the right operand is greater. It's often used for sorting.

$result = $a <=> $b; // 0 if $a equals $b, 1 if $a > $b, -1 if $a < $b
 

2. Question: Explain the use of the Null Coalescing Operator (??) in PHP 7.

Answer: The Null Coalescing Operator is used to check if a variable is set and not null. It returns the first operand if it exists and is not null; otherwise, it returns the second operand.

$value = $variable ?? "default";
 

3. Question: What are traits in PHP, and how are they different from classes?

Answer: Traits are similar to classes, but they are intended to group functionality in a fine-grained and consistent way. A class can use multiple traits, and they are intended to group functionality in a fine-grained and consistent way, unlike classes that support single inheritance.

4. Question: How can you secure your PHP sessions against session hijacking?

Answer: To secure PHP sessions against session hijacking, you can:

  • Use secure session handling functions.
  • Regenerate session IDs periodically.
  • Store session data on the server-side.
  • Use HTTPS to encrypt data transmission.

5. Question: Explain the concept of type hinting in PHP.

Answer: Type hinting is a way to declare the type of data a function should receive or return. It helps in enforcing strong typing and improves code clarity. For example:

function add(int $a, int $b): int {
    return $a + $b;
}
 

. Question: What is PSR, and why is it important in PHP development?

Answer: PSR (PHP Standard Recommendation) is a set of standards defined by the PHP Framework Interop Group (PHP-FIG) to enhance interoperability between PHP frameworks. Adhering to PSR ensures consistency and compatibility between different PHP projects.

7. Question: Explain the concept of dependency injection in PHP.

Answer: Dependency injection is a design pattern where the dependencies of a class are injected from the outside rather than created within the class. It helps in achieving loose coupling, making the code more modular and testable.

8. Question: How can you handle file uploads in PHP securely?

Answer: To handle file uploads securely in PHP, you should:

  • Use the move_uploaded_file function to move files to a secure directory.
  • Validate file types and sizes on the server-side.
  • Generate unique filenames to prevent overwriting.

9. Question: What is the use of the yield keyword in PHP?

Answer: The yield keyword is used in PHP for implementing generators. Generators allow you to iterate through a set of data without needing to create an array in memory, which can be more memory-efficient.

10. Question: Explain the concepts of late static binding in PHP.

Answer: Late static binding allows static methods to reference the calling class using the static keyword. This helps in achieving polymorphism and makes it easier to override static methods in child classes.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

49018