-2

Given this list.txt file:

9
10
19

Guess what?

max(split("\n",$file_handle)) gives me 9

max(explode(PHP_EOL,$file_handle)) gives me 9

Now, on a string:

$string = "9 10 19"; max(split("\n",$string)) gives me 19

Updated: When reading from a file, I get strings and max() can't seems to find the correct max when comparing string values?! 9 is bigger than 10. The same file content on a string works fine. file_get_contents()/fread() same issue.

MPaulo
  • 1,491
  • 14
  • 19
  • What you've mentioned above for 90 is what I'd expect for a string comparison because it starts at the first character and 9 is greater than the first 1 in 100. I'm not sure the best way to do it in PHP but you probably want something like this: http://stackoverflow.com/questions/9593765/how-to-convert-array-values-from-string-to-int – PeterJ Mar 07 '15 at 04:56
  • Works fine: http://3v4l.org/qgnnZ. Something wrong with reading from file. – sectus Mar 07 '15 at 05:02
  • Yes, sectus, that's the issue. When reading from the file it get's interpreted as "string" and thus 9 is bigger than 10. I wonder why? – MPaulo Mar 07 '15 at 05:04
  • No, max works fine with number strings. Show how are you reading. – sectus Mar 07 '15 at 05:15
  • Seems it does not... when I convert to numbers it can successfully finds the correct max. – MPaulo Mar 07 '15 at 05:22
  • @billynoah I have updated the question. I am sorry for the confusion. It was code mixed with talk. :( – MPaulo Mar 07 '15 at 05:31
  • ok thanks.. have you tried `explode()` instead of `split()`? I've never used `split` and it's deprecated.. not sure if that would have any bearing. – But those new buttons though.. Mar 07 '15 at 05:32
  • try `max(explode(PHP_EOL,$file_handle))` and let me know if that helps – But those new buttons though.. Mar 07 '15 at 05:34
  • @billynoah explode() fails also. when seems to work when converting to numbers. The weird thing is from a string works fine. Go guess. – MPaulo Mar 07 '15 at 05:38
  • what do you get when you `var_dump($file_handle);`? Something tells me it doesn't contain what you think it does. perhaps `max(explode(PHP_EOL,file_get_contents('list.txt')))`; – But those new buttons though.. Mar 07 '15 at 05:42
  • @billynoah file_get_contents fails also – MPaulo Mar 07 '15 at 05:46
  • please, add var_dump of $file_handle. – sectus Mar 07 '15 at 05:46
  • http://zuma-design.com/shared/list.png – But those new buttons though.. Mar 07 '15 at 05:55
  • @billynoah With the exact same code, I get 9. Maybe php version? – MPaulo Mar 07 '15 at 05:59
  • Mystery finally solved: list.txt was in a DOS format! The dos end line character in use on the file was somehow causing PHP to interpret it as a string instead of an integer by default. In short, the difference is that the UNIX end-of-line character is a line feed/newline character (\n). The DOS/Windows end-of-line character is a carriage return, followed by a line feed/newline (\r\n). – MPaulo Mar 25 '15 at 18:16

2 Answers2

3

Are you sure that EOL (end-of line) is "\n"?

Look at this answer:

Explode PHP string by new line

And check this for EOL explanation:

Difference between \n and \r?

Community
  • 1
  • 1
teo
  • 801
  • 6
  • 14
  • That was indeed the issue! The dos end line character in use on the file was somehow causing PHP to interpret it as a string instead of an integer by default. I converted list.txt file to use the unix line breaks instead of the dos breaks, and now script is working as expected. – MPaulo Mar 25 '15 at 18:18
0

This solves it. Still a mystery to me why values come as string from file and as numbers from a string with the same content. Anyway, here's the fix:

max(array_map('intval', split("\n",$file_handle)));
MPaulo
  • 1,491
  • 14
  • 19