What is the use of count () function in PHP ?

  • In PHP this unbuilt function is used to count the current elements in the array.
  • If it has been set to an empty array, whether the function might return 0 for the variable and the variable that is not set the function returns 0.
  • Generally, this function takes one parameter, the array for which the elements need to be counted.
  • The function an take a parameter mode which tells the function to count the element in recursive or normal mode but in addition.
  • In Count () syntax $array is mandatory and in which elements are needed to be counted.
  • In Count () mode is optional which is used to set the mode of function and their parameter can take two possible values, either 0 or 1. 1 generally indicates to count of the values of the array recursively.
  • In an array, the functions return the number of elements.

Syntax

[pastacode lang=”php” manual=”Count%20(%24array%2C%20mode)” message=”” highlight=”” provider=”manual”/]

Sample Code

[pastacode lang=”php” manual=”%3C!DOCTYPE%20html%3E%0A%20%20%3Chtml%3E%0A%20%20%20%3Cbody%3E%0A%0A%20%20%20%20%20%3C%3Fphp%0A%20%20%20%20%20%20%20%24cars%3Darray(%22Volvo%22%2C%22Volkswagen%22%2C%22Hyundai%22%2C%22Ferrari%22)%3B%0A%20%20%20%20%20%20%20echo%20count(%24cars)%3B%0A%20%20%20%20%20%20%20%3F%3E%0A%0A%20%20%20%3C%2Fbody%3E%0A%3C%2Fhtml%3E%0A” message=”” highlight=”” provider=”manual”/]

Output

Leave a Reply

Your email address will not be published. Required fields are marked *

You May Also Like