doukong9316 2014-12-16 04:22
浏览 180
已采纳

为什么我收到Undefined Variable错误?

I know that it's not necessary to define variables and arrays before using them in PHP. But I'm facing 'Undefined Variable mat1' and 'Undefined Variable mat2' errors when I run the following code:

for($i=0;$i<3;$i++)
{
   for($j=0;$j<3;$j++)
   {
       $ans[$i][$j] = 0;
       for($k=0;$k<3;$k++) 
       {
          $ans[$i][$j] = $ans[$i][$j] + $mat1[$i][$k]*$mat2[$k][$j];
       }
   }
}
var_dump($ans);

I tried to define them with following 2 lines:

$mat1=array(array());
$mat2=array(array());

But errors were changed to 'Undefined offset: ...' errors. Am I missing something in my code?!

  • 写回答

3条回答 默认 最新

  • doujiene2845 2014-12-16 04:29
    关注

    Why I'm getting Undefined Variable errors?

    Because you haven't defined anything.

    Just use

    $mat1 = $mat2 = array(
                0 => array(
                        0 => 1, 
                        1 => 1, 
                        2 => 1, 
                        3 => 1, 
                        4 => 1, 
                        5 => 1, 
                    ),
                1 => array(
                        0 => 2, 
                        1 => 2, 
                        2 => 2, 
                        3 => 2, 
                        4 => 2, 
                        5 => 2, 
                        ),
                2 => array(
                        0 => 3, 
                        1 => 3, 
                        2 => 3, 
                        3 => 3, 
                        4 => 3, 
                        5 => 3, 
                )
        );
    

    And test it

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站