serialize function trouble

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Ralphy
Forum Newbie
Posts: 2
Joined: Thu Mar 03, 2011 6:10 pm

serialize function trouble

Post by Ralphy »

I hope you guys can help me, Im getting serialized values from a database field but if the field is empty, the unserialized array returns 1 with the count function when it should be 0.

Anyone have any ideas?
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: serialize function trouble

Post by greyhoundcode »

Per the manual:
PHP wrote:If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL, 0 will be returned.
So if for example your field is basically an empty string then this is expected behaviour, and is not a problem resulting from the unserialize() function. You might want to change the conditions of your test.

Code: Select all

if (count($result) == 0 || empty($result))
{
    // Empty field / no elements in the array
}
Post Reply