Syntax error with complex code for calculating min

<?php
$d = array();
echo intval($d) . "<br>";
$e = array("red", "green", "blue");
echo intval($e) . "<br>";
$values = array(-1,0,1,12,-100,1);
echo "output2" . (min(array_diff(array_map('intval', $values), array(0)))) . "<br>";
//echo min(array_diff(array_map('intval', $values), array(0))); also gives error
echo "output#2".min($values);
?>

The error message is:

2021/08/03 18:47:11 [error] 1888#1888: *3 FastCGI sent in stderr: “PHP message: PHP Parse error: syntax error, unexpected ‘$values’ (T_VARIABLE), expecting ‘,’ or ‘)’ in /var/www/html/w3_intvalf.php on line 7” while reading response header from upstream, client: 127.0.0.1, server: _, request: “GET /w3_intvalf.php HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php7.2-fpm.sock:”, host: “localhost”

Also I found that if I just do:

echo “output#2”.min($values);

my answer would be -100 which the syntax error prone code is trying to achieve:

echo “output2” . (min(array_diff(array_map(‘intval’, $values), array(0)))) . “
”;

I got the code from Q17 here:

Q17

Zulfi.

I copied and pasted your code into an online PHP tester and it gave the following result:

0<br>1<br>output2-100<br>output#2-100

If I un-comment your commented-out line which says “also gives error”, I get

0<br>1<br>output2-100<br>-100output#2-100
1 Like

Not to mention is seems awfully convoluted for a problem statement defined as ‘calculating min’.
min already handles strings…

1 Like

Hi,
Thanks. I commented that line and wrote the same thing again by creating a line above it. It worked. Next time I would try this strategy.

God blesses you.

Zulfi.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.