1

I'm trying to implement a function for multiplying two numeric multidimensional arrays, I performed three cycles to iterate over the rows and columns of corresponding. However something is failing there.

The PHP is just not generating any web page, whenever I comment the function code and the function calling it fails...

function multiplicaMatriz($matrix1, $matrix2){

    $ab = 0;

    echo "<table border="1">";
        for($i = 0; $i < 3; $i++) {
            echo "<tr>";
            for($j = 0; $j < 3; $j++) {
                $ab = 0;
                for($k = 0; $k < 3; $k++) {
                    $ab += (($matrix1[$i][$k])*($matrix2[$k][$j]));
                }
                echo "<td>".$ab."</td>";
            }
            echo "</tr>";
        }
    echo "</table>";
}

Rest of my code is here:

<html>
    <head>
        <title>
        Novatos del PHP
        </title>
    </head>

    <body> <center>



    <?php


        $m1 = array (   array (rand(0,100), rand(0,100), rand(0,100)), 
                        array (rand(0,100), rand(0,100), rand(0,100)), 
                        array (rand(0,100), rand(0,100), rand(0,100)), 
                        //array (rand(0,100), rand(0,100), rand(0,100))
                    );


        $m2 = array (   array (rand(0,100), rand(0,100), rand(0,100)), 
                        array (rand(0,100), rand(0,100), rand(0,100)), 
                        array (rand(0,100), rand(0,100), rand(0,100)), 
                        //array (rand(0,100), rand(0,100), rand(0,100))
                    );

        function imprimeMatriz($matrix){
            echo "<table width=\"200\" border=\"1\">";

            foreach($matrix as $row =>$rValue){
                echo "<tr>";
                foreach($rValue as $col =>$cValue){
                    echo "<td>".$cValue."</td>";
                }
                echo "</tr>";
            }
            echo "</table>";
        }

        function sumaMatriz($matrix1, $matrix2){
                echo "<table width=\"200\" border=\"1\">";

                foreach($matrix1 as $row1 =>$rValue1){
                        echo "<tr>";
                        foreach($rValue1 as $col1 =>$cValue1)
                                echo "<td>".($cValue1+$matrix2[$row1][$col1])."</td>";
                        echo "</tr>";
                }
                echo "</table>";
        }

        function multiplicaMatriz($matrix1, $matrix2){

            $ab = 0;

            echo "<table border="1">";
                for($i = 0; $i < 3; $i++) {
                    echo "<tr>";
                    for($j = 0; $j < 3; $j++) {
                        $ab = 0;
                        for($k = 0; $k < 3; $k++) {
                            $ab += (($matrix1[$i][$k])*($matrix2[$k][$j]));
                        }
                        echo "<td>".$ab."</td>";
                    }
                    echo "</tr>";
                }
            echo "</table>";
        }       


        print "<h2>Matriz 1</h2>";
        imprimeMatriz($m1);

        print "<br>";

        print "<h2>Matriz 2</h2>";
        imprimeMatriz($m2);

        print "<br>";

        print "<h2>Suma de matrices</h2>";
        sumaMatriz($m1, $m2);

        print "<br>";

        print "<h2>Suma de matrices</h2>";
        multiplicaMatriz($m1, $m2);

    ?>


     </center> </body>

</html>
7
  • Where's the rest of your code? The lack of a displayed web page is probably due to something elsewhere in the code. Commented Aug 22, 2013 at 18:38
  • I just posted it, @StephenTG Commented Aug 22, 2013 at 18:42
  • 1
    So if you cut out all the php, the page displays fine? (albeit with just a title). I just want to make sure it's somehow an issue with the function Commented Aug 22, 2013 at 18:45
  • Yes, it's a function issue. Commented Aug 22, 2013 at 18:47
  • 1
    Answers are given, all works fine:) You just need to switch your display_errors ON, it'll show you this simple mistakes in code. Commented Aug 22, 2013 at 18:55

2 Answers 2

2

You forgot to escape the " on line 57. You probably want to turn on some error reporting so that you get some idea about what is causing the failure :)

error_reporting(E_ERROR | E_WARNING | E_PARSE);

To fix the code, change:

echo "<table border="1">";

to:

echo "<table border=\"1\">";
Sign up to request clarification or add additional context in comments.

1 Comment

If you turn on error reporting (adding that line at the top of your script), then you will see the error on the page. Alternatively, if you can't get it to work, you could drop it all into a try...catch block and print out the errors your self. php error handling docs
2

Diego, of it's your real code, byte-to-byte, you just need to copy it to some PHP IDE, and you'll see what is the problem.

function multiplicaMatriz($matrix1, $matrix2){
    $ab = 0;
    echo "<table border="1">"; // <-- Look here, you messed up your quotes

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.