Skip to main content

Posts

PHP Basic Code Challenges

  Here are some PHP code challenges ranging from beginner to intermediate levels: String Manipulation Challenge: // Write a function that: // - Takes a string // - Removes all vowels // - Returns the modified string function removeVowels($str) {     return preg_replace('/[aeiou]/i', '', $str); } // Test case echo removeVowels("hello world"); // Output: "hll wrld" Array Manipulation Challenge: // Write a function that: // - Takes an array of numbers // - Returns only the unique numbers // - Preserves the original order function getUniqueNumbers($arr) {     return array_values(array_unique($arr)); } // Test case $numbers = [1, 2, 2, 3, 4, 4, 5]; print_r(getUniqueNumbers($numbers)); // Output: [1, 2, 3, 4, 5] Prime Number Checker: function isPrime($num) {     if ($num <= 1) return false;     for ($i = 2; $i <= sqrt($num); $i++) {         if ($num % $i == 0) ret...

Oops PHP interview questions

 Here are key PHP OOP interview questions and answers specifically for a microservices architect role: Q: What are SOLID principles and how do they apply to microservices? A: SOLID principles in microservices context: Single Responsibility (S): Each microservice should handle one specific business capability Open/Closed (O): Services should be extendable without modifying existing code Liskov Substitution (L): Services should be replaceable with their subtypes without affecting functionality Interface Segregation (I): Services should expose focused, minimal interfaces Dependency Inversion (D): High-level modules shouldn't depend on low-level ones; both should depend on abstractions Example: // Single Responsibility Principle class OrderService {     private PaymentGateway $paymentGateway;     private OrderRepository $orderRepository;     public function processOrder(Order $order): void {         // Handle only order processing logic...

MySQL's ACID compliance

Mysql acid compliance ACID is an acronym that stands for four key properties of database transactions: Atomicity Ensures that a transaction is treated as a single, indivisible unit of work Either all operations within a transaction are completed successfully, or none are If any part of the transaction fails, the entire transaction is rolled back to its previous state Prevents partial updates that could leave the database in an inconsistent state Consistency Guarantees that a transaction brings the database from one valid state to another valid state All data written to the database must adhere to defined rules, constraints, cascades, triggers, and other database integrity mechanisms Ensures that any transaction will not break the database's predefined rules Isolation Determines how and when changes made by one transaction become visible to other transactions Prevents interference between concurrent transactions MySQL provides different isolation levels: Read Uncommitted Read Commit...

Oops concept code test

Provide a good approach for the practical Oops concept.  There should be an employer add facility. Employer can have two types t1 and t2. There should be an option to calculate the tax. t1 and t2 will have different logics. What approach is good for php Oops? ----------------- Here's an object-oriented approach using Strategy Pattern for tax calculation: // Abstract base class for employers abstract class Employer {     protected $name;     protected $type;     protected $taxCalculator;     public function __construct($name, TaxCalculatorStrategy $taxCalculator) {         $this->name = $name;         $this->taxCalculator = $taxCalculator;     }     // Strategy for tax calculation     public function calculateTax($income) {         return $this->taxCalculator->calculate($income);     } } // Tax calculation strategy interface interfac...

Laravel Interview questions

What is a Laravel Observer? A Laravel Observer is a class that listens for specific events (such as creating, updating, deleting) on a model and performs actions in response to those events. When would you use Observers in Laravel? Observers are useful when you want to decouple the logic related to model events from the models themselves. They are particularly handy for maintaining clean, modular code and separating concerns. How do you create an Observer in Laravel? To create an Observer in Laravel, you typically use the make:observer Artisan command, which generates a new Observer class. You then define the event listeners (e.g., created , updated , deleted ) within that class. What are the differences between using Observers and using Model Events directly? Observers provide a cleaner and more organized way to handle model events compared to directly attaching listeners to model events. They promote better separation of concerns and make it easier to maintain and test your code. Ca...