Makaronen Makaronen - 2 months ago 6
PHP Question

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.

Answer

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.

Comments