PHP and MySQL Lab Viva Questions
Basic Concepts and Definitions
Q: What is PHP?
A: PHP originally stood for 'Personal Home Page', but now it stands for 'Hypertext
Preprocessor'. It is a widely-used open source server-side scripting language.
Q: What are the features of PHP?
A: Open-source, platform-independent, easy to learn, supports databases, faster
execution, embedded in HTML.
Q: What is a variable in PHP?
A: A variable is used to store data. It starts with a $ symbol. E.g., $name = 'John';
Q: What is a constant in PHP?
A: Constants are like variables but once defined, they cannot be changed. Defined using
define(): E.g., define('PI', 3.14);
Q: What is the use of $ symbol in PHP?
A: It is used to declare a variable. E.g., $age = 25;
Q: What is the 'this' keyword in PHP?
A: Used inside a class to refer to the current object. E.g., $this->name = 'Student';
Viva Questions Based on Lab Programs
Q: How do you print text in PHP?
A: Using echo or print, e.g., echo 'Hello World';
Q: How do you check if a number is even in PHP?
A: Use modulus: if ($num % 2 == 0)
Q: How do you find the maximum of three numbers?
A: Using nested if conditions or max() function.
Q: How do you swap two variables in PHP?
A: Using a third variable or arithmetic operations.
Q: What is a factorial and how is it calculated?
A: It's the product of all positive integers up to n. Example: 5! = 5*4*3*2*1 = 120
Q: What is a palindrome number?
A: A number that reads the same forwards and backwards. Example: 121
Q: How do you reverse a number and find its sum?
A: Use loops or convert to string and apply strrev().
Q: What is recursion in Fibonacci series?
A: A function calling itself with smaller inputs. E.g., fib(n) = fib(n-1) + fib(n-2)
Q: Name seven string functions in PHP.
A: strlen(), strrev(), strtoupper(), strtolower(), str_replace(), substr(), strpos()
Q: How do you insert an item into an array?
A: Using array_push() or $array[] = value;
Q: What is a constructor and a destructor?
A: Special functions in a class for initialize and cleanup. Constructor: __construct(),
Destructor: __destruct(). (Use double underscore)
Q: What is the use of GET and POST methods?
A: To send form data to the server. GET appends to URL, POST is sent in request body.
Q: How is form data retrieved in PHP?
A: Using $_GET or $_POST superglobals. E.g., $_POST['name']
Q: How do you handle a form using POST?
A: Create form with method='post' and use $_POST to retrieve data.
Q: How do you check if a number is prime using form input?
A: Use loops and check divisibility conditions. A number is prime if divisible only by 1
and itself.
Q: How do you get the current date and time in PHP?
A: Use date('Y-m-d H:i:s');
Q: How do you upload a file in PHP?
A: Using $_FILES superglobal and move_uploaded_file().
Q: How do you connect to a MySQL database?
A: Use mysqli_connect('localhost', 'user', 'pass', 'db');
Q: What is the SQL command to create a table?
A: CREATE TABLE table_name (columns...);
Additional Theory Questions with Examples
Q: What is MySQL?
A: MySQL is an open-source relational database system used to manage data in
structured tables.
Q: What is the difference between GET and POST?
A: GET (not secure) sends data via URL; POST sends it securely via HTTP body.
Example: form method='post'
Q: What are PHP superglobals?
A: Built-in variables like $_GET, $_POST, $_SESSION used to manage global data.
Q: What is the difference between echo and print?
A: echo is faster and can print multiple values, while print returns 1. Example: echo 'Hi';
Q: What is isset() and empty()?
A: isset() checks if variable exists; empty() checks if it has no value.
Q: How to manage user sessions in PHP?
A: Using session_start(), $_SESSION, and session_destroy().
Q: Difference between include and require?
A: require stops execution on failure; include does not.
Q: What are cookies?
A: Small files stored in the browser. Set using setcookie() function.
Q: What is form validation?
A: Checking user inputs before processing. Example: validate empty fields or email
format.
Q: What is PDO?
A: PHP Data Objects for accessing databases securely using prepared statements.
Q: What are magic constants in PHP?
A: __LINE__, __FILE__, __DIR__ – used for debugging and metadata.
Q: Difference between client-side and server-side scripting?
A: Client-side (HTML/JS) runs in browser; server-side (PHP) runs on the server.