中文在gb2312编码的时候是2字节,但在unicode编码的 情况下是1-3字节不等,英文则都是1字节,但在mysql中不做这方面的设定,varchar的长度不管是中文和英文都是一格字算一个,那么。。为什么要做这个1个汉字相当于2个英文字符的设定?
回复内容:
中文在gb2312编码的时候是2字节,但在unicode编码的 情况下是1-3字节不等,英文则都是1字节,但在mysql中不做这方面的设定,varchar的长度不管是中文和英文都是一格字算一个,那么。。为什么要做这个1个汉字相当于2个英文字符的设定?
http://xfhnever.com/blog/2014/12/20/encodingformat/ 中稍微介绍了一下各种编码格式
varchar(20)指定的是字符长度
mysql中会指定表的字符集,如下面的CHARSET=gbk
:
<code> CREATE TABLE `test_type` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`), ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gbk COLLATE=gbk_bin </code>
为什么要做这个1个汉字相当于2个英文字符的设定?
这种设定是一般基于字符串的自然长度(也就是显示的长度), 一个汉字等于2个英文字母或数字。对于普通用户来讲就是字样,计算字节一般不都是程序和程序员干的事么。所以一般的程序设计中,一般遵循着一个汉字就是2个英文字母长度的设定,而具体占用空间则视编码和环境进行计算。
参建函数 mb_strwidth()
最早出现的编码是ASCII这样面向英文语言的编码,ASCII定义了0-127,一共128个字符的编码。对于英语这样用26个字符的,自然是够用的。问题是,如果是其他使用的字符多余26个的西方文字呢?由于已经约定了0x7F空间内的字符编码,所以我们可以看到有些西方文字,例如俄文是完全不和英文共用部分字符的,需要使用ASCII扩展集。在编码理论发展的过程,出现过各个地域分别采用不同编码体系来编码本国文字。中国是使用ISO2022体系,编码了中国的常用字符GB2312,更多的字符GBK,以及最大的集合GB18030。
通过简单的思考,你会发觉一个字节8bits只能编码类似英文这样的文字系统,而中文的常用字就有几千上万个。所以至少需要2个字节。
Unicode的出现,目的是统一全世界所有的字符编码空间,不会造成字符编码冲突问题。例如,如果你用GBK编码的字符,用UTF-8编码解释,也可能是字面有效的。Unicode编码有UTF-8,UTF-16,UTF-32,甚至UTF-7等多种格式。常说的Unicode编码是指UTF-16,因为它能以统一的方式编码几乎所有常用的字符,所以如果不是针对极端情况,我们可以假设字符都能用UTF-16表示。在UTF-16编码的情况下,汉字和英文每个字符的长度都是2个字节,是等长的。在Windows系统内部就是利用这种特性,高效的处理文字。UTF-8应用也很广泛,好处是存储上来说空间节省,解码复杂些。
MySQL在多个层面上都做了Encoding的设计,支持在不同层面上指定字符编码。
不过显然,我们应该推荐在各个层面是使用UTF-8编码。如果是内存中处理,推荐使用UTF-16,其实各种语言在设计中,好像也是这样设计的。

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP remains important in modern web development, especially in content management and e-commerce platforms. 1) PHP has a rich ecosystem and strong framework support, such as Laravel and Symfony. 2) Performance optimization can be achieved through OPcache and Nginx. 3) PHP8.0 introduces JIT compiler to improve performance. 4) Cloud-native applications are deployed through Docker and Kubernetes to improve flexibility and scalability.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment