0% found this document useful (0 votes)
15 views1 page

4 2

The document is a PHP script that defines a 2D array (matrix) with 3 rows and 4 columns, filling it with the product of its indices. It then displays the matrix, calculates, and prints the sum of each row and each column. The script demonstrates basic array manipulation and summation in PHP.

Uploaded by

patelprit2910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views1 page

4 2

The document is a PHP script that defines a 2D array (matrix) with 3 rows and 4 columns, filling it with the product of its indices. It then displays the matrix, calculates, and prints the sum of each row and each column. The script demonstrates basic array manipulation and summation in PHP.

Uploaded by

patelprit2910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

<?

php
// Define a 2D array (matrix)
$rows = 3;
$cols = 4;
$matrix = array();

// Fill the matrix with values (you can also take user input here)
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $cols; $j++) {
$matrix[$i][$j] = $i * $j; // Example: fill with the product of i and j
}
}

// Display the matrix


echo "Matrix:\n";
for ($i = 0; $i < $rows; $i++) {
for ($j = 0; $j < $cols; $j++) {
echo $matrix[$i][$j] . " ";
}
echo "\n";
}

// Calculate the sum of each row


echo "\nSum of each row:\n";
for ($i = 0; $i < $rows; $i++) {
$rowSum = array_sum($matrix[$i]); // Sum the elements of the row
echo "Row " . ($i + 1) . ": " . $rowSum . "\n";
}

// Calculate the sum of each column


echo "\nSum of each column:\n";
for ($j = 0; $j < $cols; $j++) {
$colSum = 0;
for ($i = 0; $i < $rows; $i++) {
$colSum += $matrix[$i][$j]; // Sum the elements of the column
}
echo "Column " . ($j + 1) . ": " . $colSum . "\n";
}
?>

You might also like