为什么我的变量在for循环语句中取消定义?

时间:2016-05-25 20:18:11

标签: php laravel-5.2

为什么在if语句中定义的for循环语句中变量$ myVar未定义,如下所示(Laravel 5.2中的刀片模板)?

  $myVar = [];

  @if (empty($myVar))
     Var is empty.
  @endif

  @foreach ($myVar as $var)
    <li>{{$var}}</li>
  @endforeach

2 个答案:

答案 0 :(得分:0)

也尝试isset

$myVar = [];

@if (empty($myVar))
   Var is empty.
@endif

@if (isset($myVar))
   Var is isset.
@endif


@foreach ($myVar as $var)
   <li>{{$var}}</li>
@endforeach

答案 1 :(得分:0)

我通过更改返回视图('process',withMyVar($ myVar); with view('process',compact($ myVar);

)使其工作