Ubuntu php garbled code solution
When using Ubuntu to build a PHP development environment, you may encounter garbled code problems. While this problem isn’t too big of a problem for professional web developers, it can be a very tricky one for beginners. This article will introduce how to solve the PHP garbled problem under Ubuntu.
First of all, you need to determine the garbled code problem. In PHP, garbled characters may appear in many places, such as HTML pages, database storage, file reading and writing, etc. Therefore, the specific garbled problem needs to be determined first.
If it appears in the HTML page, you can add the following meta tag to the page to specify the character set:
If it appears in the database storage, you can specify it in the database connection Character set:
$conn = mysqli_connect('localhost', 'username', 'password', 'db_name'); mysqli_set_charset($conn, 'utf8');
If it appears in the reading and writing of the file, you can specify the character set when opening the file:
$file = fopen('example.txt', 'r'); stream_filter_append($file, "convert.iconv.utf-8/cp936");
After determining the garbled code problem, we must further determine the character set to be used. If everything else is correct but the character set is incorrect, garbled characters will continue to occur. Typically, utf-8 is the most commonly used character set. You can use the following code in PHP code to determine the current character set:
echo ini_get('default_charset');
If the current character set is not utf-8, you can modify the default character set to utf-8 in the php.ini file.
To modify the php.ini file, enter the following command in the terminal:
sudo nano /etc/php/7.2/apache2/php.ini
Here with PHP 7.2 and Apache2 For example, the specific version will be modified according to the actual situation. The file path may also vary depending on the installation method.
Then find the following lines of code, uncomment and modify them to utf-8:
; default_charset = "UTF-8" default_charset = "utf-8" ; input_encoding = "UTF-8" ; output_encoding = "UTF-8"
Note that if the above code does not exist in the php.ini file, you need to add it manually.
After modifying the php.ini file, you need to restart Apache for the modification to take effect. Enter the following command in the terminal to restart Apache:
sudo service apache2 restart
After restarting Apache, you need to test whether the garbled problem is solved. You can write a simple PHP file to test. For example, create a file named index.php with the following content:
Then, access the file in the browser to check whether there is any garbled code problem.
If none of the above methods can solve the garbled code problem, you can also try the following methods:
Summary
When using Ubuntu to build a PHP development environment, garbled characters are a common problem. Typically, this problem can be solved by specifying the character set and modifying the php.ini file. If none of these methods solve the problem, you need to have a deeper understanding of PHP and related technologies for further debugging and optimization.
The above is the detailed content of How to solve ubuntu php garbled code. For more information, please follow other related articles on the PHP Chinese website!