2

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";
}
1
  • note that the check is actually redundant in this case as implode with an array of size 1 will give the desired result also. Just replace the whole thing with echo implode(", ", $array) . " have been entered"; and skip the check. Commented Dec 8, 2009 at 8:35

3 Answers 3

3
if (count($array) == 1){
        echo $array[0] . " has been entered";
} else {
        echo implode(", ", $array) . " have been entered";
}
Sign up to request clarification or add additional context in comments.

2 Comments

sizeof() also does the same thing, if you prefer writing sizeof instead of count.
sizeof is just an alias to count.
1
if (count($array)){
    echo implode(", ", $array) . " have been entered";
}

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.