TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

How PHP's foreach works

196 pointsby vlucasabout 12 years ago

6 comments

Wilduckabout 12 years ago
As far as I can tell from my reading, the strangeness stems from the fact that:<p>&#62; Arrays in PHP are ordered hashtables (i.e. the hash buckets are part of a doubly linked list)<p>And that iteration is done using a "internal array pointer":<p>&#62; This pointer is part of the HashTable structure and is basically just a pointer to the current hashtable Bucket. The internal array pointer is safe against modification, i.e. if the current Bucket is removed, then the internal array pointer will be updated to point to the next bucket.<p>Which together require some complex copying rules to allow for some simple things like iterating over the same array in nested loops.<p>I'm not very familiar with the implementation details of many other languages with these constructs, but in python a `for` loop (which operates similarly to the described `foreach` loop in php) simply operates over an iterator, which have a well defined implementation [1]. I don't know about the implementation any deeper than that, however.<p>I'm curious how other languages implementation of foreach type constructs stack up and how the choice of implementation for the standard list/array datatype affects the interface.<p>[1] <a href="http://excess.org/article/2013/02/itergen1/#iterators" rel="nofollow">http://excess.org/article/2013/02/itergen1/#iterators</a>
dansoabout 12 years ago
Is this actually Stackoverflow or an impostor phishing site? I don't see the "Question has been closed as not constructive" notice even though this question meets all the requirements for it.
评论 #5295593 未加载
评论 #5296550 未加载
评论 #5296046 未加载
评论 #5295598 未加载
评论 #5296431 未加载
评论 #5297318 未加载
SeoxySabout 12 years ago
If anybody feels like explaining something else that's also puzzling about the Zend engine and PHP arrays; I had a few hours spent the other day on a WTF moment writing a PHP extension in C and querying array zvals.<p>I was doing something fairly simple, trying to extract values passed as named argument to a function and turning them back into simple C types (char * and int):<p><pre><code> // capturing hash keys as zvals zval **salt_hex_val; zval **key_hex_val; zval **iterations_val; if ( // getting values zend_hash_find(hash, "salt", strlen("salt") + 1, (void**)&#38;salt_hex_val) == FAILURE || zend_hash_find(hash, "key", strlen("key") + 1, (void**)&#38;key_hex_val) == FAILURE || zend_hash_find(hash, "iterations", strlen("iterations") + 1, (void**)&#38;iterations_val) == FAILURE || // checking types Z_TYPE_PP(salt_hex_val) != IS_STRING || Z_TYPE_PP(key_hex_val) != IS_STRING || (Z_TYPE_PP(iterations_val) != IS_LONG &#38;&#38; Z_TYPE_PP(iterations_val) != IS_DOUBLE) ) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not extract and check types on required values in hash: salt, key, and iterations."); RETURN_NULL(); } char *salt_hex; char *key_hex; if (Z_STRLEN_PP(salt_hex_val) != salt_length * 2 || Z_STRLEN_PP(key_hex_val) != key_length * 2) { php_error_docref(NULL TSRMLS_CC, E_WARNING, "Key or Salt length incorrect."); RETURN_NULL(); } salt_hex = Z_STRVAL_PP(salt_hex_val); key_hex = Z_STRVAL_PP(key_hex_val); int iterations = (Z_TYPE_PP(iterations_val) == IS_LONG ? (int)Z_LVAL_PP(iterations_val) : (int)Z_DVAL_PP(iterations_val)); </code></pre> The part that I still don't understand (but that I figured out by trial-and-error) was why `zend_hash_find` takes a `void••`[1] as argument, which should actually be a `zval•••` cast as `void••`. What's the purpose of the triple pointer here?<p><pre><code> zend_hash_find(hash, "salt", strlen("salt") + 1, (void**)&#38;salt_hex_val) </code></pre> [1]: Imagine the • there is a star / asterisk.
评论 #5296074 未加载
评论 #5301700 未加载
stormbrewabout 12 years ago
I think the interesting thing that this highlights about php, perhaps especially for people who've never worked in it, is the fact that php is an extremely rare example of a scripting language that has value semantics for complex objects.<p>I've always found that an interesting choice.
评论 #5296988 未加载
nkozyraabout 12 years ago
So basically it operates on a copy unless it determines it doesn't need to?<p>I'm not sure why this is interesting.
评论 #5295793 未加载
francispellandabout 12 years ago
Wasn't this a given when working with PHP? You can afterall send the reference so that you are modifying the array as you go, rather than at the end.<p>$array = array(1,2,3,4,5); foreach ($array as &#38;value){...}
评论 #5296419 未加载
评论 #5295878 未加载