简体   繁体   English

在数组中打印出错误的值

[英]Printing out the wrong value in array

<?php
$arr = [];
$highNum = null;
$longest = null;

foreach ($_SERVER as $key => $value) {
    $num = strlen($value);
    $arr = nl2br("$key: $num\n");
    echo $arr;
    if ($num > $highNum) {
        $highNum = $num;
        $longest = $key;
    }
}
?>

<p>The longest entry is: <?= htmlentities($_SERVER[$longest]); ?></p>
<p>And its value is: <?= print(strval($highNum)); ?></p>

When I run the code it prints out the longest entry but when I print out the value, say for example that the value is 746, I get 7461. I've been stuck at this one for quite a while now and I can't seem to get rid of the one at the end. 当我运行代码时,它会打印出最长的条目,但是当我打印出该值时,例如说该值为746,我将得到7461。似乎摆脱了最后一个。 If I remove 1 from the value I get 7451 instead. 如果我从值中删除1,则会得到7451。

Write this: 这样写:

<p>And its value is: <?= strval($highNum); ?></p>

Since you're doing echo already, you don't need to use print again. 由于您已经在执行echo ,因此无需再次使用print

Note: print also return 1 upon successful printing. 注意:成功打印后, print也会返回1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM