PHP Script for Addition of Two 2x2 Matrices with Output (Easy Example)

php script for addition of two 2x2 matrices using two dimensional arrays with output example

This is one of the most commonly asked PHP practical questions in exams. In this tutorial, you will learn how to write a PHP script for addition of two 2x2 matrices using two dimensional arrays.

We will also understand associative arrays and how to use the foreach loop in PHP with a simple example.


Quick Answer: PHP Code for Matrix Addition

Here is a simple and direct PHP program:

<?php
$a = [[1,2],[4,5]];
$b = [[1,2],[4,5]];

for($i=0;$i<2;$i++){
  for($j=0;$j<2;$j++){
    $result[$i][$j] = $a[$i][$j] + $b[$i][$j];
  }
}

print_r($result);
?>

What is Matrix Addition in PHP?

Matrix addition in PHP means adding corresponding elements of two matrices using two dimensional arrays. Both matrices must have the same number of rows and columns.

Example:

[1 2] + [1 2] = [2 4]
[4 5] [4 5] [8 10]


Steps to Add Two 2x2 Matrices in PHP

  1. Create two 2D arrays for the matrices.
  2. Use nested for loops to access rows and columns.
  3. Add corresponding elements.
  4. Store results in a new matrix.
  5. Display the result.

Detailed PHP Program (With Explanation)

<?php 
$a1 = Array(
    Array(1,2),
    Array(4,5)
);

$a2 = Array(
    Array(1,2),
    Array(4,5)
);

$result = array();

for($i=0; $i<=1; $i++) {
    for($j=0; $j<=1; $j++) {
        $result[$i][$j] = $a1[$i][$j] + $a2[$i][$j];
    }
}

echo "Result Matrix:<br/>";
print_r($result);
?>

Output

php matrix addition output 2x2 example

Explanation of the Code

  • Two matrices are stored using 2D arrays.
  • Nested loops are used to access each element.
  • Each element is added and stored in a result array.
  • The final matrix is printed using print_r().

PHP Associative Array with Foreach Loop

Now let’s understand associative arrays in PHP. These arrays use key-value pairs instead of numeric indexes.

<?php
$info = array(
    "Department"=>"Computer",
    "Semester"=>"5th",
    "Enrollment No."=>"54"
);

foreach($info as $key => $value) {
    echo "Key=" . $key . ", Value=" . $value;
    echo "<br>";
}
?>

Output

php associative array foreach loop output

Real-World Use Cases

  • Data processing and calculations
  • Handling structured datasets
  • Looping through database records

Common Mistakes

  • Using matrices of different sizes
  • Incorrect loop conditions
  • Mixing associative and indexed arrays

Best Practices

  • Always check matrix dimensions before addition
  • Use proper indentation for readability
  • Keep variable names meaningful

FAQ

Can we add matrices of different sizes in PHP?

No, both matrices must have the same dimensions.

Which loop is used for matrix operations?

Nested for loops are commonly used.

What is a 2D array in PHP?

A 2D array is an array of arrays used to represent matrix structure.


Conclusion

This practical helps you understand matrix operations and associative arrays in PHP. These concepts are important for exams and real-world development.

If this helped you, bookmark this page and explore more PHP programs to strengthen your skills.


🔗 Related Articles

Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment