$list = "1,2,4,10,8,0,20,30,9";
$token = strtok($list, ",");
while($token)
{
print $token."\n";
$token = strtok(",");
}
?>
Using the code we get this output:
1
2
4
10
8
Where are the other four integers? Actually when the value of $token is zero (0) - the sixth integer in the list, while ($token) evaluates to False and the loop is broken there. Lets use while($token != FALSE) instead.
$token = strtok($list, ",");
while($token != FALSE)
{
print $token."\n";
$token = strtok(",");
}
Still the same output. Oh, the mistake is we used the '!=' operator which tests the equality between $token and FALSE. Here zero (0) is equal to FALSE. A stupid way to avoid the problem is to calculate the length - while(strlen($token)) and it will work definitely! as the length of the string '0' is 1. But best solution (to me) is to use the '!==' operator which means not identical. So the following code works fine:
$list = "1,2,4,10,8,0,20,30,9";
$token = strtok($list, ",");
while($token !== FALSE)
{
print $token."\n";
$token = strtok(",");
}
?>
Output:
1
2
4
10
8
0
20
30
9
It's always useful to know details about operators in PHP.
2 comments:
Or you could use explode(',', $string) and just get an array instead of doing your own tokenizing.
The development of the IT sector especially in the countries such as India and China has gone a long way in changing the face of these countries. The rise of the middle class, the interest that the foreign market is showing in these countries, and the overall development of the economies of these countries can be massively owed to the growth and flourish of the IT sector. http://www.infysolutions.com/resources/resources.html
Post a Comment