PHP Assignment Solutions
Q1: Differentiate the Following
A. GET and POST Method:
● GET: Used to retrieve data from a server. Data is appended to the URL as query parameters.
● POST: Used to send data to a server. Data is encoded in the HTTP request body.
B. fgetc() and fgets():
● fgetc(): Reads a single character from a file.
● fgets(): Reads a line from a file.
C. implode() and explode():
● implode(): Joins elements of an array into a string.
● explode(): Splits a string into an array of elements.
D. include() and require():
● include(): Includes a PHP file, generating a warning if it fails.
● require(): Includes a PHP file, generating a fatal error if it fails.
E. include() and include_once():
● include(): Includes a file without checking if it has already been included.
● include_once(): Includes a file only if it has not already been included.
F. fgets() and fgetss():
● fgets(): Reads a line from a file.
● fgetss(): Reads a line from a file, stripping HTML tags.
G. dir() and opendir():
● dir(): Creates a directory object.
● opendir(): Opens a directory for reading.
H. overloading and overriding:
● Overloading: Creating multiple methods with the same name but different parameters.
● Overriding: Defining a method in a child class that has the same name and signature as a
method in the parent class.
Q2: Define the Following Terms
A. Class: A blueprint for creating objects.
B. Object: An instance of a class.
C. Data member: A variable declared within a class.
D. Member function: A function declared within a class.
E. Encapsulation: Bundling data and methods within a class.
F. Data hiding: Making data members private within a class.
G. Constructor: A method that is automatically called when an object is created.
H. Destructor: A method that is automatically called when an object is destroyed.
I. Inheritance: The ability of a class to inherit properties and methods from another class.
J. Code reusability: The ability to reuse code in different parts of a program.
K. Polymorphism: The ability of objects of different types to be treated as if they were of the
same type.
Q3: Write a PHP Script for the Following Tasks
A. Whether the given number is palindrome or not:
PHP
function isPalindrome($num) {
$reversed = strrev($num);
return $num == $reversed;
}
B. Whether the given number is Armstrong or not:
PHP
function isArmstrong($num) {
$digits = strlen($num);
$sum = 0;
$temp = $num;
while ($temp != 0) {
$digit = $temp % 10;
$sum += pow($digit, $digits);
$temp = (int)($temp / 10);
}
return $sum == $num;
}
C. Whether the given number is prime or not:
PHP
function isPrime($num) {
if ($num <= 1) {
return false;
}
for ($i = 2; $i * $i <= $num; $i++) {
if ($num % $i == 0) {
return false;
}
}
return true;
}
D. To find out the prime factor of a given number:
PHP
function primeFactors($num) {
$factors = [];
while ($num % 2 == 0) {
$factors[] = 2;
$num /= 2;
}
for ($i = 3; $i * $i <= $num; $i += 2) {
while ($num % $i == 0) {
$factors[] = $i;
$num /= $i;
}
}
if ($num > 2) {
$factors[] = $num;
}
return $factors;
}
E. Recursive traversal for a directory:
PHP
function traverseDirectory($directory) {
$files = scandir($directory);
foreach ($files as $file) {
if ($file != '.' && $file != '..') {
$path = $directory . '/' . $file;
if (is_dir($path)) {
traverseDirectory($path);
} else {
echo "File: $path\n";
}
}
}
}
SQL Command Syntax
A. Create Database
SQL
CREATE DATABASE database_name;
Replace database_name with the desired name for your database.
B. Create Table
SQL
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
...
);
Replace table_name with the desired name for your table, and column1, column2, etc. with the
column names and their corresponding data types (e.g., INT, VARCHAR, DATE).
C. Insert Row
SQL
INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...);
Replace table_name with the name of the table you want to insert data into, and column1,
column2, etc. with the column names. Replace value1, value2, etc. with the actual values you
want to insert.
D. Select Command Using WHERE Clause
SQL
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Replace column1, column2, etc. with the columns you want to select, table_name with the name
of the table, and condition with the condition you want to apply. For example:
SQL
SELECT * FROM customers WHERE city = 'New York';
E. Update
SQL
UPDATE table_name
SET column1 = value1, column2 = value2, ...
WHERE condition;
Replace table_name with the name of the table you want to update, column1, column2, etc. with
the columns you want to modify, and value1, value2, etc. with the new values.
F. Delete
SQL
DELETE FROM table_name
WHERE condition;
Replace table_name with the name of the table you want to delete rows from, and condition with
the condition you want to apply.
G. Search According to Different Criteria
You can use various conditions in the WHERE clause to search for data according to different
criteria. Here are some examples:
● Equality: column_name = value
● Inequality: column_name != value
● Greater than: column_name > value
● Less than: column_name < value
● Greater than or equal to: column_name >= value
● Less than or equal to: column_name <= value
● Between: column_name BETWEEN value1 AND value2
● In: column_name IN (value1, value2, ...)
● Like: column_name LIKE pattern (e.g., LIKE '%John%' to find rows where the column value
contains "John")
● AND: Combine multiple conditions using AND.
● OR: Combine multiple conditions using OR.