2

When I tested this little script:

$str = "apple";

echo md5($str);

The result matched the result of doing md5 using utf8 (tested using C#)

Should I trust that this will always be the case in any other environment?

If I where to put this script on any webhost, windows or linux, would it behave always the same with UTF8 encoding ?

sharp12345
  • 4,420
  • 3
  • 22
  • 38
  • Using the same input it will always give you the same output, is this what you're asking? – Maerlyn May 25 '13 at 19:50
  • @Maerlyn Yes, I am worried that the input would have a different encoding based on the environment, like coming from a GET variable, or pre-processed by web-browser or web-server. – sharp12345 May 25 '13 at 19:52
  • You'll need to make sure it's in the same encoding. – Maerlyn May 25 '13 at 19:53
  • Related question: encoding is different in Javascript and PHP https://stackoverflow.com/questions/51866469/how-to-transport-data-from-javascript-to-php – William Entriken Aug 16 '18 at 13:52

2 Answers2

6

The encoding of a string literal is whatever encoding you saved the source file in. If you saved this php file in UTF-16, you would get a different result, that is, if the code even runs.

There is no unified or managed encoding in PHP. Strings in PHP can be in any encoding, in other words, they are equivalent to byte arrays of languages that have more abstract string type.

Esailija
  • 138,174
  • 23
  • 272
  • 326
0

Simply md5() will always give the same encoding. If you cannot trust it, Simply you can encode data in Database itself.

user9371102
  • 1,278
  • 3
  • 21
  • 43