Jack The Baker
Jack The Baker

Reputation: 1883

Count cookie items

I show number of items in a cookie with:

if(isset($_COOKIE['Bookmark'])) {
$sAds = explode(",",$_COOKIE['Bookmark']);
$countSAds = sizeof($sAds);
$savedAds = '<span>'.$countSAds.'</span>';
} else {
$savedAds = ''; 
}

And in html:

echo $savedAds; // output: <span>3</span>

Also sample data of cookie:

252,190,210

This is return 3.

But if there is no bookmark (item) set in cookie, it show 1, when there is nothing in this cookie, it return 0 so by sizeof it return 1 because it count number of array, how can I count cookie items if it is empty and should return 0

Example:

sample data is nothing, I mean nothing in cookie, code return <span>1</span> but I want to show 0

Upvotes: 0

Views: 220

Answers (1)

Syscall
Syscall

Reputation: 19780

You could use !empty() instead of isset() to check if there is something in the cookie:

if (!empty($_COOKIE['Bookmark'])) {
    $sAds = explode(",",$_COOKIE['Bookmark']);
    $countSAds = sizeof($sAds);
    $savedAds = '<span>'.$countSAds.'</span>';
} else {
    $savedAds = ''; 
}

Upvotes: 1

Related Questions