Re: Count empty array

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu, 21 Dec 2006, Kevin Murphy wrote:

I'm wondering why this is.

$data = "";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$array has 1 element: An empty string.

$data = "Test";
$array = explode(",",$data);
$count = count($array);
$count will = 1

$array has 1 element: The string "Test"

$data = "Test,Test";
$array = explode(",",$data);
$count = count($array);
$count will = 2

$array has 2 elements:.....

Why doesn't the first one give me an answer of 0 instead of 1. I know I could do a IF $data == "[empty]" and then not count if its empty and just set it to 0, but am wondering if there was a better way.

Because explode divides the string in n+1 elements, where n is the amount of "," (or your favorite delimiter) found in the string. So if no "," is found, explode will return 1 element: the whole string (even if it's empty).

--
 21:50:04 up 2 days,  9:07,  0 users,  load average: 0.92, 0.37, 0.18
---------------------------------------------------------
Lic. Martín Marqués         |   SELECT 'mmarques' ||
Centro de Telemática        |       '@' || 'unl.edu.ar';
Universidad Nacional        |   DBA, Programador,
    del Litoral             |   Administrador
---------------------------------------------------------
-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux