1.
Attempt any FIVE (2 marks each):
(a) Describe the advantages of PHP.
For 2 marks, focus on two key advantages with brief explanations:
* Open Source: PHP is open-source, meaning it’s free to use, which reduces
development costs.
* Easy to Learn: PHP has a relatively simple syntax, making it easier for
beginners to learn and use for web development.
(b) What is an array? How to store data in an array?
* What is an array? An array is a data structure that stores multiple values
under a single variable name.
* How to store data? Data can be stored using the array() function or square
bracket [] syntax: $colors = array(“red”, “blue”); or $colors[] = “red”;
$colors[] = “blue”;
© List types of inheritance.
For 2 marks, list two types:
* Single inheritance
* Multiple inheritance
(d) How can we destroy cookies?
Cookies are destroyed by setting their expiration time to a past date using
the setcookie() function.
€ List any four data types in MySQL.
For 2 marks, list four:
* INT
* VARCHAR
* TEXT
* DATE
(f) Write syntax of PHP.
PHP code is embedded within HTML using <?php ?> tags. Variables are
declared using a dollar sign ($), e.g., $myVar = “Hello”;
(g) How to create a session variable in PHP?
Session variables are created using the $_SESSION superglobal array after
starting a session with session_start();.
2. Attempt any THREE (4 marks each):
(a) Write down rules for declaring PHP variables.
For 4 marks, provide four rules:
* Variables start with a dollar sign ($).
* Variables must start with a letter or an underscore.
* Variables are case-sensitive.
* Variables can contain alphanumeric characters and underscores.
(b) Write a program to create an associative array in PHP.
For 4 marks, provide the code and a brief explanation:
<?php
$age = array(“Peter”=>”35”, “Ben”=>”37”, “Joe”=>”43”); // Creates an
associative array with names as keys and ages as values.
Echo “Peter is “ . $age[‘Peter’] . “ years old.”; // Accesses and prints a value
using its key.
?>
An associative array uses names as keys.
© Define Introspection and explain it with a suitable example.
For 4 marks:
* Define Introspection: Introspection is the ability of a program to examine
its own structure or the properties of objects during runtime.
* Example:
<?php
Class MyClass {
Public $myProperty = “Test”;
$obj = new MyClass();
Echo get_class($obj); // Example of introspection function to get class name
?>
Get_class() is used to get the class name of an object.
(d) Write the difference between the get() & post() methods of form (Any four
points).
For 4 marks, provide a table with four differences:
| Feature | GET | POST |
| Data in URL | Visible | Not visible |
| Data Length | Limited | Larger data |
| Security | Less secure | More secure |
| Bookmarking | Can be bookmarked | Cannot be bookmarked |
3. Attempt any THREE (4 marks each):
(a) Define function. How to define user-defined function in PHP? Give
example.
For 4 marks:
* Define function: A function is a block of code that performs a specific task
and can be reused.
* How to define: User-defined functions are defined using the function
keyword: function myFunction() { //code }
* Example:
<?php
Function greet($name) {
Echo “Hello, “ . $name;
Greet(“World”);
?>
This defines and calls a function to display a greeting.
(b) Explain method overloading with example.
For 4 marks:
* Explain: Method overloading is the ability to define multiple methods with
the same name but different parameters within a class. PHP doesn’t directly
support traditional method overloading.
* Example (Conceptual – PHP doesn’t do this directly):
<?php
Class Calc {
Function add($x, $y) {}
Function add($x, $y, $z) {} //PHP would only keep this one
?>
PHP would only recognize the last defined add function.
© Define session & cookie. Explain the use of session start.
For 4 marks:
* Define session: A session is a way to store information about a user across
multiple pages.
* Define cookie: A cookie is a small text file that a website stores on the
user’s computer.
* Explain session_start: session_start() begins a new session or resumes an
existing one, and it must be called before any output.
(d) Explain delete operation of PHP on table data.
For 4 marks:
The DELETE operation removes rows from a table in a database. In PHP, this
is done using the SQL DELETE statement.
<?php
// Example (simplified):
$query = “DELETE FROM users WHERE id = 1”;
// …code to connect to database and execute the query…
?>
It involves constructing a DELETE query and executing it using PHP’s
database functions.
4. Attempt any THREE (4 marks each):
(a) Write PHP script to sort any five numbers using array function.
For 4 marks, provide the code:
<?php
$numbers = array(5, 2, 8, 1, 9);
Sort($numbers); // Sorts the array in ascending order
Foreach ($numbers as $n) {
Echo $n . “ “;
?>
Sort() function is used to sort arrays.
(b) Write PHP program for cloning of an object.
For 4 marks, provide the code:
<?php
Class MyClass {
Public $prop;
Function __construct($val) { $this->prop = $val; }
$obj1 = new MyClass(“Hello”);
$obj2 = clone $obj1; // Creates a copy of $obj1
$obj2->prop = “World”;
Echo $obj1->prop; // Outputs Hello
?>
The clone keyword is used to create a copy of an object.
© Create a customer form like customer name, address, mobile no, date of
birth using different form of input elements & display user inserted values in
new PHP form.
For 4 marks, provide the HTML form (or a simplified version) and the PHP to
display the data:
<form action=”display.php” method=”post”>
Name: <input type=”text” name=”name”><br>
<input type=”submit”>
</form>
// display.php
<?php
Echo “Name: “ . $_POST[‘name’];
?>
This shows a basic form and how to access the submitted data.
(d) Inserting and retrieving the query result operations.
For 4 marks:
* Inserting: Adding new data to a database table using the SQL INSERT
statement.
* Retrieving: Fetching data from a database table using the SQL SELECT
statement.
These operations involve constructing SQL queries in PHP and using
database functions to execute them.
€ How do you validate user inputs in PHP?
For 4 marks, mention key validation techniques:
* Checking for required fields.
* Validating data types (e.g., is it an integer?).
* Sanitizing input to prevent security risks (e.g., using htmlspecialchars()).
Validation ensures data quality and security.
5. Attempt any TWO (6 marks each):
(a) Explain different loops in PHP with example.
For 6 marks, explain three loops with examples:
For loop: Executes a block of code a specified number of times.
<?php
For ($i = 0; $i < 3; $i++) {
Echo $i;
} // Outputs 012
?>
While loop: Executes a block as long as a condition is true.
<?php
$i = 0;
While ($i < 3) {
Echo $i;
$i++;
} // Outputs 012
?>
Foreach loop: Iterates over arrays.
<?php
$arr = array(“a”, “b”, “c”);
Foreach ($arr as $x) {
Echo $x;
} // Outputs abc
?>
Explain the basic syntax and purpose of each loop.
(b) How do you connect MySQL database with PHP.
For 6 marks, outline the connection process:
* Establish a connection: Use mysqli_connect() or PDO to connect to the
MySQL server, providing hostname, username, password, and database
name.
* Check the connection: Verify if the connection was successful.
* Execute a query: Use mysqli_query() or PDO’s query() or
prepare()/execute() to send SQL queries to the database.
* Process the result: If it’s a SELECT query, fetch the results using functions
like mysqli_fetch_assoc() or PDO’s fetch() methods.
* Close the connection: Use mysqli_close() or set the PDO object to null to
close the connection (though PHP usually closes it automatically at the end
of the script).
Provide a simplified code example using mysqli_connect():
<?php
$conn = mysqli_connect(“host”, “user”, “pass”, “db”);
If (!$conn) {
Die(“Connection failed: “ . mysqli_connect_error());
$result = mysqli_query($conn, “SELECT * FROM table”);
//…process result…
Mysqli_close($conn);
?>
© Create a class as “Percentage” with two properties length & width.
Calculate the area of the rectangle for two objects.
For 6 marks, provide the class definition and object creation:
<?php
Class Rectangle { // Corrected class name
Public $length;
Public $width;
Function __construct($l, $w) {
$this->length = $l;
$this->width = $w;
Function area() {
Return $this->length * $this->width;
$rect1 = new Rectangle(10, 5);
$rect2 = new Rectangle(20, 3);
Echo “Area 1: “ . $rect1->area() . “<br>”; // Outputs 50
Echo “Area 2: “ . $rect2->area(); // Outputs 60
?>
Define the class, its properties, a constructor, an area calculation method,
and demonstrate creating and using two objects.
6. Attempt any TWO (6 marks each):
(a) Write a PHP program to demonstrate the use of cookies.
For 6 marks, provide a complete example:
<?php
$cookie_name = “user”;
$cookie_value = “JohnDoe”;
Setcookie($cookie_name, $cookie_value, time() + (86400 * 30), “/”); // Set
cookie for 30 days
If(isset($_COOKIE[$cookie_name])) {
Echo “Cookie ‘” . $cookie_name . “’ is set!<br>”;
Echo “Value: “ . $_COOKIE[$cookie_name];
} else {
Echo “Cookie ‘” . $cookie_name . “’ is not set!”;
?>
Explain setting the cookie with setcookie() (name, value, expiration), and
then retrieving it with $_COOKIE.
(b) Explain any four string functions in PHP with example.
For 6 marks, explain four string functions with examples:
Strlen(): Returns the length of a string.
<?php echo strlen(“Hello”); // Outputs 5 ?>
Strpos(): Finds the position of the first occurrence of a substring.
<?php echo strpos(“abc”, “b”); // Outputs 1 ?>
Str_replace(): Replaces occurrences of a substring with another string.
<?php echo str_replace(“a”, “b”, “abc”); // Outputs bbc ?>
Strtolower(): Converts a string to lowercase.
<?php echo strtolower(“ABC”); // Outputs abc ?>
Explain the function’s purpose and show its usage.
© (i) What is inheritance? (ii) Write update operation on table data.
For 6 marks:
* (i) What is inheritance? Inheritance is an OOP concept where a class
(child/subclass) derives properties and methods from another class
(parent/superclass), promoting code reuse and hierarchy.
* (ii) Write update operation on table data.
* The SQL UPDATE statement modifies existing data in a table.
* Example:
UPDATE employees
SET salary = 50000
WHERE employee_id = 100;
This query updates the salary of the employee with ID 100.