Although the order of matrices should be fine, the following code throws back the exception. It might be a tiny thing I'm not being able to notice, but can't figure it out.
<?php $mat1 = array(5,1); $mat2 = array(1,5); function matrixmult($m1,$m2){ $r=count($m1); $c=count($m2[0]); $p=count($m2); if(count($m1[0])!=$p){throw new Exception('Incompatible matrixes');} $m3=array(); for ($i=0;$i< $r;$i++){ for($j=0;$j<$c;$j++){ $m3[$i][$j]=0; for($k=0;$k<$p;$k++){ $m3[$i][$j]+=$m1[$i][$k]*$m2[$k][$j]; } } } } return($m3); } matrixmult($mat1,$mat2); ?>
$mat2 = array(array(1),array(5));array( )around them. He's essentially defining vectors here, those aren't two-dimensional arrays.