In this article, you are going to learn the use of count() and compact() functions in php. Basically, these are array functions and are fry cry from each other. Count() is used to calculate the number of elements present in an array and an object. Compact() is used to create an array comprising of several variables along with their values.
Now, let’s go through the syntax, return value and an example of each of the above functions so as to get a clear picture regarding the use of both the functions. Join w3training school php training in Gurgaon and practice of similar functions over live projects.
COUNT() FUNCTION
Syntax : count($array_or_countable, $mode);
Two parameters are used in this function which are discussed below.
- $array_or_countable- this parameter represents an array of elements or objects to be counted or determined.
- $mode- this is an optional(additional) parameter that can have two values
(1) COUNT_NORMAL – it will count the elements normally. It will not include all the elements when dealing with multidimensional array.
(2) COUNT_RECURSIVE – it will count the elements recursively. It will include all the elements when dealing with multidimensional array.
Result/ Return Value : There are three possible return value of the count() function.
- It will return an integer value i.e, the number of elements on the successful execution of the php script.
- It will return 1, if the value in array_or_countable parameter is neither an array nor an object but some other countable value.
- It will return 0, if the array_or_countable parameter is set Null.
Errors : There are no significant error. However, an E_WARNING is generated whenever it tries to restrict infinite looping while counting the elements recursively.
Example : An example of count() function is shown below. Here we have taken 6 colors to be counted namely red, pink, yellow, green, white, orange.
Output : You will get the following output after running the above php script. It will count all the elements(colors) and will output 6 as we have used same number of colors.
COMPACT() FUNCTION
Syntax : compact($variable1, $variable2, $…..);
The parameters used in this function represents the following.
- string comprising the name the variable, and
- an array of elements comprising the name the variables.
Result/ Return Value : The successful execution of this function will return an array of elements along with the value of each element.
Errors : There are no such influencing error. It should be noted that this function cannot pass superglobal arrays.
Example : We have shown below an example of compact() function to create an array of elements.
Output : You will get the following output after running the above php script. Clearly, we have used three variables $firstname, $lastname and $age having values ‘ABC’, ‘XYZ, and ’27’ respectively. These variables are converted into an array of elements with their respective values as shown in the image below.
We have provided you the best possible description on count() and compact() functions in php. We have tried to simplify this concept by giving an example of each with their outputs. Hope you liked this article. For more articles and updates, keep reading our php blogs.