在课堂上声明变量

Is it okay to declare the variable any part of your class ? I have this code, I am using laravel 5.1 framework

In my controller

 public function myTest(){

     $i = 0;<--- is it okay to declare if we use oop,or we should put on top and declare private.
     foreach($myarray as $arr){
            // do something
             // do something with $i
      }


   }

This is fine. But your variable scope is limited to that function only. Outside that function you can not use that variable. As you used it inside loop, this will work. And you can make it global variable also. That will make your work easy.