Skip to main content

Top 10 Essential PHP Interview Questions & Answers

P- PHP learning Tool practice MCQS 
P1. What are the main data types in PHP?
PHP supports scalar types: int, float, string, bool; compound types: array, object; and special types: null, resource. PHP is loosely typed – variables are cast automatically based on context.

---

2. What is the difference between include and require?
· include emits a warning and continues execution if the file is not found.
· require throws a fatal error and stops the script.
  Use require for critical dependencies (e.g., configuration), include for optional content.

---

3. Explain PHP superglobals. Name a few.

Superglobals are built-in variables available in all scopes. Key ones:

· $_GET – URL query parameters
· $_POST – form data sent via HTTP POST
· $_SESSION – session data
· $_COOKIE – client-side cookies
· $_SERVER – server and execution environment info
· $_FILES – uploaded files

---

4. How do you connect to a MySQL database securely?

Use PDO or MySQLi with prepared statements to prevent SQL injection. Example with PDO:
```php
$pdo = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
$stmt = $pdo->prepare('SELECT * FROM users WHERE email = ?');
$stmt->execute([$email]);
$user = $stmt->fetch();
```

---

5. What is the difference between == and === in PHP?
· == (loose equality) compares values after type juggling (e.g., 1 == '1' is true).
· === (strict equality) checks both value and type – 1 === '1' is false.

---

6. Explain basic OOP concepts in PHP: class, inheritance, polymorphism.

· Class – blueprint for objects.
· Inheritance – a child class extends a parent using extends, gaining all non‑private properties/methods.
· Polymorphism – methods with the same name can behave differently across classes (achieved via interfaces or abstract classes).
  Example:

```php
abstract class Animal {
    abstract public function makeSound();
}
class Dog extends Animal {
    public function makeSound() { echo "Bark"; }
}
```

---

7. How do you handle exceptions in PHP?

Use try, catch, and optionally finally:

```php
try {
    // code that may throw an exception
} catch (Exception $e) {
    echo 'Caught: ' . $e->getMessage();
} finally {
    // always executed
}
```

You can also create custom exception classes by extending Exception.

---

8. Sessions vs. Cookies – what’s the difference?
· Sessions store data on the server; a session ID is stored in a cookie (or URL). Data is temporary and cleared when the session ends.
· Cookies store data on the client side (browser) and have expiration dates. They are limited to ~4KB and are less secure.

---

9. How can you prevent XSS (Cross‑Site Scripting) attacks?

Always escape output. Use htmlspecialchars($string, ENT_QUOTES, 'UTF-8') when rendering user‑supplied data in HTML. For input, validate and sanitize, but never trust user input.

---

10. What are PHP magic methods? Give two examples.
Magic methods start with __ and are called automatically.

· __construct() – invoked when an object is instantiated.
· __toString() – defines how an object behaves when treated as a string.
  Others: __get(), __set(), __call(), __destruct(), etc.

--- PHP learning Tool practice MCQS 

These questions cover the fundamentals, security, and modern practices – a solid foundation for any PHP developer. Good luck!

Comments

Popular posts from this blog

How to Generate Images with Gemini AI and Convert Them into Videos

Introduction Artificial Intelligence Artificial Intelligence has completely changed the way we create and share digital content. One of the most exciting innovations is Gemini AI, Google’s advanced multimodal AI model that can work with text, images, and more. With Gemini AI, you can generate realistic and creative images just by giving a text prompt. Once you have the images, you can also convert them into professional-looking videos for YouTube, Instagram, Facebook, or Blogger. In this article, you will learn step by step how to generate AI images using Gemini AI and then how to turn those images into videos. This guide is written for beginners, so even if you are new to AI tools, you can follow along easily. --- What is Gemini AI? Gemini AI is Google’s latest artificial intelligence model, developed as an upgrade to Bard. Unlike traditional AI tools that focus only on text, Gemini is multimodal, meaning it can handle: Text Images Audio Code And more For content creators, the most po...

UGC Act Strengthening India’s Academic Integrity: Enforcing DigiLocker/NAD Verification and Cracking Down on Fake Universities

UGC Act Strengthening India’s Academic Integrity : Enforcing DigiLocker/NAD Verification and Cracking Down on Fake Universities Introduction In India, higher education and employment are deeply connected: degrees determine eligibility for jobs, further study, and professional credibility. Yet, a persistent problem continues to undermine the hopes and hard work of genuine graduates — fake or unrecognized universities issuing invalid degrees, leading to career setbacks, lost opportunities, and deep frustration among legitimate jobseekers.  The Times of India This Article explores:  What fake universities are How the University Grants Commission (UGC) Act 1956 defines degree-granting authority ✔ The role of digital systems like DigiLocker and National Academic Depository (NAD) in verification ✔ Why better policies are needed now ✔ A proposed roadmap to ensure fair employment for valid degree holders 1. What Are Fake or Unrecognized...

Future Skills That Will Create New Industries

Future Skills That Will Create New Industries (Human-led innovation in the age of advanced technology) built by machines alone. They will be imagined, designed, operated, and expanded by human curiosity, courage, and creativity.  Technology will act as a tool, but people will remain the core creators. As humanity prepares for space travel, aerial mobility, bio-design, climate engineering, and immersive realities, entirely new sectors will emerge—sectors that do not yet fully exist today. Below is a deep exploration of future skills and the new industries they will create, along with the kinds of jobs and opportunities that will arise for people. .1. Space Habitat Design New Industry: Human Living Systems in Space As space missions evolve from short visits to long-term habitation, humans will need environments where they can live, work, and thrive beyond Earth. This creates an industry focused on designing livable ecosyst...