1.
Write a php program which adds up 2 columns and rows of given table
<?php
// Sample table data
$table = array(
array(10, 20, 30),
array(40, 50, 60),
array(70, 80, 90)
);
// Initialize the row totals and column totals
$row_totals = array();
$column_totals = array();
// Loop through each row
foreach ($table as $row) {
$row_total = array_sum($row);
$row_totals[] = $row_total;
foreach ($row as $key => $value) {
if (!isset($column_totals[$key])) {
$column_totals[$key] = 0;
}
$column_totals[$key] += $value;
}
}
// Display the results
echo "<table border='1'>";
echo "<tr><th>Row</th><th>Col 1</th><th>Col 2</th><th>Col 3</th><th>Row
Total</th></tr>";
foreach ($table as $key => $row) {
echo "<tr>";
echo "<td>" . ($key + 1) . "</td>";
foreach ($row as $value) {
echo "<td>" . $value . "</td>";
}
echo "<td>" . $row_totals[$key] . "</td>";
echo "</tr>";
}
echo "<tr><th>Col Totals</th><td>" . $column_totals[0] . "</td><td>" .
$column_totals[1] . "</td><td>" . $column_totals[2] . "</td><td>" .
array_sum($row_totals) . "</td></tr>";
echo "</table>";
?>
2. Write a PHP program to compute the sum of first n given prime numbers
<?php
function isPrime($num) {
if ($num <= 1) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}
function sumOfFirstNPrimes($n) {
$sum = 0;
$count = 0;
$num = 2;
while ($count < $n) {
if (isPrime($num)) {
$sum += $num;
$count++;
}
$num++;
}
return $sum;
}
$n = 10; // compute the sum of the first 10 prime numbers
echo "The sum of the first $n prime numbers is: ". sumOfFirstNPrimes($n);
?>
3. Write a PHP program to find valid an email address
<?php
function validateEmail($email) {
$pattern = '/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/';
if (preg_match($pattern, $email)) {
return true;
} else {
return false;
}
}
if (validateEmail($email)) {
echo "The email address '$email' is valid.";
} else {
echo "The email address '$email' is not valid.";
}
?>
4. Write a PHP program to convert a number written in words to digit.
<?php
function wordToDigit($word) {
$ones = array(
'zero' => 0,
'one' => 1,
'two' => 2,
'three' => 3,
'four' => 4,
'five' => 5,
'six' => 6,
'seven' => 7,
'eight' => 8,
'nine' => 9
);
$teens = array(
'ten' => 10,
'eleven' => 11,
'twelve' => 12,
'thirteen' => 13,
'fourteen' => 14,
'fifteen' => 15,
'sixteen' => 16,
'seventeen' => 17,
'eighteen' => 18,
'nineteen' => 19
);
$tens = array(
'twenty' => 20,
'thirty' => 30,
'forty' => 40,
'fifty' => 50,
'sixty' => 60,
'seventy' => 70,
'eighty' => 80,
'ninety' => 90
);
$hundreds = array(
'hundred' => 100
);
$thousands = array(
'thousand' => 1000,
'million' => 1000000,
'billion' => 1000000000
);
$word = strtolower($word);
$words = explode(' ', $word);
$number = 0;
foreach ($words as $w) {
if (array_key_exists($w, $ones)) {
$number += $ones[$w];
} elseif (array_key_exists($w, $teens)) {
$number += $teens[$w];
} elseif (array_key_exists($w, $tens)) {
$number += $tens[$w];
} elseif (array_key_exists($w, $hundreds)) {
$number *= $hundreds[$w];
} elseif (array_key_exists($w, $thousands)) {
$number *= $thousands[$w];
}
}
return $number;
}
// Test the function
echo wordToDigit("one hundred twenty three"); // Output: 123
echo wordToDigit("five thousand six hundred seventy eight"); // Output: 5678
echo wordToDigit("ninety nine million two hundred fifty six thousand seven hundred
eighty nine"); // Output: 99256789
?>
5. Write a PHP script to delay the program execution for the given number of seconds.
<?php
function delay($seconds) {
sleep($seconds);
}
// Example usage:
echo "Starting program...\n";
delay(5); // delay for 5 seconds
echo "Program resumed.\n";
?>
6. Write a PHP script, which changes the colour of the first character of a word
<?php
function colorFirstChar($word, $color) {
$firstChar = substr($word, 0, 1);
$restOfWord = substr($word, 1);
return "<span style='color: $color'>$firstChar</span>$restOfWord";
}
// Test the function
$word = "Hello World";
$color = "red";
echo colorFirstChar($word, $color);
?>
7. Write a PHP program to find multiplication table of a number.
<?php
function multiplicationTable($number, $limit = 10) {
echo "Multiplication table of $number:<br>";
for ($i = 1; $i <= $limit; $i++) {
$result = $number * $i;
echo "$number x $i = $result<br>";
}
}
// Test the function
$multiplicationTable(5);
?>
8. Write a PHP program to calculate Factorial of a number.
<?php
function factorial($n) {
if ($n == 0) {
return 1;
} else {
return $n * factorial($n - 1);
}
}
// Test the function
$num = 5;
$result = factorial($num);
echo "The factorial of $num is $result";
?>
9. Write a PHP code to create a student mark sheet table. Insert, delete and modify records.
<?php
// Configuration
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'student_marksheet';
// Create connection
$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
// Create table
$sql = "CREATE TABLE IF NOT EXISTS student_marksheet (
id INT AUTO_INCREMENT,
name VARCHAR(255),
roll_no INT,
maths INT,
science INT,
english INT,
total INT,
percentage FLOAT,
PRIMARY KEY (id)
)";
if ($conn->query($sql) === TRUE) {
echo "Table created successfully";
} else {
echo "Error creating table: " . $conn->error;
}
// Insert record
if (isset($_POST['insert'])) {
$name = $_POST['name'];
$roll_no = $_POST['roll_no'];
$maths = $_POST['maths'];
$science = $_POST['science'];
$english = $_POST['english'];
$total = $maths + $science + $english;
$percentage = ($total / 300) * 100;
$sql = "INSERT INTO student_marksheet (name, roll_no, maths, science, english,
total, percentage) VALUES ('$name', '$roll_no', '$maths', '$science', '$english',
'$total', '$percentage')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
// Delete record
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$sql = "DELETE FROM student_marksheet WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
echo "Record deleted successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
// Modify record
if (isset($_POST['modify'])) {
$id = $_POST['id'];
$name = $_POST['name'];
$roll_no = $_POST['roll_no'];
$maths = $_POST['maths'];
$science = $_POST['science'];
$english = $_POST['english'];
$total = $maths + $science + $english;
$percentage = ($total / 300) * 100;
$sql = "UPDATE student_marksheet SET name='$name', roll_no='$roll_no',
maths='$maths', science='$science', english='$english', total='$total',
percentage='$percentage' WHERE id='$id'";
if ($conn->query($sql) === TRUE) {
echo "Record modified successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
// Display records
$sql = "SELECT * FROM student_marksheet";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>Roll
No</th><th>Maths</th><th>Science</th><th>English</th><th>Total</th><th>Percentage</
th><th>Action</th></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr>";
echo "<td>" . $row["id"]. "</td>";
echo "<td>" . $row["name"]. "</td>";
echo "<td>" . $row["roll_no"]. "</td>";
echo "<td>" . $row["maths"]. "</td>";
echo "<td>" . $row["science"]. "</td>";
echo "<td>" . $row["english"]. "</td>";
echo "<td>" . $row["total"]. "</td>";
echo "<td>" . $row["percentage"]. "</td>";
echo "<td><form action='' method='post'><input type='hidden' name='id'
value='" . $row["id"]. "'><input type='submit' name='delete' value='Delete'></form>
<form action='' method='post'><input type='hidden' name='id' value='" . $row["id"].
"'><input type='submit' name='modify' value='Modify'></form></td>";
echo "</tr>";
echo "</table>";
} else {
echo "No records found";
$conn->close();
?>
<!-- HTML Form -->
<form action="" method="post">
10. From a XML document (email.xml), write a program to retrieve and print all the e-mail addresses
from the document using XML
<?php
// Load the XML file
$xml = simplexml_load_file('email.xml');
// Check if the XML file is loaded successfully
if (!$xml) {
echo "Failed to load XML file";
exit;
// Retrieve all email addresses
$emails = $xml->xpath('//email');
// Print all email addresses
echo "Email Addresses:<br>";
foreach ($emails as $email) {
echo $email. "<br>";
?>
11. From a XML document (tree.xml), suggest three different ways to retrieve the text value'John' using
the DOM:
12. Write a program that connects to a MySQL database and retrieves the contents of any one of its
tables as an XML file. Use the DOM.