Interview QuestionsPHP Interview Question

Top 100 PHP Interview Questions and Answers

Top 100 PHP Interview Questions and Answers

Here are 100 real-time PHP interview questions and answers, categorized into Basic, Core, OOP, Database, Security, and Advanced sections. These are ideal for freshers, mid-level, and senior developers.


🟒 BASIC PHP (1–20)

  1. What is PHP?
    PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.
  2. What does PHP stand for?
    PHP: Hypertext Preprocessor.
  3. Is PHP case-sensitive?
    Partially. Variables are case-sensitive; functions are not.
  4. How do you declare a variable in PHP?
    Using the $ sign, like $name = "John";.
  5. Difference between echo and print?
    echo is slightly faster and can take multiple parameters; print returns 1.
  6. What are the different data types in PHP?
    String, Integer, Float, Boolean, Array, Object, NULL, Resource.
  7. What is a constant?
    A name or identifier for a simple value that cannot change during script execution. Define using define().
  8. What are the types of arrays in PHP?
    Indexed, Associative, and Multidimensional.
  9. How to find the length of a string?
    strlen($string);
  10. How to count array elements?
    count($array);
  11. How do you include files in PHP?
    Using include, include_once, require, or require_once.
  12. Difference between include() and require()?
    require() causes a fatal error on failure; include() only a warning.
  13. What are Superglobals?
    Predefined variables like $_GET, $_POST, $_REQUEST, $_SESSION, etc.
  14. What is the difference between GET and POST?
    GET displays data in URL; POST hides it.
  15. How to connect to MySQL using PHP?
    Using mysqli_connect() or PDO.
  16. What is isset()?
    Checks if a variable is set and not null.
  17. What is empty()?
    Checks whether a variable is empty.
  18. What is die()?
    Terminates the script and outputs a message.
  19. What is the use of header()?
    Sends raw HTTP headers (e.g., redirects).
  20. What is a session in PHP?
    A way to store data across multiple pages using $_SESSION.

πŸ”΅ CORE PHP (21–50)

  1. How to start and end PHP code?
    <?php ... ?>
  2. What is the default file extension of PHP?
    .php
  3. What is the difference between == and ===?
    == checks value; === checks value and type.
  4. What is a constructor?
    Special function called automatically when an object is created.
  5. What is a destructor?
    Special function called when an object is destroyed.
  6. What is a magic method in PHP?
    Methods starting with __, like __construct(), __get(), __set().
  7. What is the use of explode()?
    Splits a string into an array.
  8. What is implode()?
    Joins array elements into a string.
  9. What is a cookie?
    Data stored on the client’s browser.
  10. How to set a cookie?
    setcookie(name, value, expire);
  11. How to delete a cookie?
    Set its expiry time in the past.
  12. Difference between mysql and mysqli?
    mysql is deprecated; mysqli is improved with more functionality.
  13. What is PDO?
    PHP Data Objects – a database access abstraction layer.
  14. What is the use of require_once?
    Includes a file only once to avoid duplication.
  15. What is error handling in PHP?
    Using try, catch, and finally blocks.
  16. How to send email in PHP?
    Using the mail() function.
  17. What is a resource in PHP?
    A special variable holding a reference to an external resource (e.g., database).
  18. What is nl2br()?
    Inserts HTML line breaks before newlines in a string.
  19. How to redirect a page in PHP?
    Using header("Location: url");
  20. What is array_merge()?
    Merges two or more arrays.
  21. What is the ternary operator?
    A shorthand for if-else: $x = ($a > $b) ? $a : $b;
  22. What is the difference between foreach and for?
    foreach is used for arrays; for is used for loops with counters.
  23. How to suppress errors in PHP?
    Using @ before a function.
  24. How to define a function in PHP?
    function functionName() { }
  25. What is recursion?
    A function calling itself.
  26. What is type hinting?
    Specifying expected data types for function arguments.
  27. What is require_once used for?
    Ensures a file is included only once.
  28. How to create a class in PHP?
    class ClassName { }
  29. How to create an object?
    $obj = new ClassName();
  30. What is this keyword?
    Refers to the current object inside a class.

