开发者

Why is the return value of this PHP function zero for a long array?

开发者 https://www.devze.com 2023-01-15 15:05 出处:网络
When the row[\'error\'] is bigger than 35, the value isn\'t present and the result of the function is 0. Where is the problem?

When the row['error'] is bigger than 35, the value isn't present and the result of the function is 0. Where is the problem?

<?php
    if ($row['error'] == "")
    {
        $error = "0";
    }
    else
    {开发者_开发问答
        $error = $row['error'];
    }

    if ($row['error'] != "")
    {
       if (strlen($error) > 35)
       {
           $error = substr($row['error'],0,32) + "...";
       }
       else
       {
           $error = $row['error'];
       }
    }
?>


Change

$error = substr($row['error'],0,32) + "...";

to:

$error = substr($row['error'],0,32) . "...";

The concatenate operator in PHP isn't a plus (+) sign; it's a period (.) sign


All this code is not necessary. The second condition is redundant, and it doubles the else condition from the above. Make it all with just these few lines of code:

<?php
    $error = $row['error'];
    if (strlen($error) > 35) {
        $error = substr($row['error'],0,32) . "...";
    }
?>


Because you check:

if(strlen($error) > 35) {
}
0

精彩评论

暂无评论...
验证码 换一张
取 消