PHP Lab Program - II BCOM CA (2)

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

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;
}
}

$email = 'example@example.com'; // test email address


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.
10. From a XML document (email.xml), write a program to retrieve and print all the e-mail
addresses from the document using XML
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.

You might also like