★ wanayoo — archive 1999 http://php.net/manual/it/function.sizeof.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Arrays
Quick Reference
Arrays
English version of this pageGerman version of this pageJapanese version of this page
Italian version of this pageFrench version of this pageHungarian version of this page
Spanish version of this page
* array
* array_count_values
* array_diff
* array_flip
* array_intersect
* array_keys
* array_merge
* array_merge_recursive
* array_multisort
* array_pad
* array_pop
* array_push
* array_rand
* array_reverse
* array_shift
* array_slice
* array_splice
* array_unique
* array_unshift
* array_values
* array_walk
* arsort
* asort
* compact
* count
* current
* each
* end
* extract
* in_array
* key
* krsort
* ksort
* list
* natsort
* natcasesort
* next
* pos
* prev
* range
* reset
* rsort
* shuffle
* sizeof
* sort
* uasort
* uksort
* usort

  Thanks to:
 Chek.com
 easyDNS
 VA Linux Systems

  Related sites:
Apache
MySQL
PostgreSQL
Zend Tech.

  Community:
LinuxFund.org
OSDN
Manual: sizeof
View the source code for this pageSearch the site



Previous page
 shuffle
 Updated
Mon, 18 Sep 2000
sort 
Next page


sizeof

(PHP3 <= 3.0.16, PHP4 )

sizeof -- Get the number of elements in an array

Description

int sizeof (array array)

Returns the number of elements in the array.

See also count().


User Contributed Notes: sizeof


HSH@taxon.demon.nl
25-Jun-1999 04:09
If you're used to arrays in other languages, this function may trip you up: it returns the number of elements in the array that is actually filled.
If only myarray[2] has a value and mayarray[0] and myarray[1] do not, then *sizeof(mayarray)* will return *1*, not *3*.

This is caused by the fact that arrays here are actually always associative arrays. If you're filling some elements of the array using an index counter, you may want to save the counter value for the highest element to access the array later on.

count() does the same thing: it also returns only the number of actually _filled_ elements.



christian@schmidt.net
08-Dec-1999 10:50
Note that sizeof($x), where $x is a variable that is set but is not an array sizeof, returns 1. Hence you have to be careful e.g. when you are dealing with arrays of multiple dimensions.

for ($i = 0; $i < sizeof($myarray[$j]); $i++) will not work as expected if $myarray[$j] hasn't been assigned a value (i.e. an array value).



sgarner@expio.co.nz
10-Jan-2000 09:22
If some elements in your array are not set, then sizeof() and count() will not return the index of the last element, but will return the number of set elements. To find the index of the last element in the array:

<pre>
end($yourArray);
$index = key($yourArray);
</pre>

... Where $yourArray is the array you want to find the last index ($index) of.



cu@ulakbim.gov.tr
04-Feb-2000 09:12
Remember, PHP code is interpreted so sizeof() is always evaluated in place and everytime. So a very simple code like below can blow up your server box (tested :)

<PRE>
&lt;?
$arr= range(0,19);
echo sizeof($arr)."
";
for($i=0; $i<=sizeof($arr); $i++) {
// <= is used intentionally
$arr[$i] = "";
echo sizeof($arr)." Passing over #".$i."
\n";
}
?>
</PRE>

The assignment $arr[$i] when used in conjunction with <= in the for loop causes the assignment to add an extra element to the end of the array. So when the for-condition is next time checked when $i=20, sizeof($arr) will yield 21, the next time 22, and so on. So the program in fact starts to leak heavily and very rapidly.
You can instead use:
<PRE>
&lt;?
//...
$c = sizeof($arr);
for($i = 0; $i <= $c; $i++) {
// <= is used intentionally again ...
?>
</PRE>
This one will add the 21th element, but will stop then. No leak at all...



lev@artifacts.de
16-Aug-2000 10:17
To avoid such leaks (pressuming that you only want to pass through the array) and speeding up the code, you simply should use 'each'. 'for'-maniacs can use the following example too:

for ($i=sizeof($arr);$i--;)
{
echo $arr[$i];
...some sort of buggin code...
}

$i will be initialized only once! :-)



 About Notes


Previous page
 shuffle
 Updated
Mon, 18 Sep 2000
sort 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.