FukHaLfDaN
FukHaLfDaN

Reputation: 25

PHP how to count the arrays values?

I'm trying to count all the values in the array and check if one value was entered and if so display a certain message for it and if there was more then one value entered display a different message for it.

How will I be able to this and where should I add it to my code?

Here is the code below.

if ($array == 1){
    echo $array . " has been entered";
} else {
    echo implode(", ", $array) . " have been entered";
}

Upvotes: 2

Views: 618

Answers (3)

Peter Lindqvist
Peter Lindqvist

Reputation: 10200

if (count($array)){
    echo implode(", ", $array) . " have been entered";
}

Upvotes: 1

yu_sha
yu_sha

Reputation: 4410

count($array) gives you number of elements in the array

Upvotes: 0

Scott Evernden
Scott Evernden

Reputation: 39940

if (count($array) == 1){
        echo $array[0] . " has been entered";
} else {
        echo implode(", ", $array) . " have been entered";
}

Upvotes: 3

Related Questions