[PHP] Count empty array
I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 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. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326
Re: [PHP] Count empty array
On Thu, 2006-12-21 at 13:31 -0800, Kevin Murphy wrote: I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 Why doesn't the first one give me an answer of 0 instead of 1. For the same reason the second one gives you a count of one. There are no commas and so only one item exists. Whether that be an string with content or the empty string itself is irrelevant. Cheers, Rob. -- .. | InterJinn Application Framework - http://www.interjinn.com | :: | An application and templating framework for PHP. Boasting | | a powerful, scalable system for accessing system services | | such as forms, properties, sessions, and caches. InterJinn | | also provides an extremely flexible architecture for | | creating re-usable components quickly and easily. | `' -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count empty array
Kevin Murphy wrote: I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 Why doesn't the first one give me an answer of 0 instead of 1. This: var_dump(explode(',','')); Returns this: array(1) { [0]= string(0) } And oddly, This: var_dump(explode(',',NULL)); Returns this: array(1) { [0]= string(0) } That explains the count() result. It seems to me that the first case could go either way (The part of before the ',' is still ), but I'm unable to think of a logical reason why the second doesn't return an empty array. 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. $array = empty($data) ? array() : explode(',',$data); /* That what you're looking for? */ jon -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Count empty array
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
Re: [PHP] Count empty array
Not sure why it does it, but doesn't seem to be a huge deal. I'm guessing it's because an empty string is still a string. It's not null. Anyway, it's documented at: http://us3.php.net/manual/en/function.explode.php A user writes: If you split an empty string, you get back a one-element array with 0 as the key and an empty string for the value. -TG = = = Original message = = = I'm wondering why this is. $data = ; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test; $array = explode(,,$data); $count = count($array); $count will = 1 $data = Test,Test; $array = explode(,,$data); $count = count($array); $count will = 2 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. -- Kevin Murphy Webmaster: Information and Marketing Services Western Nevada Community College www.wncc.edu 775-445-3326 ___ Sent by ePrompter, the premier email notification software. Free download at http://www.ePrompter.com. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php