0% found this document useful (0 votes)
5 views23 pages

PHP Viva

The document covers fundamental concepts of PHP, including expressions, control statements, arrays, functions, object-oriented programming, forms, and database operations. It explains PHP syntax, variable scope, data types, and various operators, as well as how to handle arrays and functions. Additionally, it discusses form handling, validation, session management, and MySQL database interactions.

Uploaded by

sakshi1482005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views23 pages

PHP Viva

The document covers fundamental concepts of PHP, including expressions, control statements, arrays, functions, object-oriented programming, forms, and database operations. It explains PHP syntax, variable scope, data types, and various operators, as well as how to handle arrays and functions. Additionally, it discusses form handling, validation, session management, and MySQL database interactions.

Uploaded by

sakshi1482005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 23

Chapter 1)Expressions & Control Statements in PHP

1) PHP full form?


Ans: Hypertext Preprocessor. PHP scripts are
executed on the server and returned as plain HTML
on browser.

2)Comments in PHP.
Ans: Single line->// or #
Mulitple line->/* ….
*/

3)Case sensitivity in PHP.


Ans: In php keywords, classes, functions and user
defined functions are not case sensitive. But all the
variable names are case sensitive.
4) Rules for declaring PHP variables.
Ans:
1. A Variable starts with $sign, followed by name of
variable name.
2. A Variable name must start with a letter or
underscore character.
3. A Variable name can’t start with a number.
4. Variable names are case sensitive.
5. PHP variables can be of any length and doesn’t
contain spaces.

5) Explain Variable Scope.


Ans:
1. Local Scope = A variable declared inside a function
has a local scope and can only be accessed within that
function.
2. Global Scope = A variable declared outside a function
has a global scope and can't be accessed inside the
function unless using the global keyword.
3. Static Keyword = The static keyword is used to keep
a variable's value even after the function has ended.
4. Function Parameters = Parameters are values
passed to a function to perform specific operations.
6) Explain the operators.
Ans:
1. Arithmetic Operators:-
Perform basic math operations.
+ (Add), - (Subtract), * (Multiply), / (Divide), % (Modulus)

2. Assignment Operators
Assign values to variables.
= (Assign), +=, -=, *=

3. Comparison Operators
Compare values.
== (Equal), != (Not Equal), > (Greater), < (Less)

4. Logical Operators
Combine conditions.
&& (And), || (Or), ! (Not)

5. Increment/Decrement Operators
Increase or decrease values by 1.
++$x (Pre-increment), $x++ (Post-increment)
6. String operators
String operators are specifically designed for strings.
. -> for concatenation
.= ->for concatenation assignment

7) Explain decision making statements.


Ans:
1. if Statement
Executes code if the condition is true.
2. if-else Statement
Executes one block if true and another if false.
3. if-elseif-else Statement
Checks multiple conditions.
4. Switch Statement
Best for checking multiple values of a variable.
5. Nested if
A nested if means an if statement inside another if.
It is used when multiple conditions need to be checked.
8) Explain Loop Control Structure.
Ans:
1. while Loop
Repeats as long as the condition is true.
2. do-while Loop
Runs at least once and then repeats while the condition
is true.
3. for Loop
Best when you know how many times to run the loop.
4. foreach Loop
Used to loop through arrays.

9) Data types in PHP


Ans:
Pre defined data types=Boolean, integer, double, string.
User defined data types=arrays,objects.
Special data types=null, resource.
Chapter 2) Arrays, Functions & Graphics
1) What is an Array and explain it’s types?
Ans: An array in PHP is a collection of similar type of data.
We can create an array using an array() function.
1. Indexed Array =
Indexed Arrays can store numbers, strings and any object
but their index will be represented by numbers.
Eg = $fruits = ["Apple", "Banana", "Mango"];
echo $fruits[1]; // Output: Banana
2. Associative Array =
An array having string keys is typically called as
Associative Array.
Eg= $person = ["name" => "Sakshi", "age" => 19];
echo $person["name"]; // Output: Sakshi
3. Multidimensional Array =
An array containing other arrays inside it (like a table) is
called a multidimensional array.
Eg= $students = [
["Sakshi", 19],
["Diya", 20]
];
echo $students[0][0]; // Output: Sakshi

2) Explain how to extract data from arrays.


Ans: We can extract data from arrays by using extract()
and compact() functions.
1. Extract() = It extracts the data from arrays and store it
in variables.
a. EXTR_OVERWRITE -> (by default)
If a variable with the same name already exists, it will be
replaced by the array value.
b. EXTR_SKIP ->
If a variable with the same name already exists, it will not
be replaced.
c. EXTR_PREFIX_SAME ->
If a variable with the same name exists, it will add a prefix
to the extracted variable.
d. EXTR_PREFIX_ALL ->
It adds a prefix to all extracted variables from the
array, even if there are no conflicts.
2. compact() = This function creates an array from
variables and their values. Here, variable names are
converted to key and variable values are converted to
array values.

3)Explain Implode and Explode function.


Ans:
1. Implode() = It is used to convert an array into a string.
Syntax – implode(separator, array)
2. Explode() = It is used to convert a string into an array.
Syntax – explode(separator,string,limit)

4)What is array_flip()?
Ans: The array_flip() function flips or exchanges all keys
with their associated values in an array.
Syntax – array_flip(array)

5)What is traversing array?


Ans: Traversing an array means to visit each and every
element of array using a looping structure and iterator
functions.
6)What is function and explain it’s types.
Ans: A function is a block of code which is used to perform
some specific tasks.
Two types of functions :-
1. built-in functions = fopen(), fwrite(), fclose()
2. User defined functions =
7) String Functions/ Operations provided by Php.
Ans:
1. str_word-count() = counts the total words in a string.
Syntax – str_word_count(string);
2.strlen() = returns the length of the string
Syntax = strlen(string);
3. strrev() = reverse a string
Syntax = strrerv(string);
4. strops() = finds the position of the first occurrence of the
string.
Syntax = strpos(string, word);
5. strrpos() = finds the position of the last occurrence of
the string.
Syntax = strpos(string, word);
6. str-replace() = replaces some characters with some
other characters in a string.
7. ucwords() = converts the first character of each word to
uppercase.
Syntax = ucwords($string, $seperator)
8. strtoupper() = converts the string to uppercase
Syntax = strtoupper($string)

9. strtolower() = converts the string to upperlower


Syntax = strtolower($string)
10. strcmp() = compares two strings and is case sensitive.
Syntax = strcmp($string1, $string2);
Chapter 3) Object Oriented Concepts in
PHP

1) What is a class?
Ans: A class is a blueprint of an object. Using a class we
can create many objects. A class itself is made up of
properties and methods.
Properties are different types of data objects like numbers,
string, Boolean etc. whereas Methods are functions that
operate on the data.
2) What is an Object?
Ans: An object is an instance or occurance of a class eg.
Laptop, car etc.
3) What is $this keyword?
Ans: $this is a reserved keyword that refers to calling that
particular object. I t is usually the object to which the
method belongs.
4) What are the three levels of visisbility?
Ans:
1.Public → Accessible from anywhere.
2.Protected → Accessible within the class and by child
classes.
3.Private → Accessible only within the class where it is
defined.

5) Define Constructor with it’s types.


Ans: A constructor is a special member function that runs
automatically when an object of a class is created. It is
used to initialize the object’s properties.
Syntax= __constructor()
Types:-
1. Default Constructor
2. Parameterized Constructor

6)Define Destructor.
Ans: A destructor is a special member function that is
called automatically when an object is destroyed or the
script ends. It is mainly used for cleanup tasks like closing
database connections or freeing resources.

7) What is Inheritance and explain types?


Ans: Inheritance allows child class to reuse the properties
and methods of parent class.
Types of inheritance=
1) Single inheritance: one child class inherits from one
parent class
2) Multilevel inheritance: one child class inherits from a
parent class and then another inherits from that child
class.
3) Hierarchial inheritance: multiple child classes inherit
from one parent class.
8) What is object cloning?
Ans: Object cloning is the process to create a copy of an
object. It is done by using clone keyword. When an object
is cloned, PHP will perform a shallow copy of all the
object’s properties.
Syntax= $copy_object_name=clone $object_to_b_copied

9)What is introspection?
Ans: Introspection is the ability of a program to check
name, properties or methods.
PHP Introspection functions for examining classes=
1. class_exists()
2. get_class_methods()
3. get_class_vars()
PHP Introspection functions for examining objects=
1. is_object()
2. get_object_vars()
3. method_exists()

10) What is Serialization?


Ans: Serialization refers to converting an object into byte
stream representation that can be stored into a file. This is
useful for persistent data.
Serialize() :- It is an inbuilt function which is used to
serialize the given array.
Syntax= serialize($values_in_form_of_array)
Unserialize() :- It is an unbuilt function that is used to
unserialize the given serialized array to get back to it’s
original value o complex array.
Syntax= unserialize($serialized array)
11) What are the magic methods?
Ans:
1. __sleep method() = it is used when we want to serialize
an object. It is used in cleaning up the pending tasks.
2.__wakeup method() = it is used when we want to de-
serialize an object. It is used in res-establishing the
database or any other connection that has been lost during
serialization process.
12) What is method overriding?
Ans: PHP does not support traditional method
overloading (same method name with different
parameters). Method Overriding in PHP means defining a
method in the child class with the same name and same
parameters as a method in the parent class

Chapter 4) Creating & Validating Forms


1) What are forms in PHP?
Ans: Forms are used to collect user input and send it to a
server for processing.

2) What are the form attributes and form controls?


Ans:
Attributes =
 action → URL to send form data.
 method → HTTP method (GET or POST).
 name → specifies the name of the form
Form Controls =
 Textbox → Single-line input for text.
 Textarea → Multi-line text input.
 Checkbox → Select multiple options.
 Radio Button → Select one option from a group.
 Dropdown → Choose one or multiple options from a list.
 Button → Submits, resets, or performs actions.

3) What are the attributes of input tag?


Ans:
 type → It is used to specify type of an input element. It’s default value
is text. (e.g., text, radio, checkbox).

 value → It is used to specify value of an input element.


 name → It is used to specify name of an input element.
4) Explain GET and POST method in PHP.
Ans:
 GET → Sends data via URL, visible in the address bar. Suitable for
simple data like search queries.
 POST → Sends data securely in the request body, not visible in the
URL. Used for sensitive data like passwords.

5) What is validation?
Ans: Validation means to check the input submitted by the
user.

6) What is session & Cookie?


Ans:
 Session → Stores user data temporarily on the server
until the browser is closed or the session expires. Server-
side, temporary.
 Cookie → Stores user data on the user's browser for a
set period, even after closing the browser. Client-side, can
be long-term.
setcookie(name, value, expire, path, domain, security);

7) How to create session variable in PHP?


Ans: A session is started using session_start() function.
Session variables are set with the PHP global variable:
$_SESSION.

8) Describe get and start session variable.


Ans:
 Start Session: session_start(); → Begins a session to
store data.
 Set Session Variable: $_SESSION['key'] = 'value'; →
Creates a session variable.
 Get Session Variable: $_SESSION['key']; → Accesses
the stored value.

9) How to destroy session?


Ans:Session Destroy=session_destroy() and
session_unset();
It deletes all session data and logs out the user.

10) PHP mail()


Ans: Used to send emails.
mail(to, subject, message, headers, parameters);
Chapter 5) Database Operations

1) What is MYSQL?
Ans: MYSQL is a relational database management system
(RDBMS) that is used to store & retrieve data.
Syntax = mysql_function(value,value,…..);
There are two types of functions :-
1. mysqli_connect($connect)
2. mysqli_query($connect,”SQL statement”);

2) What is database?
Ans: A database is a collection of data. The CREATE
DATABASE statement is used to create database in MYSQl.
$conn = mysqli_connect() =>creates connection
die() => fun that prints error msg and then exits
$sql=”CREATE DATABASE myDB”; =>Creates database
mysqli_query=>fun that executes sql queries
3) Ways of working with MYSQL & PHP.
Ans:
1. MySQLi (Procedural) → Simple, easy to use, specific
to MySQL.
2. PDO (PHP Data Objects) → Supports multiple
databases, secure, uses prepared statements.

4) Database Operations
Ans: mysql_query() function is used to insert, select, delete
& update record in a table.
1. Insert Data: Add data to a table.
INSERT INTO table_name (column1, column2) VALUES
('value1', 'value2');
2. Retrieve Data: Get data using a query.
SELECT * FROM table_name;
3. Update Data: Modify existing data.
UPDATE table_name SET column1='new_value' WHERE
condition;
4. Delete Data: Remove data from a table.
DELETE FROM table_name WHERE condition;
5. Create Table: Make a new table.
CREATE TABLE table_name (id INT, name VARCHAR(50));
6. Drop Table: Delete an entire table.
DROP TABLE table_name;
5) What are the datatypes in MYSQL.
Ans: Numeric, DATETIME, DATE, TIMESTAMP

6) State the use of given commands.


Ans:
1.mysql_fetch_row()
 Fetches a row from a result set as a numeric array (0,
1, 2...).
2.mysql_fetch_array()
 Fetches a row as an associative array, numeric
array, or both.

You might also like