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>
4

2 回答 2

2

您忘记转义第"57 行。您可能想要打开一些错误报告,以便了解导致失败的原因:)

error_reporting(E_ERROR | E_WARNING | E_PARSE);

要修复代码,请更改:

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

至:

echo "<table border=\"1\">";
于 2013-08-22T18:52:22.790 回答
2

Diego,这是您的真实代码,逐字节地,您只需将其复制到某个 PHP IDE 中,您就会发现问题所在。

function multiplicaMatriz($matrix1, $matrix2){
    $ab = 0;
    echo "<table border="1">"; // <-- Look here, you messed up your quotes
于 2013-08-22T18:53:02.910 回答