renjith's blog

thoughts from my perspective

What is the string length of an array() construct?

What is the string length of an array() construct?

This was a tricky question asked in Zend certification Exam

When ever you try to echo strlen(array()), it gives you answer 5.
Reason
When ever you are trying to use strlen for a reserved keyword or language construct, it will give the length of the
corresponding datatype For Eg: the above operation give datatype array which contains 5 letters.
Here is one more example

Class Foo {
public function the() {
echo ‘test’;
}
}

$obj = new Foo();
echo strlen($obj);
echo strlen(clone($obj));

Here result is 6 that means datatype is ‘object’ contains 6 letters.
For me its a new information.. what about you?

Post to Twitter Tweet This Post


Categorized as Technology

5 Comments

  1. Hey this is a new info for mee too…
    nice article.. keep updating these sort of tips

  2. Really I dont understand.. Very much differ from C++ coding synatx. I suspect ur case with our STL classes (container classes like std::string, std::vector)

    In C++ even if we didn’t put some values(say strings) into it, its capacity() will be > 1. Compiler will reserve some memory space before initializing the variable.

    Keep updating ur blog. !!!

  3. Its pretty much same in php as well id you didn’t put any values also.. its capacity will be 1.
    But here as in C strlen is not meant for array.. its for strings..
    if we try to use strlen for an array it was giving answer 5.. I was wondering why it happend..but after some R & D i found that it is giving the string length of datatype :) for Eg: array containd 5 letters

  4. Satyender hooda says:

    Sorry Dear, I think you are using old version.In Latest version ie. PHP 5.3 , it gives an error. we can’t get length of an array.

  5. Keep posting stuff like this i really like it

Leave a Reply