How to get the length of the array in PHP?

How to get the length of the array in PHP?

Right now, will figure out how to get the length of the exhibit in PHP? There are 2 worked in functions that arrival us the length of the exhibit:

  • 1) tally(),
  • 2) sizeof().

Exhibits are basic information type in PHP and we require it ordinarily to store comparable information type in one element. As a PHP designer, it is essential to realize the activities on the cluster, for example, getting its length. Utilizing the implicit PHP functions we can get the length of the cluster.

There are 2 worked in functions that arrival us the length of the cluster:

  • check()
  • sizeof()

We’ll talk about them in the accompanying area…

1) Using the check() function

The check() function takes in the exhibit parameter and returns the tally of the quantity of component present in that cluster. The tally() function is progressively well known and generally utilized that the other function. It essentially comprises of the circle that has a counter component which crosses the entire exhibit and returns the last value, for example, the length of the cluster.

Code

<?php
	//Create a hobbies array
	$hobbies = array('Painting', 'Coding', 'Gaming', 'Browsing');

	//Get the number of elements in hobbies array
	$hobby_count = count($hobbies);

	//Display the hobbies count
	echo $hobby_count; // Output: 4
?>

Right now, make the $hobbies variable which comprises of 4 components. At that point, we have another variable hobby_count which stores the aftereffect of the check() activity applied on the leisure activities exhibit which is the number of components in it. At that point, we basically show the hobby_count variable which shows the number of components in the leisure activities cluster.

2) Using the sizeof() function

The sizeof() is the false name of the check(a) strategy that profits the number of components in the cluster simply like the tally() function. Rarely with engineers, since it mistakes for the sizeof administrator in C. We should think about an example,

Code

<?php
	//Create a hobbies array
	$hobbies = array('Painting', 'Coding', 'Gaming', 'Browsing');

	//Get the number of elements in hobbies array
	$hobby_count = sizeof($hobbies);

	//Display the hobbies count
	echo $hobby_count; // Output: 4
?>

As should be obvious, the outcome is the same, since these functions are the nom de plume of one another. While it is suggested that you utilize the principal function to restore the length of the exhibit, it is acceptable to realize that there are different strategies to do it.

Expectation you like the article. If it’s not too much trouble share your contemplations in the remarks.

Recent Posts

error: Alert: Content is protected!!