0

I'm new to php so I was trying to code a program which adds two 3x3 matrices. I'm getting undefined symbol error multiple times in php code.
Is there any different way to define multidimensional arrays in php.`
There are already many programs on how resolve this problem for one dimensional array. They are hard coding default values. Giving default values to each element in array will be time consuming.
Here is my code:

    <html>
    <head></head>
    <body>
        <form action="matAdd.php" method="post">
            Enter values in first matrix:
            <table cellpadding=10 cellspacing=10>
            <tr>
            <td>
            <table>
                <tr><td colspan="3">Matrix 1</td>
                </tr>
                <tr>
                    <td><input type="number" name="m00" size="3" required/></td>
                    <td><input type="number" name="m01" size="3" required/></td>
                    <td><input type="number" name="m02" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="m10" size="3" required/></td>
                    <td><input type="number" name="m11" size="3" required/></td>
                    <td><input type="number" name="m12" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="m20" size="3" required/></td>
                    <td><input type="number" name="m21" size="3" required/></td>
                    <td><input type="number" name="m22" size="3" required/></td>
                </tr>
            </table>
            </td>
            <td>
            <table>
                <tr>
                    <td colspan="3">Matrix 2</td>
                </tr>
                <tr>
                    <td><input type="number" name="n00" size="3" required/></td>
                    <td><input type="number" name="n01" size="3" required/></td>
                    <td><input type="number" name="n02" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="n10" size="3" required/></td>
                    <td><input type="number" name="n11" size="3" required/></td>
                    <td><input type="number" name="n12" size="3" required/></td>
                </tr>
                <tr>
                    <td><input type="number" name="n20" size="3" required/></td>
                    <td><input type="number" name="n21" size="3" required/></td>
                    <td><input type="number" name="n22" size="3" required/></td>
                </tr>
            </table>
            </td>
            </tr>
            </table>
            <center><input type="submit" value="Add"/></center>
        </form>  
    </body>
</html>
    <?php 
            $m = array(array());
            $n =array(array());
            $sum= array(array());
            for($i=0;$i<3;$i++) 
            {
                for($j=0;$j<3;$j++)
                {
                    $m[$i][$j]=$_POST['m'.$i.$j];
                    $n[$i][$j]=$_POST['n'.$i.$j];
                    $sum[$i][$j]=$m[$i][$j]+$n[$i][$j];
                }
            }
            echo 'Addition is :';
            echo '<table border=1 style="border-collapse:collaspe">';
            for($i=0;$i<3;$i++)
            {
                echo '<tr>';
                for($j=0;$j<3;$j++)
                {
                    echo '<td>'.$sum[$i][$j].'</td>';
                }
                echo '</tr>';
            }
            echo '</table>';
    ?>

I'm getting error in that nested for loop where I am taking values from post method into multidimensional array.

  • 1
    Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Jonnix Oct 30 '18 at 10:20
  • What is the expected input and expected output? – Jaquarh Oct 30 '18 at 10:20
  • Instead of `$m = array(array())`, you really want something like `$m = array(array(), array(), array())`. – Chris Lear Oct 30 '18 at 10:23
  • 1
    Hmm... i think `
    ` is supposed to be `
    ` at least
    – Eugene Anisiutkin Oct 30 '18 at 10:26
  • @EugeneAnisiutkin Yeah it is there i forgot to write that but error is still there. – Pritam Parab Oct 30 '18 at 10:29
  • @ChrisLear What's the difference? – Pritam Parab Oct 30 '18 at 10:30
  • @PritamParab your code initialises an array with just one index. So if you were to try to access `$m[0]`, that would work, but if you try to access `$m[1]`, you would find that it's not set, hence trying to do something like `$m[1][2] = 'x'` would fail. My code initialises `$m` with three indices, so `$m[0]`, `$m[1]` and `$m[2]` are all set. Which means that if you try to set `$m[1][2]` it will work. Try both, and use `var_dump` to see the difference yourself. – Chris Lear Oct 30 '18 at 11:29

3 Answers3

0

just try to change following

1. change your <form> with

<form action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?> method="post">

2 . add name attribute to your submit button

<input type="submit" name ="submit" value="Add"/>

3. put your php code inside

if(isset($_POST['submit'])){
    $m = array(array());
    $n =array(array());
    //your code....
}

hope this solve your issue

Chirag
  • 363
  • 2
  • 12
  • Just putting code inside `if(isset($_POST['submit'])) {//code here}` works. The main problem is there). Well right now with the changed user code it is there =) – Eugene Anisiutkin Oct 30 '18 at 10:40
  • have you add name attribute to your submit button? – Chirag Oct 30 '18 at 10:45
  • when you click your add button your matrix is send via post.but when u run it first time post matrix is not set means(empty) that why in loop it produce error undefined. – Chirag Oct 30 '18 at 10:49
0

A bit of explanation. Your originall problem was caused mainly by $_POST['m'.$i.$j]. At the time of the first execution of the php script they were not defined, because no data from form was sent yet, and the named values simply were not yet added to $POST variable.

The check if(isset($_POST['submit'])) { basically checks if any forms with data wre submitted and executes the code inside the {...} only if there were.

So if you make your code look like

if(isset($_POST['submit'])){
//Your original php code goes here
}

it will work, because your code will only execute when a form was submitted.

It's not a good built explanation, but it's the best I can do

0

I'm getting undefined index error for multidimensional array in php description : Your code is right. Just initialize array as foolows. If again error occour then check your inform form

<?php 
      $m = array();
      $n =array();

?>