explode()
returns an array of strings, so they're being compared lexicographically. You need to convert them to numbers so that max()
will compare them numerically.
$logitemArray = array_map('intval', explode("
", $logContents));
echo max($logitemArray);
BTW, you can use the file()
function to read a file directly into an array of lines, instead of using file_get_contents()
followed by explode()
.
$logitemArray = array_map('intval', file("logs/mainlog.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));