I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.
I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)
I would use count() if they are the same, as in my experience it is more common, and therefore will cause less developers reading your code to say "sizeof(), what is that?" and having to consult the documentation.
I think it means sizeof() does not work like it does in C (calculating the size of a datatype). It probably made this mention explicitly because PHP is written in C, and provides a lot of identically named wrappers for C functions (strlen(), printf(), etc)
According to phpbench:
Is it worth the effort to calculate the length of the loop in advance?
//pre-calculate the size of array
$size = count($x); //or $size = sizeOf($x);
for ($i=0; $i<$size; $i++) {
//...
}
//don't pre-calculate
for ($i=0; $i<count($x); $i++) { //or $i<sizeOf($x);
//...
}
A loop with 1000 keys with 1 byte values are given.
| count() | sizeof() | |
|---|---|---|
| With precalc | 152 | 212 |
| Without precalc | 70401 | 50644 |
(time in µs)
So I personally prefer to use count() instead of sizeof() with pre calc.
What is the difference between sizeof and count in PHP?
$array = array("red", "green", "blue");
echo sizeof($array); // 3
echo count($array); // 3
How to use sizeof to count array elements in PHP?
$array = array("apple", "banana", "orange");
$count = sizeof($array);
echo $count; // Output: 3
Can sizeof handle multidimensional arrays?
$array = array(
"fruits" => array("apple", "banana"),
"colors" => array("red", "blue")
);
echo sizeof($array); // 2
echo sizeof($array, COUNT_RECURSIVE); // 6
strlen returns the number of bytes in the string, not the character length. View the PHP Manual here.
Look closely at:
Note:
strlen() returns the number of bytes rather than the number of characters in a string.
If you take the result and multiple by 8, you can get bits.
Here is a function which can easily do the math for you.
function strbits($string){
return (strlen($string)*8);
}
Note, if you use, memory_get_usage(), you will have the wrong value returned. Memory get usage is the amount of memory allocated by the PHP script. This means, within its parser, it is allocating memory for the string and the value of the string. As a result, the value of this before and after setting a var, would be higher than expected.
Example, the string: Hello, this is just test message, produces the following values:
Memory (non-real): 344 bytes
Strlen: 32 Bytes
Strlen * 8bits: 256 bits
Here is the code:
<?php
$mem1 = memory_get_usage();
$a = 'Hello, this is just test message';
echo "Memory (non-real): ". (memory_get_usage() - $mem1)."\n";
echo "Strlen: ". strlen($a)."\n";
echo "Strlen * 8bits: ". (strlen($a) * 8)."\n";
$start_memory = memory_get_usage();
$foo = "Some variable";
echo memory_get_usage() - $start_memory;
This is good if you are working with any type of var.