PHP Development Website Coding Unification problem

Source: Internet
Author: User
Tags php class sublime text

A good website code neat, comments appropriate is the most basic, is also a good habit, which can avoid the very messy in the future of their own feeling is messy, once the restructuring of the trouble is a time-consuming, the site of the entire system encoding is the most important aspect, in order to site the stability of the proposed PHP program, HTML page, CSS and JS files, the database encoding is all UTF-8 encoding, so that the site can be accessed in any location can be displayed correctly, of course, it is also possible to use the gb2312 or GBK code to occupy less space, but note also to encode unified, not the same to convert.

1, the first HTML file, the file itself is determined by the editor, regardless of whether you use the entity or hexadecimal encoding is independent of the page encoding is not related, here the editor should be set to pure utf-8 and gb2312, do not bring BOM head otherwise sometimes problems, vim, sublime Text, Gedit Editor are the default pure UTF-8 encoding, page encoding is determined to utf-8 encoding, we also use <meta> tag to set the page encoding, to the HTTP header, so that the browser correctly parse, the code is as follows:

<charset= "Utf-8">

This type of writing is HTML5 standard notation, the browser is now supported, no longer like HTML4 write so long

2, then is the PHP file, or the file itself encoding to determine, if the PHP tag is embedded in the HTML, then and 1 as suggested in the <meta> tag declaration, if it is a PHP class file, you do not have to declare the encoding, keep the file itself code, PHP scripts that do not embed HTML each file must be encoded in the first line of the PHP file, using the header () function to encode the HTTP header with the following code:

Header ("content-type:text/html; Charset=utf-8 ");

That's it, okay?

3, CSS and independent JavaScript code, this does not have to declare, the file itself with Utf-8 encoding write line, they default is UTF-8 encoding, especially JavaScript pass the parameter is UTF-8 encoding, if the page or database is GBK encoding, then transcoding.

4, database encoding, set up a database when the general set of character sets, we have to maintain a unified set to UTF8 series on the line, generally choose utf8_general_ci this character set, and then use PHP to connect the database and select the database, you must set the database encoding, set the code as follows:

mysql_query (' Set names UTF8 ');

5, if the interaction with other websites or crawl Web pages and their own site code is inconsistent, then need to convert the encoding and URL encoding decoding operation, character encoding we can use the Iconv () function or mb_convert_encoding () function to encode the conversion, If send to URL to encode and decode with UrlEncode () and Urldencode () function, the focus is to find out our own Web site file encoding, Declaration header code, request parameters and so on all the code is unified, and other Web site interaction as long as they are converted to the same as their website encoding on the line, Then the flexible use of these functions is no problem, the other to the browser to complete, we directly to the browser to send characters or URL encoding is OK, the key is consistent coding

After the above steps of the code is unified, for the entire Web site garbled problem can be solved, this is not easy to understand at the beginning, more practice more thinking of the natural can master good

PHP Development Website Coding Unification problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.