Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <blockquote> <p>Is good practice to control the encoding of the string received, to avoid unexpected errors?</p> </blockquote> <p>No. There is no reliable way to detect the incoming data's encoding<sup>*</sup>, so the common practice is to <strong>define</strong> which encoding is expected:</p> <ul> <li><p>If you are exposing an API of some sort, or a script that gets requests from third party sites, you will usually point out in the documentation what encoding you are expecting.</p></li> <li><p>If you have forms on your site that are submitted to scripts, you will usually have a site-wide convention of which character set is used.</p></li> </ul> <p>The possibility that broken data comes in is always there, if the declared encoding doesn't match the data's actual encoding. In that case, your application should be designed so there are no errors except that a character gets displayed the wrong way. </p> <p>Looking at the encoding that the request declares the incoming data to be in like @Ignacio suggests is a <em>very</em> interesting idea, but I have never seen it implemented in the PHP world. That is not saying anything against it, but you were asking about common practices.</p> <p><sub>*: It <em>is</em> often possible to <em>verify</em> whether incoming data has a specific encoding. For example, UTF-8 has specific byte values that can't stand on their own, but form a multi-byte character. ISO-8859-1 special characters overlap with those values, and will therefore be detected as invalid in UTF-8. But <em>detecting</em> a completely unknown encoding from an arbitrary set of data is close to impossible. </sub> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload