Home >Backend Development >PHP Problem >How to detect garbled characters in php

How to detect garbled characters in php

藏色散人
藏色散人Original
2020-09-28 09:17:423484browse

php method to detect garbled characters: first create a PHP sample file; then use the "json_encode($string) === 'null'" method to determine the garbled characters in the string; finally check the return result. .

How to detect garbled characters in php

Recommended: "PHP Video Tutorial"

php determines whether a string contains garbled characters

Determine whether a string contains garbled characters:

1: You can use regular matching, but you need to know what symbols the garbled characters probably include.

2. Use black magic json_encode( $string) === 'null' to judge. If the string contains garbled characters, json_encode will return null.

Summary of several methods to determine whether a variable is empty in PHP

Judge whether a variable is empty is used on many occasions. At the same time, you and yourself Many novices often make some mistakes, so I sorted out some commonly used methods in PHP to determine whether a variable is empty

1. isset function: determine whether the variable has been initialized

Note: It does not determine whether the variable is empty, and can be used to determine whether the elements in the array have been defined.

Note: When using isset to determine whether the array elements have been initialized, it The efficiency is about 4 times higher than array_key_exists

The code is as follows:

$a = '';
$a['c'] = '';
if (!isset($a)) echo '$a 未被初始化' . "";
if (!isset($b)) echo '$b 未被初始化' . "";
if (isset($a['c'])) echo '$a 已经被初始化' . "";
// 显示结果为
// $b 未被初始化
// $a 已经被初始化

2. empty function: detect whether the variable is "empty"

Note: Any uninitialized variable, a variable with a value of 0 or false or an empty string "" or null, an empty array, or an object without any attributes will be judged as empty==true

Note 1: Uninitialized variables can also be detected as "empty" by empty

Note 2: empty can only detect variables, not statements

Code As follows:

$a = 0;
$b = '';
$c = array();
if (empty($a)) echo '$a 为空' . "";
if (empty($b)) echo '$b 为空' . "";
if (empty($c)) echo '$c 为空' . "";
if (empty($d)) echo '$d 为空' . "";

3. var == null Function: Determine whether the variable is "null"

Description: The value is 0 or false or the empty string "" or null variable, empty array , will be judged as null

Note: The significant difference from empty is that var == null will report an error when the variable is not initialized.

The code is as follows:

$a = 0;
$b = array();
if ($a == null) echo '$a 为空' . "";
if ($b == null) echo '$b 为空' . "";
if ($c == null) echo '$b 为空' . "";
// 显示结果为
// $a 为空
// $b 为空
// Undefined variable: c

4. is_null function: detect whether the variable is "null"

Description: When the variable When assigned a value of "null", the detection result is true

Note 1: null is not case-sensitive: $a = null; $a = NULL makes no difference

Note 2: Only in When the value of the variable is "null", the detection result is true. 0, empty string, false, and empty array are all detected as false

Note 3: When the variable is not initialized, the program will report an error

The code is as follows:

$a = null;
$b = false;
if (is_null($a)) echo '$a 为NULL' . "";
if (is_null($b)) echo '$b 为NULL' . "";
if (is_null($c)) echo '$c 为NULL' . "";
// 显示结果为
// $a 为NULL
// Undefined variable: c

5. var === null function: detect whether the variable is "null", and the type of the variable must also be " null"

Note: When a variable is assigned a value of "null" and the type of the variable is also "null", the detection result is true

Note 1: When it is judged to be "null" , all equal to the same effect as is_null

Note 2: When the variable is not initialized, the program will report an error

Summary In PHP, "NULL" and "empty" are two concepts.

isset is mainly used to determine whether a variable has been initialized.

empty can set variables with values ​​of "false", "empty", "0", "NULL", and "uninitialized" Judge as TRUE

is_null Only judge the variable with value "NULL" as TRUE

var == null Judge as "false", "empty", "0", "NULL" All variables are judged to be TRUE

var === null. Only variables with a value of "NULL" are judged to be TRUE

. Therefore, when we judge whether a variable is really "NULL", most of the Use is_null to avoid interference with values ​​such as "false" and "0".

The above is the detailed content of How to detect garbled characters in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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