PHP Theory Viva Questions & Answers
Q: What is PHP?
A: PHP (Hypertext Preprocessor) is a widely-used open-source server-side scripting language that
is especially suited for web development and can be embedded into HTML.
Q: What are the key features of PHP?
A: Open-source, platform-independent, integrates with HTML, supports databases, server-side
execution, loosely typed.
Q: What are the different data types in PHP?
A: String, Integer, Float, Boolean, Array, Object, NULL, Resource.
Q: What is the difference between echo and print in PHP?
A: echo can output one or more strings and is faster; print returns a value (1) and can be used in
expressions.
Q: What are superglobals in PHP?
A: Built-in variables like $_GET, $_POST, $_REQUEST, $_SESSION, $_COOKIE, $_SERVER,
$_FILES.
Q: What is the difference between GET and POST?
A: GET shows data in URL and is limited; POST hides data and is more secure.
Q: What is a session in PHP?
A: Used to store user data across multiple pages using $_SESSION.
Q: What is a cookie?
A: A small file stored on the clients system using setcookie().
Q: How do you connect to a MySQL database in PHP?
A: Using mysqli_connect("localhost", "username", "password", "database");
Q: What is the difference between include and require?
A: include gives a warning if file is missing, require gives a fatal error.
Q: What is an associative array?
A: Array with named keys like $student = ["name" => "Prakash", "marks" => 85];
Q: What are functions in PHP?
A: Reusable block of code declared using function keyword.
Q: What is object-oriented programming in PHP?
A: Supports classes, objects, inheritance, encapsulation, and polymorphism.
Q: How do you define constants in PHP?
A: Using define("CONST_NAME", "value");
Q: What is the difference between == and ===?
A: == compares value, === compares value and type.
Q: How do you handle errors in PHP?
A: Using error_reporting(), try...catch, set_error_handler().
Q: What is the use of isset() and empty()?
A: isset() checks if variable is set, empty() checks if it's empty.
Q: What is explode() in PHP?
A: Splits a string into an array using a delimiter.
Q: What is the use of require_once?
A: Includes a file only once, preventing redeclaration errors.
Q: What is the difference between server-side and client-side scripting?
A: Server-side runs on the server (PHP), client-side runs in browser (JavaScript).