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条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码