🟠 PHP & DATABASE (51–70)

  1. What is SQL Injection?
    A security vulnerability when user input is improperly handled in SQL queries.
  2. How to prevent SQL Injection?
    Use prepared statements with mysqli or PDO.
  3. How to insert data into MySQL with PHP?
    Using INSERT INTO query via mysqli_query().
  4. How to fetch data from a table?
    Use SELECT query and loop with mysqli_fetch_assoc().
  5. How to update a record in MySQL?
    Use UPDATE query.
  6. How to delete a record?
    Use DELETE query.
  7. How to check if a table exists in PHP?
    Use SHOW TABLES LIKE 'tablename'.
  8. How to connect using PDO?
    $pdo = new PDO('mysql:host=localhost;dbname=test', 'user', 'pass');
  9. How to handle DB errors in PDO?
    Use try/catch block and set error mode.
  10. How to fetch rows in PDO?
    $stmt->fetch(PDO::FETCH_ASSOC);
  11. What are stored procedures?
    Predefined SQL code saved in the database.
  12. What is mysqli_real_escape_string()?
    Escapes special characters to prevent SQL injection.
  13. Difference between fetch_assoc() and fetch_row()?
    assoc returns associative array; row returns numeric array.
  14. How to count rows from a result?
    mysqli_num_rows($result);
  15. How to close a DB connection?
    mysqli_close($conn);
  16. What is auto_increment?
    Automatically increases numeric ID on new row.
  17. What is a primary key?
    Uniquely identifies each row.
  18. What is foreign key?
    Links one table to another.
  19. Difference between inner join and left join?
    Inner join matches only common records; left join returns all left records.
  20. What is indexing in databases?
    Used to speed up data retrieval.

🟣 OOP in PHP (71–90)

  1. What is OOP?
    Object-Oriented Programming; organizes code using classes and objects.
  2. Pillars of OOP?
    Encapsulation, Inheritance, Polymorphism, Abstraction.
  3. What is inheritance?
    A class can inherit properties and methods from another class.
  4. What is polymorphism?
    Ability to call the same method on different objects with different outcomes.
  5. What is encapsulation?
    Wrapping data and methods together, restricting access using access modifiers.
  6. What is abstraction?
    Hiding implementation details and showing only the interface.
  7. What are access modifiers?
    public, private, protected.
  8. What are interfaces?
    A blueprint for classesβ€”only method declarations, no body.
  9. What are traits?
    Code reusability tool to include methods in multiple classes.
  10. What is static keyword?
    Declares class properties or methods that can be accessed without creating an object.
  11. Can PHP support multiple inheritance?
    No. But traits can help simulate it.
  12. Difference between abstract class and interface?
    Abstract class can have both implemented and unimplemented methods; interfaces cannot.
  13. What is final keyword?
    Prevents class/method from being extended/overridden.
  14. What is a constructor chaining?
    Calling a parent constructor from child using parent::__construct().
  15. What is method overloading in PHP?
    PHP does not support traditional overloading; can be done using __call().
  16. What is method overriding?
    Redefining a parent class method in a child class.
  17. What is object cloning?
    Creating a copy using the clone keyword.
  18. What is namespace in PHP?
    Helps avoid name conflicts in larger applications.
  19. What is autoloading in PHP?
    Automatically includes class files when used.
  20. What is Composer?
    Dependency manager for PHP.

πŸ”΄ Advanced & Security (91–100)

  1. What is CSRF?
    Cross-Site Request Forgery – a type of malicious exploit.
  2. How to prevent CSRF?
    Use tokens in forms.
  3. What is XSS?
    Cross-Site Scripting – injecting malicious scripts.
  4. How to prevent XSS?
    Sanitize user input using htmlspecialchars() or strip_tags().
  5. What are PHP design patterns?
    Solutions to common software design problems (e.g., Singleton, Factory).
  6. What is curl in PHP?
    Library for sending HTTP requests.
  7. How to validate email in PHP?
    filter_var($email, FILTER_VALIDATE_EMAIL)
  8. What is .htaccess?
    Configuration file to control Apache server settings.
  9. How to increase script execution time?
    set_time_limit(seconds);
  10. How to handle large file uploads?
    Modify php.ini: upload_max_filesize, post_max_size, max_execution_time.

Related posts

Top 20 SQL Interview Questions and Answers

Engineer Robin

Top 20 SASS Interview Questions and Answers

Engineer Robin

Top 10 PHP Interview Questions and Answers

Engineer Robin

2 comments

Top 50 SASS Interview Questions and Answers - Shikshatech June 14, 2025 at 6:07 am

[…] Top 100 PHP Interview Questions and Answers […]

Reply
Our Best Top 20 SQL Interview Questions and Answers - Shikshatech June 27, 2025 at 4:29 am

[…] Top 100 PHP Interview Questions and Answers […]

Reply

Leave a Comment