Laravel中的PHP If语句参数

时间:2018-11-28 07:35:34

标签: php laravel if-statement laravel-blade

我要进行计算,其中变量$totalPercent可能为0。在这种情况下,我向脚本中添加了一些if语句,使其仅在初始返回后除以零的错误返回有时。

这是新修订的计算,现在可以在$totalPercent为0的情况下使用,但是当它有一个值并且调用了假定的脚本时,它返回了一个错误Undefined variable: designPercent.

这是新脚本,其中添加了if语句,仅当$totalPercent大于0时才打算进行计算。

@if ($totalnumerical > '0')
    $totalPercent= '100' / $totalnumerical;
    $stylePercent= ($styletempt *  $totalPercent).'%';
    $designPercent= ($designtempt * $totalPercent).'%';
    $managePercent= ($managetempt * $totalPercent).'%';
    <div class="graphSection">
        <div class="skillGraph">
            <span style="width:{{$designPercent}}" class="bar-1"></span>
            <span style="width:{{$stylePercent}}" class="bar-2"></span>
            <span style="width:{{$managePercent}}" class="bar-3"></span>
        </div><br>
        <div class="skillGraph graph_text">
            @if(($designtempt * $totalPercent) > 15)
                <span style="width:{{$designPercent}}" >Design</span>
            @else
                <span class="hovertext" style="width:{{$designPercent}}; margin-top:-57px" >Design</span>
                <span style="width:{{$designPercent}}" ></span>
            @endif
            @if(($styletempt * $totalPercent) > 15)
                <span style="width:{{$stylePercent}}" >Interior Styling</span>
            @else
                <span class="hovertext" style="width:{{$stylePercent}}; margin-top:-67px;" >Interior Styling</span>
                <span style="width:{{$stylePercent}}" ></span>
            @endif
            <span style="width:{{$managePercent}}" >Project Management</span>
        </div>
    </div>
@endif

1 个答案:

答案 0 :(得分:1)

刀片视图中的所有内容都解释为文本,而不是代码。如果要在视图中添加php代码,则需要将此代码放在@php blocks中。

@if ($totalnumerical > '0')
    @php
        $totalPercent= '100' / $totalnumerical;
        $stylePercent= ($styletempt *  $totalPercent).'%';
        $designPercent= ($designtempt * $totalPercent).'%';
        $managePercent= ($managetempt * $totalPercent).'%';
    @endphp
    <div class="graphSection">
        <div class="skillGraph">
            <span style="width:{{$designPercent}}" class="bar-1"></span>
            <span style="width:{{$stylePercent}}" class="bar-2"></span>
            <span style="width:{{$managePercent}}" class="bar-3"></span>
        </div><br>
        <div class="skillGraph graph_text">
            @if(($designtempt * $totalPercent) > 15)
                <span style="width:{{$designPercent}}" >Design</span>
            @else
                <span class="hovertext" style="width:{{$designPercent}}; margin-top:-57px" >Design</span>
                <span style="width:{{$designPercent}}" ></span>
            @endif
            @if(($styletempt * $totalPercent) > 15)
                <span style="width:{{$stylePercent}}" >Interior Styling</span>
            @else
                <span class="hovertext" style="width:{{$stylePercent}}; margin-top:-67px;" >Interior Styling</span>
                <span style="width:{{$stylePercent}}" ></span>
            @endif
            <span style="width:{{$managePercent}}" >Project Management</span>
        </div>
    </div>
@endif