如何在变量中使用'if'和'else'

问题描述 投票:0回答:6

我有以下PHP代码,我试图使用if / else语句来判断返回的字符串是否为空;但是,到目前为止我没有成功。

$result = '<div class="card-deck">';

if($rows > 1) {
    while($list = $engrams->fetch_assoc()) {
        $result .= '
        <div class="col-4 p-2">
            <div class="card border-info m-1">
                <img src="/assets/images/items/'.$list['image'].'" alt="'.$list['name'].'" class="card-img-top">
                <div class="card-body" style="margin-left: 60px; position: absolute; text-align: center;">
                    <p class="card-title">'.$list['name'].'</p>
                </div>
                <div class="card-body" style="margin-top: 50px;">
                    <p>'.$list['description'].'</p>
                    <p>'.$list['res_name1'].'</p>
                    <p>'.$list['res_name2'].'</p>
                    <p>'.$list['res_name3'].'</p>
                </div>
                <div class="card-footer d-flex justify-content-between">
                    <small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>
                    'if (empty($list['engram_points'])) {'<small>PLM</small>'}'
                    'else {'<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>'}'
                </div>
            </div>
        </div>
        ';
    }
}
$result .= '</div>';

echo json_encode(['result'=>$result,'rows'=>$rows,'qty'=>$qty,'active'=>$active])
php
6个回答
2
投票

如上所述,三元运算符是最佳选择。

与if else代码一起使用时,它将如下所示:

<div class="card-footer d-flex justify-content-between">
    <small>
        <span class="text-left text-muted">Level Required: '.$list['lvl'].'</span> 
    </small>'
    . ( empty($list['engram_points']) ? '<small>PLM</small>' : '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>' ) .
'</div>'

1
投票

以下应该做的伎俩:

        <div class="card-footer d-flex justify-content-between">
            <small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>
            '.empty($list['engram_points']) ? '<small>PLM</small>' : '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>'.'
        </div>

1
投票

你不能把代码放在像这样的字符串中。但是,你可以打破字符串并在循环中使用if / else ...像这样:

while($list = $engrams->fetch_assoc()) {
        $result .= '
        <div class="col-4 p-2">
            <div class="card border-info m-1">
                <img src="/assets/images/items/'.$list['image'].'" alt="'.$list['name'].'" class="card-img-top">
                <div class="card-body" style="margin-left: 60px; position: absolute; text-align: center;">
                    <p class="card-title">'.$list['name'].'</p>
                </div>
                <div class="card-body" style="margin-top: 50px;">
                    <p>'.$list['description'].'</p>
                    <p>'.$list['res_name1'].'</p>
                    <p>'.$list['res_name2'].'</p>
                    <p>'.$list['res_name3'].'</p>
                </div>
                <div class="card-footer d-flex justify-content-between">
                    <small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>';

                    if (empty($list['engram_points'])) {
                       $result .= '<small>PLM</small>';
                    }
                    else {
                       $result .= '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>;
                    }

        $result .= '        </div>
            </div>
        </div>
        ';
    }

1
投票

在将所有值分配给$result变量之前,您可以检查变量是否为空。

例:

if (empty($list['engram_points'])){
    $points = "<small>PLM</small>";
}else{
    $points = '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>';
}

 $result .= '
        <div class="col-4 p-2">
            <div class="card border-info m-1">
                <img src="/assets/images/items/'.$list['image'].'" alt="'.$list['name'].'" class="card-img-top">
                <div class="card-body" style="margin-left: 60px; position: absolute; text-align: center;">
                    <p class="card-title">'.$list['name'].'</p>
                </div>
                <div class="card-body" style="margin-top: 50px;">
                    <p>'.$list['description'].'</p>
                    <p>'.$list['res_name1'].'</p>
                    <p>'.$list['res_name2'].'</p>
                    <p>'.$list['res_name3'].'</p>
                </div>
                <div class="card-footer d-flex justify-content-between">
                    <small><span class="text-left text-muted">Level Required: '.$list['lvl'].'</span></small>
                   '.$points.'
                </div>
            </div>
        </div>
        ';

0
投票

您可以使用PHP的三元运算符,如下所示:

$var='<small>'.($list['engram_points']?$list['engram_points']:'PLM').'<small>';

格式为(expr1?expr2:expr3)并计算结果 - 如果设置了expr1则使用expr2否则使用expr3

更多信息:http://php.net/manual/en/language.operators.comparison.php


0
投票

如果你真的想把逻辑放在一个变量中,你可以使用一个闭包:

<?php

function createLister($list) {
              return function() use ($list) {
                  // Your logic
                  if (empty($list['engram_points'])) {
                      return '<small>PLM</small>';
                  } else {
                      return '<small><span class="text-right text-muted">Skill Points: '.$list['engram_points'].'</span></small>';
                  }
              };
}

$lister = createLister($list);
echo $lister(); // Your output

在这里查看更多:

http://php.net/manual/en/class.closure.php

否则,三元运算符可能就是您要寻找的:

<?php

$condition = true;

$doThis = 'Hello World';
$elseThis = 'Goodbye';

// Outputs 'Hello World'
$condition ? $doThis : $elseThis;
© www.soinside.com 2019 - 2024. All rights reserved.