Home Backend Development PHP Tutorial 关于PHP文件及编码的有关问题

关于PHP文件及编码的有关问题

Jun 13, 2016 pm 01:22 PM
ascii mysql php quot utf8

关于PHP文件及编码的问题
1:PHP代码文件本身的编码格式,是GB或者UTF8会影响什么?
2:PHP代码文件中是混杂的,有GB又有UTF8会导致什么样的不好结果么?
3:PHP语言中的字符串编码格式是什么?(比如C语言就是ASCII,但C#就是UNICODE,不知道PHP是什么)
4:PHP代码文件的编码格式跟MYSQL的数据库字符编码格式不同会导致什么不好的结果么?
5:MYSQL本身的编码格式会影响输出到页面上的文字的效果么?

问题好像有点多了,请大家指教,非常感谢.一定及时结帖给分.

------解决方案--------------------
1:PHP代码文件本身的编码格式,是GB或者UTF8会影响什么?
只要与 html 语言声明不冲突,就没有关系

2:PHP代码文件中是混杂的,有GB又有UTF8会导致什么样的不好结果么?
多半会差生“乱码”,而使你无所适从

3:PHP语言中的字符串编码格式是什么?(比如C语言就是ASCII,但C#就是UNICODE,不知道PHP是什么)
由你决定

4:PHP代码文件的编码格式跟MYSQL的数据库字符编码格式不同会导致什么不好的结果么?
只要通知 mysql 需要什么编码的内容,没有不好的结果

5:MYSQL本身的编码格式会影响输出到页面上的文字的效果么?
不会

------解决方案--------------------
1.一个人的血型是 O 或者A 会有什么影响?

2.一个人的血型又是O 又是A 某个时候是正常的 比如可能产生了稀有血液,大多数时候是直接死亡。

3.php你就是造物主 你想怎么造就怎么造.

4.不同会让你戴上1000度的眼镜也无法看明白那串乱码到底是什么... 当然改造一下眼镜问题也就解决了。

5.数据库就是你的弹仓,如果你不一哆嗦...那么他不会发射出来,他会老老实实的呆着,直到....你觉得足够兴奋,你要输出了.这个时候你才要考虑编码格式的事情.正确的解决方法就是 在建这个弹仓的时候 先考虑好所有编码的统一,就万事具备了。


------解决方案--------------------
程序文件用什么编码无所谓,只要兼容ASCII就行。关键是数据库存储用UTF-8就行
------解决方案--------------------
首先文件本身的编码格式要指定,并且项目中要统一。其次就是参数编码也要指定统一,否则中文会乱码。与mysql的编码不一致,要么设置mysql数据库的编码集要么用php转码函数转码,否则中文乱码
------解决方案--------------------
大神来了。(- -#)


源代码中的字面常量字符串的编码直接受源代码文件编码控制,举例:

$str = "你好吗";

1,如果是GBK编码保存的这三个汉字,那么硬盘上实际占用了6个字节。
2,如果是UTF8编码保存的这三个汉字,那么硬盘上实际占用了超过6个字节。

假如文件是UTF8保存的,那么你希望转化这个字符串为GBK的,并发给浏览器。

那么mb_convert_encoding($str, "utf8", "gbk")就会得到一个gbk编码的“你好吗”,而你这个函数的调用的效果就是告诉PHP解释器:这个字符串里的字节是UTF8编码的,你得按照UTF8编码解释这一段字节,并将这三个字符转化成GBK编码的6个字节。 

之后echo输出到浏览器,并content type告诉浏览器这一段字节是gbk的,请按照gbk编码去解释这6个字节。
------解决方案--------------------
看了你这边说的有点晕....
CI的代码...如果没有记错,应该都是英文等...
新浪的开放平台不知道,但是一般情况下,代码也应该都是英文及符号等....

对于127以下的ascii字符,包括英文及符号,数字, 他们的ascii码和utf8码是完全一样的....
所以可能不存在你说的需要统一的问题


探讨

呵呵,楼上的解释真油菜~

其实现实情况是这样的,我的一个PHP项目,使用的是CI框架,它的代码本身是ANSI编码的,然后我们的项目里面又调用了新浪的开放平台接口,它提供的接口代码又是UTF-8的,这该如何是好?难道手动统一一遍?

请大神们救命~谢谢

------解决方案--------------------
怎么处理?改成统一的编码
当然你也可以给 php 部分请个翻译。php 提供了 mb_string 和 iconv 两个函数组,都可以方便的解决你的问题
------解决方案--------------------
探讨

1:PHP代码文件本身的编码格式,是GB或者UTF8会影响什么?
只要与 html 语言声明不冲突,就没有关系

2:PHP代码文件中是混杂的,有GB又有UTF8会导致什么样的不好结果么?
多半会差生“乱码”,而使你无所适从

3:PHP语言中的字符串编码格式是什么?(比如C语言就是ASCII,但C#就是UNICODE,不知道PHP是什么)
由你决定

4:PHP代码文件的编码……

------解决方案--------------------
探讨
1:PHP代码文件本身的编码格式,是GB或者UTF8会影响什么?
2:PHP代码文件中是混杂的,有GB又有UTF8会导致什么样的不好结果么?
3:PHP语言中的字符串编码格式是什么?(比如C语言就是ASCII,但C#就是UNICODE,不知道PHP是什么)

------解决方案--------------------
探讨
可是还是碰到一个问题,就是一个项目中,如果有一部分PHP文件中含了中文字符,比如要echo出一些文本到页面,然后另一个PHP文件中也包含了中文,但这个文件是UTF-8编码的,在输出的时候按前面各位讲的方法可以控制,但是用IDE编辑代码的时候就比较痛苦,比如NetBeans,要指定一个项目的编码格式,如果指定为ANSI,那UTF8那些页面编辑时就显示乱码,如果指定为UTF8那么有ANSI编码的那些文件编辑时就显示为乱码,所以很不方便.今天又试了一下VS.PHP,还没确认是否可能规避这个问题.
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

How to view database password in Navicat for MariaDB? How to view database password in Navicat for MariaDB? Apr 08, 2025 pm 09:18 PM

Navicat for MariaDB cannot view the database password directly because the password is stored in encrypted form. To ensure the database security, there are three ways to reset your password: reset your password through Navicat and set a complex password. View the configuration file (not recommended, high risk). Use system command line tools (not recommended, you need to be proficient in command line tools).

Navicat cannot connect to MySQL/MariaDB/PostgreSQL and other databases Navicat cannot connect to MySQL/MariaDB/PostgreSQL and other databases Apr 08, 2025 pm 11:00 PM

Common reasons why Navicat cannot connect to the database and its solutions: 1. Check the server's running status; 2. Check the connection information; 3. Adjust the firewall settings; 4. Configure remote access; 5. Troubleshoot network problems; 6. Check permissions; 7. Ensure version compatibility; 8. Troubleshoot other possibilities.

Navicat connects to database error code and solution Navicat connects to database error code and solution Apr 08, 2025 pm 11:06 PM

Common errors and solutions when connecting to databases: Username or password (Error 1045) Firewall blocks connection (Error 2003) Connection timeout (Error 10060) Unable to use socket connection (Error 1042) SSL connection error (Error 10055) Too many connection attempts result in the host being blocked (Error 1129) Database does not exist (Error 1049) No permission to connect to database (Error 1000)

How to execute sql in navicat How to execute sql in navicat Apr 08, 2025 pm 11:42 PM

Steps to perform SQL in Navicat: Connect to the database. Create a SQL Editor window. Write SQL queries or scripts. Click the Run button to execute a query or script. View the results (if the query is executed).

How to create a new connection to mysql in navicat How to create a new connection to mysql in navicat Apr 09, 2025 am 07:21 AM

You can create a new MySQL connection in Navicat by following the steps: Open the application and select New Connection (Ctrl N). Select "MySQL" as the connection type. Enter the hostname/IP address, port, username, and password. (Optional) Configure advanced options. Save the connection and enter the connection name.

See all articles