8 ms·
How is that a "bullshit PHP rule"? If you for loop in C, setting a pointer each iteration, you would expect the pointer to still be set the the last assignment
by xd 14y ago
How is that a "bullshit PHP rule"? If you for loop in C, setting a pointer each iteration, you would expect the pointer to still be set the the last assignment in the loop once you're out of it.
- LnxPrgr3 14y agoC lets you define variables with block scope: int main(int argc, char *argv[]) { for(int i=0;i<10;++i) printf("%d\n", i); printf("%d\n", i); } The last line fails to compile—'i' is no longer defined after exiting the loop. Java: public static void main(String[] args) { for(int i=0;i<10;++i) System.out.println(i); System.out.println(i); } That also won't compile. Coming from a language that supports variables with block scope, PHP's behavior is very surprising indeed. If the first mention of $object is in the loop, I can understand someone expecting $object in the second loop to be a distinct variable that just happens to share the same name. This behavior is even surprising coming from Perl, though for a slightly different reason: @array = ('c', 'c++', 'java', 'perl'); foreach $item (@array) { if($item eq 'perl') { $item = 'php'; } } foreach $item (@array) { print $item . "\n"; } For that matter, PHP's reference semantics are a little surprising in general coming from any other language I've ever used.
- xd 14y agoI was thinking more of: #include <stdio.h> int main() { int arr[10] = {1,2,3,4,5,6,7,8,9,0}, *ptr; for(int i = 10; i>=0; i--) { ptr = &arr[i]; printf("%d\n", *ptr); } printf("%d\n", *ptr); }
- LnxPrgr3 14y agoSure, but you defined ptr outside of the loop. Who defined $object outside of the loop in the PHP example? Also note that, in C, you have to explicitly dereference pointers: ptr = 42; You should get a warning from your compiler if you try that. PHP effectively goes ahead and changes it to: *ptr = 42; Perl requires you to explicitly dereference references as well: @array = ('c', 'c++', 'java', 'perl'); $item = \$array[3]; $item = 'php'; # Does NOT replace the item in the array foreach $item (@array) { print $item . "\n"; } PHP is weird in this regard: if you ever assign a reference to a variable, later assignments go through that reference automatically. To convert the variable back to a normal variable, you have to unset it. I don't know of another language that acts like this. Can you think of one? Combine that with the lack of block scope, and you get surprises like the example BadCRC linked.
- ufo 14y agoWTF. Sometimes I wonder if I'm being unfairly prejudiced by not learning PHP myself and making my own decision about it but things like this make me change my mind.
- smsm42 14y agoYou are completely correct - you are being unfairly prejudiced by judging PHP on explanations of people that do not understand it instead of learning it. If you learned it, you would know references work exactly like they supposed to, and do exactly what they are meant to do - just because they are not, as parent assumes, pointers, he misunderstands them and thinks they are weird. Moreover, he thinks since PHP in not like C and does not have C-like pointers PHP is weird. Please read http://us2.php.net/references http://us2.php.net/references if you want real information instead of collection of misunderstanding.
- ufo 14y agoI think the whole point here is why does PHP even have references like that. Right now the only mainstream language I know that does that is C++ and well, its C++ and mutable references are actually not frequently used in practice. Most other languages stay with pass by value most of the time and PHP references kind of look like a leaky abstraction from the underlying C implementation...
- xd 14y agoI use references for passing around large arrays of data for instance, rather than having copies made all over the place (class instances are passed by reference no matter what you do).
- ufo 14y agoWhat I meant is that people usually pass things via a pointer (that needs to be explicitly dereferrenced) or via const references (where there is no mutation to worry about). Mutable references are rarer and usually they are function arguments and are relatively "obvious"
- drivebyacct2 14y agoI'm just upset that you don't have braces around the inner body of your `for` statements. I want to edit the DOM just to fix it.
- sneak 14y agoHis indent level is set enough that it's likely not an issue. (Yes, the "what if you add another statement line later?!" trope has been registered.)
- drivebyacct2 14y ago"Yeah, yeah, I know there's a nail in my tired, but I know it's there so it's okay."