8

In java and C++ when we don't know the size - array not used like in PHP, instead used linkedList etc.

In PHP exist SPL, but most of the times programmers use array, why (because people don't know about SPL )?

When we should use Array in PHP and whenSPL and what is the difference in this case between PHP and Java/C++?

1

4 Answers 4

4

Every PHP request must initialize all variables and after request they are freed. Because of that not often comes situations where special data structures (like maxheap, linkedlist or queue) are more efficient than array. Also arrays are much simpler to understand and use for beginner.

Difference from C++ in PHP is that arrays length is dynamic. You can add elements whenever you want.

$arr=array();
$arr[]=5; //add integer to array
echo count($arr); //1
$arr[]=7;
echo count($arr); //2

you can dynamically create and add array to another array

$arr[]=array();
$arr[2][]=5;
echo count($arr); //3
echo count($arr[2]); //1

This will create new array, add element with value 5 and add it as element to array $arr.

$arr[][]=5;

In PHP arrays are hash tables, so you can have not only integer keys but also strings:

$arr['somekey']='somevalue';

If array element is integer then each element requires a value structure (zval) which takes 16 bytes. Also requires a hash bucket - which takes 36 bytes. That gives 52 bytes per value. Memory allocation headers take another 8 bytes*2 - which gives 68 bytes.

About arrays in PHP: http://oreilly.com/catalog/progphp/chapter/ch05.html

Sign up to request clarification or add additional context in comments.

2 Comments

dinamic php array mean that each time that new element inserted created new array. Or for example php array have default size like 100 cells and she increase if array need more then 100 cells by multiply 2 etc..??
Each time new element is created new memory for variable is allocated and new memory for hash is allocated.
1

Use standard arrays, it is faster than ArrayObject.

Use ArrayObject only to implement your own specified arrays with your custom methods.

2 Comments

Why does "it's slower" always have to be the first arguments against something?
php.net/manual/pt_BR/class.arrayobject.php#90371 check this comment for benchmark. Performance is a very important parameter in heavy application, sorry.
1

You're asking us to compare two massively different things, almost the only similarity being that they're both (arrays and the SPL) available in PHP.

To that end, it would be essentially nonsensical to compare directly, or prescribe, when one should be used over the other for times when both might be used to accomplish a task. On that note, both might be used intertwined: for example, using the ArrayIterator to iterate over an array, or the ArrayObject to make use of array-style syntax when working with objects.

You also seem to be confused, or just unclear, about what the SPL is; it certainly is not restricted to tools used to iterate over collections of things. Did you mean to ask about specific parts of the library, or are you perhaps just unclear what is available in it?

1 Comment

I think i bit confuce avout SPL
0

Not necessarilly a confusion maybe, because people can decide to use "SplStack" instead of vanilla array associated with array_push/pop functions. Or use arrays in classical way,

and then there exist SplDoublyLinkedList, SplQueue, SplHeap. plus ArrayObject.

So the question is valid. And the answer is: you do the same in a more javaish way, and it is slower.

SPL generally speaking is a tentative to mimic standard & "off-the-shelf" classes & API, like there exist in C++ & Java.

IMHO it came too late.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.