1.
ADD UP COLUMNS AND ROWS OF A GIVEN TABLE
AIM:
To write a PHP program to add up the columns and rows of a given table
ALGORITHM:
Step 1: Start the program.
Step 2: Read number of rows and columns using $row = (int)trim(fgets(STDIN)); $col =
(int)trim(fgets(STDIN));
Step 3: Read matrix elements using a loop and store in array $a.
Step 4: Display the matrix using implode(' ', $a[$i]).
Step 5: For each column, calculate sum using nested loop:
$sum += $a[$i][$j]; and display it.
Step 6: For each row, calculate sum using array_sum($a[$i]) and display it.
Step 7: End the program.
PROGRAM:
<?php
echo "PHP program to find the sum of each column and row of a table\n";
echo "Enter the number of rows: ";
$row = (int)trim(fgets(STDIN));
echo "Enter the number of columns: ";
$col = (int)trim(fgets(STDIN));
echo "Enter {$row}x{$col} elements of matrix a\n";
for ($i = 0; $i < $row; $i++)
$a[$i] = array_map('intval', explode(' ', trim(fgets(STDIN))));
echo "\nThe given matrix:\n";
for ($i = 0; $i < $row; $i++)
echo implode(' ', $a[$i]) . "\n";
echo "\nSum of each column\n";
for ($j = 0; $j < $col; $j++) {
$sum = 0;
for ($i = 0; $i < $row; $i++) $sum += $a[$i][$j];
echo "Sum of " . ($j + 1) . " Column = $sum\n";
}
echo "\nSum of each row\n";
for ($i = 0; $i < $row; $i++)
echo "Sum of " . ($i + 1) . " Row = " . array_sum($a[$i]) . "\n";
?>
OUTPUT:
PHP program to find the sum of each column and row of a table
Enter the number of rows: 2
Enter the number of columns: 2
Enter 2x2 elements of matrix a
1 2
3 4
The given matrix:
1 2
3 4
Sum of each column
Sum of 1 Column = 4
Sum of 2 Column = 6
Sum of each row
Sum of 1 Row = 3
Sum of 2 Row = 7
RESULT:
Thus, the PHP program that adds up columns and rows of given table has been
executed successfully
2. SUM OF FIRST N PRIME NUMBERS
AIM:
To write a PHP program to compute the sum of first n given prime numbers.
ALGORITHM:
Step 1: Start the program.
Step 2: Display a message to enter numbers separated by commas.
echo "Enter a list of numbers, separated by commas: ";
Step 3: Read the input and convert it into an array.
$numArr = explode(',', trim(fgets(STDIN)));
Step 4: Initialize an empty array to store prime numbers:
$primes = [];
Step 5: For each number in the array, convert it to absolute and trim spaces using $n =
abs(trim($n));, skip if less than 2, check if it's prime using a loop from 2 to sqrt($n), and
if prime, add it to $primes[].
Step 6: Display the list of prime numbers.
implode(' ', $primes)
Step 7: Calculate and display the sum of prime numbers.
array_sum($primes)
Step 8: End the program.
PROGRAM:
<?php
echo "PHP program to find the sum of prime numbers\n\n";
echo "Enter a list of numbers, separated by commas: ";
$numArr = explode(',', trim(fgets(STDIN)));
$primes = [];
foreach ($numArr as $n) {
$n = abs(trim($n));
if ($n < 2) continue;
$isPrime = true;
for ($i = 2; $i <= sqrt($n); $i++)
if ($n % $i == 0) { $isPrime = false; break; }
if ($isPrime) $primes[] = $n;
}
echo "\nThe prime numbers are: " . implode(' ', $primes);
echo "\n\nThe sum of prime numbers = " . array_sum($primes) . "\n";
?>
OUTPUT:
PHP program to find the sum of prime numbers
Enter a list of numbers, separated by commas: 1,2,3,4,5,6,7,8
The prime numbers are: 2 3 5 7
The sum of prime numbers = 17
RESULT:
Thus, the PHP program to find the sum of prime numbers in an array has been
executed successfully
3. VALIDATE AN EMAIL ADDRESS
AIM:
To write a PHP program to validate the given email address.
ALGORITHM:
Step 1: Start the program.
Step 2: Display a message to enter an email address.
echo "Enter the email address: ";
Step 3: Read the email input and trim spaces using
$email = trim(fgets(STDIN));
Step 4: Check if the email is valid using filter_var($email, FILTER_VALIDATE_EMAIL).
Step 5: If valid, display: "<email> is a valid email address."
Else, display: "<email> is not a valid email address."
Step 6: End the program.
PROGRAM:
<?php
echo "PHP Program to validate the given Email Address\n";
echo "Enter the email address: ";
$email=trim(fgets(STDIN));
if(filter_var($email,FILTER_VALIDATE_EMAIL)){
echo "\n$email is a valid email address.\n";
} else{
echo "\n$email is a not valid email address.\n";
}
?>
OUTPUT:
PHP Program to validate the given Email Address
Enter the email address: jackson@[Link]
jackson@[Link] is a valid email address.
RESULT:
Thus, the email address validation program using PHP has been executed
successfully.
4. NUMBER GIVEN IN WORDS TO DIGIT
AIM:
To write a PHP program to convert a number in words to digit.
ALGORITHM:
Step 1: Start the program.
Step 2: Create a map that links number words (like "one", "two") to digits ('1', '2', etc.).
Step 3: Prompt the user to enter a number in words separated by semicolons (;).
echo "Enter the number in words: ";
Step 4: Read and convert the input to lowercase and trim spaces.
$input = strtolower(trim(fgets(STDIN)));
Step 5: Split the input using explode(';', $input), trim each word, and for each word,
append the corresponding digit from the map to $digits, or ? if not found.
Step 6: Display the final converted number.
echo "\nConverted Number: $digits\n";
Step 7: End the program.
PROGRAM:
<?php
$map = [
'zero' => '0', 'one' => '1', 'two' => '2', 'three' => '3',
'four' => '4', 'five' => '5', 'six' => '6', 'seven' => '7',
'eight' => '8', 'nine' => '9'
];
echo "Enter the number in words: ";
$input = strtolower(trim(fgets(STDIN)));
$digits = '';
foreach (explode(';', $input) as $word) {
$word = trim($word);
$digits .= $map[$word] ?? '?';
}
echo "\nConverted Number: $digits\n\n";
?>
OUTPUT:
Enter the number in words: eight;one;seven
Converted Number: 817
RESULT:
Thus, the PHP program to convert a number written in words to digit has been
executed successfully.
5. DELAY PROGRAM EXECUTION
AIM:
To write a PHP script to delay the program execution for a given number of
seconds
ALGORITHM:
Step 1: Start the program.
Step 2: Prompt the user to enter the number of seconds to delay.
$n = (int)trim(fgets(STDIN));
Step 3: Display the current time using date('h:i:s').
Step 4: Pause program execution for $n seconds using sleep($n).
Step 5: Display the time after the delay using date('h:i:s').
Step 6: End the program.
PROGRAM:
<?php
echo "Enter number of seconds to delay: ";
$n=(int)trim(fgets(STDIN));
echo "Current time: ".date('h:i:s')."\n";
sleep($n);
echo "Execution time after {$n} seconds delay: ".date('h:i:s');
?>
OUTPUT:
Enter number of seconds to delay: 6
Current time: 12:02:25
Execution time after 6 seconds delay: 12:02:31
RESULT:
Thus, the PHP script to delay the program execution for a given number of
seconds has been executed successfully.
6. CHANGE THE COLOUR OF THE FIRST CHARACTER
AIM:
To write PHP script which changes the color of the first letter of a word
ALGORITHM:
Step 1: Start the program.
Step 2: Initialize the string variable $text with "PHPtutorial".
Step 3: Use preg_replace with regex /(\b[a-z])/i to find the first letter of each word
(case-insensitive).
Step 4: Replace the matched letter with the same letter wrapped inside a <span
style="color:red">...</span> tag to color it red.
Step 5: Display the modified string.
Step 6: End the program.
PROGRAM:
<?php
$text = "PHPtutorial";
$text = preg_replace(
'/(\b[a-z])/i',
'<span style="color:red">\1</span>',
$text
);
echo $text;
?>
OUTPUT:
RESULT:
Thus, the PHP Script to change the color of the first letter of a word has been
executed successfully
7. MULTIPLICATION TABLE
AIM:
To write a PHP program to print the multiplication table for a given number.
ALGORITHM:
Step 1: Start the program.
Step 2: Prompt the user to enter a number.
$num = trim(fgets(STDIN));
Step 3: Display a message indicating the multiplication table of the entered number.
Step 4: Use a loop from 1 to 10:
• For each iteration i, calculate and display $num x $i = ($num * $i).
Step 5: End the program.
PROGRAM:
<?php
echo "Enter a number: ";
$num = trim(fgets(STDIN));
echo "Multiplication table of $num is:\n";
for ($i = 1; $i <= 10; $i++) {
echo "$num x $i = " . ($i * $num) . "\n";
}
?>
OUTPUT:
Enter a number: 7
Multiplication table of 7 is:
7x1=7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70
RESULT:
Thus, the PHP program to print the Multiplication Table has been executed
successfully.
8. FACTORIAL OF A NUMBER
AIM:
To write a PHP program to find the factorial value of a given number.
ALGORITHM:
Step 1: Start the program.
Step 2: Define a function Factorial($number) that initializes $factorial to 1, then
multiplies it by each integer from 1 up to $number, and finally returns the result.
Step 3: Prompt the user to enter a number and read the input.
Step 4: Call the Factorial function with the entered number and store the returned
factorial value.
Step 5: Display the factorial of the number.
Step 6: End the program.
PROGRAM:
<?php
function Factorial($number)
{
$factorial = 1;
for ($i = 1; $i <= $number; $i++) {
$factorial *= $i;
}
return $factorial;
}
echo "Enter a number to find its factorial: ";
$number = trim(fgets(STDIN));
$fact = Factorial($number);
echo "Factorial of $number is: $fact\n";
?>
OUTPUT:
Enter a number to find its factorial: 5
Factorial of 5 is: 120
RESULT:
Thus, the PHP program to calculate the factorial value of a given number has
been executed successfully
9. REVERSE THE FILE CONTENT
AIM:
To write a PHP Script to read a file, reverse its contents, and write the result back
to a new file.
ALGORITHM:
Step 1: Start the program.
Step 2: Read all lines from the file [Link] into an array $FileLines.
Step 3: Reverse the order of lines in the array using array_reverse().
Step 4: Write the reversed lines into a new file called [Link].
Step 5: Display a message confirming that the content has been written in reverse order.
Step 6: End the program.
PROGRAM:
<?php
$FileLines = file('[Link]');
$ReversedLines = array_reverse($FileLines);
file_put_contents('[Link]', $ReversedLines);
echo "The content of [Link] has been written into [Link] in reverse order.\n";
?>
CONTENT IN [Link]
Sujatha
Saraswathi
Alexander
Senthil Sekhar
OUTPUT:
The content of [Link] has been written into [Link] in reverse order.
CONTENT WRITTEN IN [Link]
Senthil Sekhar
Alexander
Saraswathi
Sujatha
RESULT:
Thus, the PHP script to read a file, reverse its contents, and write the result back
to a new file has been executed successfully
10. RENAME ALL THE FILES WITH EXTENSION .TXT TO .XTX
AIM:
To write a PHP script to look through the current directory and rename all the
files with extension .txt to extension .xtx.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the current working directory path using getcwd().
Step 3: Find all .txt files in the current directory using glob().
Step 4: Display a message indicating the start of renaming.
Step 5: Loop through each .txt file found.
Step 6: For each file, create a new filename by replacing the .txt extension with .xtx using
preg_replace().
Step 7: Rename the file to the new name using rename().
Step 8: If renaming is successful, display a success message; otherwise, display a failure
message.
Step 9: End the program.
PROGRAM:
<?php
$dirpath = getcwd();
$files = glob($dirpath . DIRECTORY_SEPARATOR . "*.txt");
echo "Renaming .txt files to .xtx...\n\n";
foreach ($files as $file) {
$newName = preg_replace('/\.txt$/', '.xtx', $file);
if (rename($file, $newName)) {
echo "Renamed: $file => $newName\n";
} else {
echo "Failed to rename: $file\n";
}
}
?>
OUTPUT:
Renaming .txt files to .xtx...
Renamed: C:\xampp\htdocs\php_program\[Link] =>
C:\xampp\htdocs\php_program\[Link]
Renamed: C:\xampp\htdocs\php_program\[Link] =>
C:\xampp\htdocs\php_program\[Link]
RESULT:
Thus, the PHP script to look through the current directory and rename all files
with the extension .txt to .xtx has been executed successfully
11. SORT THE FILE LIST BY LAST MODIFICATION TIME
AIM:
To write a PHP script to read the current directory and return a file list sorted by
last modification time
ALGORITHM:
Step 1: Start the program.
Step 2: Get the current working directory path using getcwd().
Step 3: Retrieve all .txt files in the directory using glob().
Step 4: Sort the files array using usort() based on file modification time in descending
order (latest first).
Step 5: Display a header message for the sorted file list.
Step 6: For each file, get its base name and format its last modification time.
Step 7: Display the file name along with its last modified date and time.
Step 8: End the program.
PROGRAM:
<?php
$dirpath = getcwd();
$files = glob($dirpath . DIRECTORY_SEPARATOR . "*.txt");
usort($files, function($a, $b) {
return filemtime($b) - filemtime($a); // latest first
});
echo "File List Sorted by Last Modification Time:\n\n";
foreach ($files as $file) {
$filename = basename($file);
$modTime = date('F d Y H:i:s', filemtime($file));
echo "$filename - Last Modified On: $modTime\n";
}
?>
OUTPUT:
File List Sorted by Last Modification Time:
[Link] - Last Modified On: July 09 2025 11:42:11
[Link] - Last Modified On: July 09 2025 11:42:04
RESULT:
Thus, the PHP Script to read the current directory and return a file list sorted by
last modification time has been executed successfully.