简体   繁体   中英

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. If I remove 1 from the value I get 7451 instead.

Write this:

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

Since you're doing echo already, you don't need to use print again.

Note: print also return 1 upon successful printing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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