Problems easily overlooked when using explode to split strings in php 1. explode method description
The explode method splits a string into arrays based on a string as a boundary point.
array explode ( string $delimiter , string $string [, int $limit ] )1
Returns an array composed of strings. each element is a substring of a string and is separated by a string delimiter as a boundary point.
Parameter description:
Delimiter
Delimiter on the boundary.
String
Input string
Limit
If the limit parameter is set and it is a positive number, the returned array can contain up to limit elements, and the last element will contain the rest of the string.
If the limit parameter is negative, all elements except the last-limit element are returned.
If the limit value is 0, it is regarded as 1.
2. example
Use to split strings
';print_r($arr);echo '
';?> 1234567
Output:
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9)123456789101112
Use, split string, limit is positive
';print_r($arr);echo '
';?> 1234567
Output:
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5,6,7,8,9)12345678
Used to split the string. the limit value is negative.
';print_r($arr);echo '
';?> 1234567
Output:
Array( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6)123456789
Used to split the string. the limit value is 0.
';print_r($arr);echo '
';?> 1234567
Output:
Array( [0] => 1,2,3,4,5,6,7,8,9)1234
3. easy to ignore
In general, we use explode to split data such as id strings.
$v){ // do sth }}?>123456789
Normally, ids is not empty, and no problem is found when you look at the code, because if (data) is judged; it is generally considered that null processing has been done.
However, the actual situation is that ids = null, but data is not empty, which may cause problems in the code executed in foreach.
123456789
Because ids = null, The explode is used for segmentation, and the resulting Array is Array ([0] =>) rather than Array ().
Therefore, you need to modify the settings to avoid problems.
$v){ // do sth }}?>