search for in the  
<shufflesort>
Last updated: Thu, 19 May 2005

sizeof

(PHP 3, PHP 4, PHP 5)

sizeof -- Alias of count()

Description

This function is an alias of: count().



User Contributed Notes
sizeof
pfortin at ss2i dot ca
07-Jul-2004 10:25
Well, I'm not very sure about the above and it seems a bit overkill if you want my opinion. I prefer to use foreach() or the array_keys function if you only need the selected indexes ... Example :

if(sizeof($_POST['Checkboxes']))
{
           foreach($_POST['Checkboxes'] as $Index => $Value)
           {
                     DoStuffHere();
           }
}

OR

$keys = array_keys($_POST['Checkboxes']);

Using this method, you have the $Index set, which in the above exemple would be the array offset. You can use the functions array_keys() as well which will return the keys of the array, in this case the selected checkboxes.
jdittmer at ppp0 dot net
20-Feb-2004 05:46
This gets pretty near the size of an array:
strlen(implode('', array_values($array) + array_keys($array)).

The internal size is perhaps more like:
strlen(serialize($array).
Fallout2man
10-Nov-2003 04:02
This is a little something that I made to mimic C's sizeof() for a script I may never complete, but I'll post it here for everyone, as it may be helpful. I haven't tested this so not quite sure if it works.

 function c_sizeof($type,$auto_detect=0) {
       if (!is_array($type)) { // if we're a single data type
           if ($auto_detect=0) { //if someone is just telling us a type
               switch ($type) {
                   case "char": {
                       return 1;
                   }
                   case "wchar_t": {
                       return 2;
                   }
                   case "int": {
                       return 4;
                   }
                   case "short": {
                       return 2;
                   }
                   case "long": {
                       return 4;
                   }
                   case "float": {
                       return 4;
                   }
                   case "double": {
                       return 8;
                   }
                   case "long double": {
                       return 10;
                   }
                   case "bool": {
                       return 1;
                   }
               }
           } else {// or else we're an actual type
               $tvar=true;
               switch ($tvar) {
                   case is_short($type): {
                         unset($tvar);
                         return 2;
                   }
                   case is_int($type): {
                       unset($tvar);
                       return 4;
                   }
                   case is_float($type): {
                       unset($tvar);
                       return 4;
                   }
                   case is_double($type): {
                       unset($tvar);
                       return 4;
                   }
                   case is_bool($type): {
                       unset($tvar);
                       return 1;
                   }
                   case is_string($type): {
                       unset($tvar);
                       return strlen($type);
                   }
               }
           }
       } else { // or if we're an array
           // calculate array data usage in bytes.
           return c_sizeof_array($type);
       }
   }

   function c_sizeof_array(&$struct) {
   /* The recursive function for getting
       the size in bytes of an array */
       $element_size=0;
       foreach ($struct as $key=>$val) {
           if (!is_array($val) {
               $element_size= ($element_size + c_sizeof($val,1));
           } else {
               $element_size= ($element_size + c_sizeof_array($val));
           }
       }
       return $element_size;
   }
gramo at skina dot com dot co
25-Mar-2003 11:48
[ Editor's Note: Unchecked boxes are not passed to the form action page at all.  Therefore if only two of the four checkboxes below are checked, only two will be passed to the action page and sizeof/count will return two.  grammo's statement that the array contains NULL elements is incorrect. ]

When you have a php page receiving data from a form with an array like:

--------8<----------------8<----------------8<--------
<FORM ACTION="receiver.php" METHOD=POST>
   <INPUT TYPE=CHECKBOX NAME="arreglo[0]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[1]">Thing #2
   <INPUT TYPE=CHECKBOX NAME="arreglo[2]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[3]">Thing #4
   <INPUT TYPE=SUBMIT>
</FORM>
--------8<----------------8<----------------8<--------

And you just pick Thing #2 and Thing #4 like:
--------8<----------------8<----------------8<--------
[ ] Thing #1
[X] Thing #2
[ ] Thing #1
[X] Thing #4
[OK]
--------8<----------------8<----------------8<--------

When you count the number of elements in arreglo {NULL, "on", NULL, "on"} you receive just a 2 for answer, because NULL counts as no element for the count function.

These have huge importance when you generate the forwarding form with a database query and you need to know how many elements does the script generate.

The only way to fix these problem is by adding a new element at the form in this way:
--------8<----------------8<----------------8<--------<FORM ACTION="receiver.php" METHOD=POST>
   <INPUT TYPE=HIDDEN NAME="arreglo_size" VALUE="4">
   <INPUT TYPE=CHECKBOX NAME="arreglo[0]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[1]">Thing #2
   <INPUT TYPE=CHECKBOX NAME="arreglo[2]">Thing #1
   <INPUT TYPE=CHECKBOX NAME="arreglo[3]">Thing #4
   <INPUT TYPE=SUBMIT>
</FORM>
--------8<----------------8<----------------8<--------

<shufflesort>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT