subreddit stringclasses 7
values | author stringlengths 3 20 | id stringlengths 5 7 | content stringlengths 67 30.4k | score int64 0 140k |
|---|---|---|---|---|
lolphp | Rican7 | 23gu79 | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|eols|><|endoftext|> | 67 |
lolphp | Various_Pickles | cgx00tu | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|sor|>Any other (sane) language: "Index out of bounds, fuckface."
PHP: "i can count to potatoindex"<|eor|><|eols|><|endoftext|> | 57 |
lolphp | Rhomboid | cgwwwop | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|sor|>Phippy, the helpful paperclip, saw that you wrote empty string but really meant array and fixed it for you. Don't you like it when the computer helps you?
<|eor|><|eols|><|endoftext|> | 47 |
lolphp | Rican7 | cgx5cso | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|sor|>Unlike for example C, PHP has an inbuilt string datatype. The string datatype allows you array-like access to the single characters in the string but will always be a string. <|eor|><|soopr|>Yes, that is known.
.... but that's the problem, if the string is initially empty then it coerces the type to an array with the character access syntax.
Fail.<|eoopr|><|eols|><|endoftext|> | 16 |
lolphp | Bratmon | cgxa9l7 | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|sor|>Unlike for example C, PHP has an inbuilt string datatype. The string datatype allows you array-like access to the single characters in the string but will always be a string. <|eor|><|sor|>>but will always be a string
Did you actually read the link? <|eor|><|eols|><|endoftext|> | 9 |
lolphp | SockPants | cgwy62k | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|sor|>More string-related wat.
$wat1 = "wat";
$wat1[3] = "z";
var_dump($wat1);
$wat2 = "wat";
$wat2[4] = "z";
var_dump($wat2);
$wat3 = "wat";
$wat3[50] = "z";
var_dump($wat3);
Output:
string(4) "watz"
string(5) "wat z"
string(51) "wat z" (_)
Because creating a string with 47 0x00 bytes in it is preferable to something sane like an index-out-of-range exception.<|eor|><|sor|>Does it actually insert a space character or is that just how one or more null characters are displayed?<|eor|><|eols|><|endoftext|> | 5 |
lolphp | Rican7 | cgx0df7 | <|sols|><|sot|>String character access modification.... oh wait.<|eot|><|sol|>http://i.imgur.com/Jstrsim.png<|eol|><|sor|>More string-related wat.
$wat1 = "wat";
$wat1[3] = "z";
var_dump($wat1);
$wat2 = "wat";
$wat2[4] = "z";
var_dump($wat2);
$wat3 = "wat";
$wat3[50] = "z";
var_dump($wat3);
Output:
string(4) "watz"
string(5) "wat z"
string(51) "wat z" (_)
Because creating a string with 47 0x00 bytes in it is preferable to something sane like an index-out-of-range exception.<|eor|><|sor|>Does it actually insert a space character or is that just how one or more null characters are displayed?<|eor|><|soopr|>That's just how they're displayed on Reddit.
Although technically a NULL byte and an ordinary space are two different things:
A normal space character is technically ASCII 32 (0x20).
A null byte is technically represented as "\0" or ASCII 0 (0x00).<|eoopr|><|eols|><|endoftext|> | 5 |
lolphp | nsfwIvan | 13zfvk | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|eols|><|endoftext|> | 65 |
lolphp | noname-_- | c78rwh7 | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>The submitted code doesn't work, either. As written, it will loop endlessly, generating an infinite series of clones in a Fantasia-like manner<|eor|><|sor|>Wasn't that the point?<|eor|><|sor|>Eh. I remember yelling at a dude (Java) for claiming that HashSet is broken because he could make it break by violating the required consistency between .equals() and .hash(). /r/programming really railed at him for that. Breaking something and then claiming that it's bad is not a very good bug report. Also, he's claiming this is a problem with clone, when it's really just smashing the stack.
If he had used some finitely large stack and claimed it's the recursive depth at fault, I would have 100% backed him. But not when he submitted broken code and indicted the wrong part of php.<|eor|><|sor|>The interpreter should never segfault. The code he posted makes it do that, so it demonstrates a bug. It doesn't really matter if the code is broken or not. Or for that matter if his theory on what's causing the bug is correct.
It's analogous with a compiler segfaulting on a piece of source code. Yes, that piece of code is probably horribly broken, but the compiler should print an error message and exit gracefully, not segfault.<|eor|><|sor|>Oh, it's the interpreter segfaulting? I thought it was the user's code that experienced the segfault. I retract my defense of this error. My bad<|eor|><|sor|>There is no distinction in an interpreted language. The interpreter creates a virtual machine in which the user program lives. They share the same host process. But a user error should never propagate outside of the virtual machine. If it does there's a bug in the interpreter. There's no concept of a segmentation fault within the PHP virtual machine.
What I'm saying is that if the user code experiences a segmentation fault, there is a bug in PHP's interpreter.<|eor|><|eols|><|endoftext|> | 27 |
lolphp | noname-_- | c78qyoj | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>The submitted code doesn't work, either. As written, it will loop endlessly, generating an infinite series of clones in a Fantasia-like manner<|eor|><|sor|>Wasn't that the point?<|eor|><|sor|>Eh. I remember yelling at a dude (Java) for claiming that HashSet is broken because he could make it break by violating the required consistency between .equals() and .hash(). /r/programming really railed at him for that. Breaking something and then claiming that it's bad is not a very good bug report. Also, he's claiming this is a problem with clone, when it's really just smashing the stack.
If he had used some finitely large stack and claimed it's the recursive depth at fault, I would have 100% backed him. But not when he submitted broken code and indicted the wrong part of php.<|eor|><|sor|>The interpreter should never segfault. The code he posted makes it do that, so it demonstrates a bug. It doesn't really matter if the code is broken or not. Or for that matter if his theory on what's causing the bug is correct.
It's analogous with a compiler segfaulting on a piece of source code. Yes, that piece of code is probably horribly broken, but the compiler should print an error message and exit gracefully, not segfault.<|eor|><|eols|><|endoftext|> | 20 |
lolphp | audaxxx | c78ibcu | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>I hate debugging a segfaulting PHP, that happens way too often. Stepping in with a debugger and then stepping to the point where it dies..sucks.
It shouldn't segfault.
edit:
WTF, a 2009 bug and still not fixed? They really mean that. Idiots.
<|eor|><|eols|><|endoftext|> | 15 |
lolphp | more_exercise | c78r2ld | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>The submitted code doesn't work, either. As written, it will loop endlessly, generating an infinite series of clones in a Fantasia-like manner<|eor|><|sor|>Wasn't that the point?<|eor|><|sor|>Eh. I remember yelling at a dude (Java) for claiming that HashSet is broken because he could make it break by violating the required consistency between .equals() and .hash(). /r/programming really railed at him for that. Breaking something and then claiming that it's bad is not a very good bug report. Also, he's claiming this is a problem with clone, when it's really just smashing the stack.
If he had used some finitely large stack and claimed it's the recursive depth at fault, I would have 100% backed him. But not when he submitted broken code and indicted the wrong part of php.<|eor|><|sor|>The interpreter should never segfault. The code he posted makes it do that, so it demonstrates a bug. It doesn't really matter if the code is broken or not. Or for that matter if his theory on what's causing the bug is correct.
It's analogous with a compiler segfaulting on a piece of source code. Yes, that piece of code is probably horribly broken, but the compiler should print an error message and exit gracefully, not segfault.<|eor|><|sor|>Oh, it's the interpreter segfaulting? I thought it was the user's code that experienced the segfault. I retract my defense of this error. My bad<|eor|><|eols|><|endoftext|> | 11 |
lolphp | EdiX | c793nf4 | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>I hate debugging a segfaulting PHP, that happens way too often. Stepping in with a debugger and then stepping to the point where it dies..sucks.
It shouldn't segfault.
edit:
WTF, a 2009 bug and still not fixed? They really mean that. Idiots.
<|eor|><|sor|>It's marked as "Not a bug" it will never be fixed they think it's okay that the interpreted code can segfault the interpreter.
To be fair, I see it as a good thing, it will deter people from embedding php in other applications.<|eor|><|eols|><|endoftext|> | 9 |
lolphp | noname-_- | c78q1gc | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>The submitted code doesn't work, either. As written, it will loop endlessly, generating an infinite series of clones in a Fantasia-like manner<|eor|><|sor|>Wasn't that the point?<|eor|><|eols|><|endoftext|> | 6 |
lolphp | more_exercise | c78qobl | <|sols|><|sot|>PHP segfaults - not a bug, totally standard.<|eot|><|sol|>https://bugs.php.net/bug.php?id=49664<|eol|><|sor|>The submitted code doesn't work, either. As written, it will loop endlessly, generating an infinite series of clones in a Fantasia-like manner<|eor|><|sor|>Wasn't that the point?<|eor|><|sor|>Eh. I remember yelling at a dude (Java) for claiming that HashSet is broken because he could make it break by violating the required consistency between .equals() and .hash(). /r/programming really railed at him for that. Breaking something and then claiming that it's bad is not a very good bug report. Also, he's claiming this is a problem with clone, when it's really just smashing the stack.
If he had used some finitely large stack and claimed it's the recursive depth at fault, I would have 100% backed him. But not when he submitted broken code and indicted the wrong part of php.<|eor|><|eols|><|endoftext|> | 5 |
lolphp | AndroTux | lr6s3i | <|sols|><|sot|>It's 2021 and the gethostbyname() DNS functions STILL don't support IPv6<|eot|><|sol|>https://bugs.php.net/bug.php?id=49493<|eol|><|eols|><|endoftext|> | 66 |
lolphp | AyrA_ch | gok9mue | <|sols|><|sot|>It's 2021 and the gethostbyname() DNS functions STILL don't support IPv6<|eot|><|sol|>https://bugs.php.net/bug.php?id=49493<|eol|><|sor|>[The documentation](https://www.php.net/manual/en/function.gethostbyname.php) says that this function returns the IPv4 address of a host.
This is probably a low priority issue because
- Needs documentation rewrite
- Might break tools that depend on the address to be IPv4
- dns_get_record already supports IPv6 and can combine flags so it's trivial to build your own gethostbyname function via `dns_get_record('one.one.one.one',DNS_A|DNS_AAAA)`<|eor|><|eols|><|endoftext|> | 20 |
lolphp | Dagger0 | gokbvil | <|sols|><|sot|>It's 2021 and the gethostbyname() DNS functions STILL don't support IPv6<|eot|><|sol|>https://bugs.php.net/bug.php?id=49493<|eol|><|sor|>This function is forever doomed anyway, because it resolves to a single address only which is simply the wrong model for name resolution. It should be deprecated and removed as soon as possible so that people can migrate to a function that actually works.
At the absolute minimum there should be some very strong language on the documentation page telling people that the function exists only for legacy code, and that it should never be used in new code.<|eor|><|eols|><|endoftext|> | 15 |
lolphp | AyrA_ch | gokaz43 | <|sols|><|sot|>It's 2021 and the gethostbyname() DNS functions STILL don't support IPv6<|eot|><|sol|>https://bugs.php.net/bug.php?id=49493<|eol|><|sor|>[The documentation](https://www.php.net/manual/en/function.gethostbyname.php) says that this function returns the IPv4 address of a host.
This is probably a low priority issue because
- Needs documentation rewrite
- Might break tools that depend on the address to be IPv4
- dns_get_record already supports IPv6 and can combine flags so it's trivial to build your own gethostbyname function via `dns_get_record('one.one.one.one',DNS_A|DNS_AAAA)`<|eor|><|soopr|>If you worry about compatibility, then introduce a new function or add a flag to it. But currently there's no way to resolve a hostname, except for manually using dns\_get\_record, but then you have the problem of resolving CNAME. And it's not like IPv6 is totally new or something, so there would have been plenty of time.
​
edit: s/determine/resolve/<|eoopr|><|sor|>> but then you have the problem of resolving CNAME
No you don't. The command does it for you. `dns_get_record('hcc.ayra.ch',DNS_A|DNS_AAAA)` will give you the final IP address even though it's a CNAME record. You can tell it's a CNAME because the host property of the response will not be the host you look up, but the host that was finally resolved to an IP address.
So this would probably be a suitable application level implementation:
//Simulates gethostbyname() but with IPv6 support
function gethostbyname2($name){
$addr=gethostbyname2l($name);
if(is_array($addr)){
return $addr[0];
}
return $name;
}
//Simulates gethostbynamel() but with IPv6 support
function gethostbyname2l($name){
$ret=[];
foreach(dns_get_record($name,DNS_A|DNS_AAAA) as $e){
$ret[]=$e[$e['type']==='A'?'ip':'ipv6'];
}
return count($ret)>0?$ret:FALSE;
}<|eor|><|eols|><|endoftext|> | 6 |
lolphp | sproingie | gdmtw1 | <|sols|><|sot|>How was your day? I just recently started to hate someone.<|eot|><|sol|>https://i.redd.it/hu046vjqvtw41.png<|eol|><|eols|><|endoftext|> | 69 |
lolphp | dotted | fpih0uk | <|sols|><|sot|>How was your day? I just recently started to hate someone.<|eot|><|sol|>https://i.redd.it/hu046vjqvtw41.png<|eol|><|sor|>/r/programminghorror<|eor|><|eols|><|endoftext|> | 21 |
lolphp | kyou20 | fpl3i0q | <|sols|><|sot|>How was your day? I just recently started to hate someone.<|eot|><|sol|>https://i.redd.it/hu046vjqvtw41.png<|eol|><|soopr|>Yes it is lolbadcode. I just thought I'd share a lol that was php even if php itself isn't the lol. Or something. Hey, things are crickets around here.<|eoopr|><|sor|>Not a PHP OO person: What's wrong with this code?
It looks like a function which takes maybe a username and a dictionary, they instantiate what I presume to be a user object, they set attributes on it from what was passed in, they return the object. What's the problem?<|eor|><|sor|>You name your variables `x` and such, dont you?<|eor|><|sor|>Ok, so the lack of descriptive variable names is the problem? I don't do that and I agree that's not cool but I was expecting something a little more interesting.<|eor|><|sor|>Nothing in this code is descriptive, not the function name, not the arguments, not the implementation, but most importantly, the `U` class, which is an outside module, suggesting that the author of the code has written code like this multiple times many different areas of the codebase, incrementing the surface area of the shitcode<|eor|><|eols|><|endoftext|> | 12 |
lolphp | sproingie | fpi86ns | <|sols|><|sot|>How was your day? I just recently started to hate someone.<|eot|><|sol|>https://i.redd.it/hu046vjqvtw41.png<|eol|><|soopr|>Yes it is lolbadcode. I just thought I'd share a lol that was php even if php itself isn't the lol. Or something. Hey, things are crickets around here.<|eoopr|><|eols|><|endoftext|> | 11 |
lolphp | kyou20 | fpl0yly | <|sols|><|sot|>How was your day? I just recently started to hate someone.<|eot|><|sol|>https://i.redd.it/hu046vjqvtw41.png<|eol|><|soopr|>Yes it is lolbadcode. I just thought I'd share a lol that was php even if php itself isn't the lol. Or something. Hey, things are crickets around here.<|eoopr|><|sor|>Not a PHP OO person: What's wrong with this code?
It looks like a function which takes maybe a username and a dictionary, they instantiate what I presume to be a user object, they set attributes on it from what was passed in, they return the object. What's the problem?<|eor|><|sor|>You name your variables `x` and such, dont you?<|eor|><|eols|><|endoftext|> | 8 |
lolphp | iheartrms | fpl2hof | <|sols|><|sot|>How was your day? I just recently started to hate someone.<|eot|><|sol|>https://i.redd.it/hu046vjqvtw41.png<|eol|><|soopr|>Yes it is lolbadcode. I just thought I'd share a lol that was php even if php itself isn't the lol. Or something. Hey, things are crickets around here.<|eoopr|><|sor|>Not a PHP OO person: What's wrong with this code?
It looks like a function which takes maybe a username and a dictionary, they instantiate what I presume to be a user object, they set attributes on it from what was passed in, they return the object. What's the problem?<|eor|><|sor|>You name your variables `x` and such, dont you?<|eor|><|sor|>Ok, so the lack of descriptive variable names is the problem? I don't do that and I agree that's not cool but I was expecting something a little more interesting.<|eor|><|eols|><|endoftext|> | 6 |
lolphp | MMauro94 | 5vyspq | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|eoss|><|endoftext|> | 68 |
lolphp | frazzlet | de5zxf7 | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>Identical here<|eor|><|eoss|><|endoftext|> | 11 |
lolphp | rabidferret | de6a84a | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>With www just redirects to without for me.<|eor|><|sor|>Same here.<|eor|><|sor|>I'm guessing this is more of a loldns and lolcors than lolphp<|eor|><|eoss|><|endoftext|> | 7 |
lolphp | the_alias_of_andrea | de6n2gz | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>With www just redirects to without for me.<|eor|><|sor|>Same here.<|eor|><|sor|>I'm guessing this is more of a loldns and lolcors than lolphp<|eor|><|sor|>Well, it's php.net that is misconfigured<|eor|><|sor|>How exactly is it "misconfigured"?
It looks to me like they're using the very common strategy of using a non-standard DNS extension to handle the redirect, since using HTTP for that has *major* drawbacks (especially concerning SSL), and then serving the site from www for clients who don't support the extension. The end result is a CORS violation on some fonts, which is really not a big deal.
I dislike PHP as much as the next person, but what they're doing here appears to be an incredibly standard technique in the industry.<|eor|><|sor|>> How exactly is it "misconfigured"?
Not configuring CORS on the machine serving the CSS.<|eor|><|eoss|><|endoftext|> | 7 |
lolphp | rabidferret | de64qon | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>With www just redirects to without for me.<|eor|><|eoss|><|endoftext|> | 6 |
lolphp | BilgeXA | de659px | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>LL<|eor|><|eoss|><|endoftext|> | 5 |
lolphp | smog_alado | de6lj3y | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>Identical here<|eor|><|sor|>They were identical for me until I unblocked Javacript. Now the non-www version has a different font while the www version has the same font as before.<|eor|><|eoss|><|endoftext|> | 5 |
lolphp | the_alias_of_andrea | de6dqap | <|soss|><|sot|>The font on the PHP website changes if the URL contains www<|eot|><|sost|>[without www](http://php.net/manual/en/function.preg-match.php)
[with www](http://www.php.net/manual/en/function.preg-match.php)<|eost|><|sor|>With www just redirects to without for me.<|eor|><|sor|>Same here.<|eor|><|sor|>I'm guessing this is more of a loldns and lolcors than lolphp<|eor|><|sor|>Well, it's php.net that is misconfigured<|eor|><|eoss|><|endoftext|> | 5 |
lolphp | throwaway-o | 1nn3rx | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|eols|><|endoftext|> | 65 |
lolphp | Sarcastinator | cckcsy6 | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>I actually use this feature all the time. My CMS is merely a series of JPGs. This way if a client decides to rip of my hard work, they won't recognise the code, and just think they have a collection of various renaissance artists work on their server.<|eor|><|sor|>It's not a feature. It's a bug you can enable and disable.<|eor|><|eols|><|endoftext|> | 17 |
lolphp | merreborn | cck958m | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>>In short, the attacker uploaded a malicious script disguised as an image; he then requested a page that contained this avatar image; the web server went to retrieve the image, realized it was actually a PHP script and executed his malicious script. This type of attack is possible when PHP's cgi.fix_pathinfo is enabled (i.e. set to 1). It must be disabled (set it to 0) to prevent this type of attack.
WTF... This sounds a lot like what Microsoft used to do in its MSIE browser, and on some other places too: download a text file, and recognize it as html, and thus show it as html. Or change the type of an image. All in order to be "helpful" to stupid webmasters who couldn't get their settings right.
But I don't get it... Isn't it Apache who first gets to decide what to do about content delivery, before PHP even gets to look at it? Aren't image files special-cased anyway?<|eor|><|sor|>They are using nginx and php-fmp(?). Nginx spawns php proccesses to handle reqursts. In the end the fault lies in php.<|eor|><|sor|>[deleted]<|eor|><|sor|>> you change the images name to random glibberish
If PHP executed "avatar.jpg", renaming the file to "a7675f322.dat" doesn't offer that much protection.
> or you save the Images as Blob inside a Database
I just felt a great disturbance in the force, as if a million DBAs and sysadmins cried out...
Static asset requests should be served from flat files on disk. Fetching them using a dynamic language from a database adds orders of magnitude of overhead. Simply printing out "hello world" from a PHP script is at least an order of magnitude slower than serving a flat file, to say nothing of actually connecting to a database and retrieving a BLOB. Your HTTPD can serve static files in well under 10 milliseconds. It's what it was built to do. No PHP code you can write can possibly match the efficiency of nginx serving flat files.
Really, you only have to do one thing: store images (and any other user uploads) separately from your code, and configure your HTTPD to NEVER treat anything in your directory of user uploaded images as executable. In a large enough environment, you should even be keeping user uploaded content on its own set of isolated servers that probably don't have PHP installed at all.<|eor|><|eols|><|endoftext|> | 13 |
lolphp | bart2019 | cck5jb5 | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>>In short, the attacker uploaded a malicious script disguised as an image; he then requested a page that contained this avatar image; the web server went to retrieve the image, realized it was actually a PHP script and executed his malicious script. This type of attack is possible when PHP's cgi.fix_pathinfo is enabled (i.e. set to 1). It must be disabled (set it to 0) to prevent this type of attack.
WTF... This sounds a lot like what Microsoft used to do in its MSIE browser, and on some other places too: download a text file, and recognize it as html, and thus show it as html. Or change the type of an image. All in order to be "helpful" to stupid webmasters who couldn't get their settings right.
But I don't get it... Isn't it Apache who first gets to decide what to do about content delivery, before PHP even gets to look at it? Aren't image files special-cased anyway?<|eor|><|eols|><|endoftext|> | 12 |
lolphp | duskwuff | cck8j1l | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>That's why you *only* accept images after validating them with `getimagesize` or similar.<|eor|><|sor|>That doesn't help; there are tons of ways to embed code in a perfectly valid image (e.g, put it in a JPEG or PNG comment chunk, EXIF headers, disguise it as a palette)<|eor|><|eols|><|endoftext|> | 10 |
lolphp | ajmarks | cck66z7 | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>>In short, the attacker uploaded a malicious script disguised as an image; he then requested a page that contained this avatar image; the web server went to retrieve the image, realized it was actually a PHP script and executed his malicious script. This type of attack is possible when PHP's cgi.fix_pathinfo is enabled (i.e. set to 1). It must be disabled (set it to 0) to prevent this type of attack.
WTF... This sounds a lot like what Microsoft used to do in its MSIE browser, and on some other places too: download a text file, and recognize it as html, and thus show it as html. Or change the type of an image. All in order to be "helpful" to stupid webmasters who couldn't get their settings right.
But I don't get it... Isn't it Apache who first gets to decide what to do about content delivery, before PHP even gets to look at it? Aren't image files special-cased anyway?<|eor|><|sor|>They are using nginx and php-fmp(?). Nginx spawns php proccesses to handle reqursts. In the end the fault lies in php.<|eor|><|sor|>More like php-fml.<|eor|><|eols|><|endoftext|> | 10 |
lolphp | ezzatron | cck7csc | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>>In short, the attacker uploaded a malicious script disguised as an image; he then requested a page that contained this avatar image; the web server went to retrieve the image, realized it was actually a PHP script and executed his malicious script. This type of attack is possible when PHP's cgi.fix_pathinfo is enabled (i.e. set to 1). It must be disabled (set it to 0) to prevent this type of attack.
WTF... This sounds a lot like what Microsoft used to do in its MSIE browser, and on some other places too: download a text file, and recognize it as html, and thus show it as html. Or change the type of an image. All in order to be "helpful" to stupid webmasters who couldn't get their settings right.
But I don't get it... Isn't it Apache who first gets to decide what to do about content delivery, before PHP even gets to look at it? Aren't image files special-cased anyway?<|eor|><|sor|>They are using nginx and php-fmp(?). Nginx spawns php proccesses to handle reqursts. In the end the fault lies in php.<|eor|><|sor|>If it's the same vulnerability that Nginx warn about in their [pitfalls](http://wiki.nginx.org/Pitfalls#Passing_Uncontrolled_Requests_to_PHP) documentation, the exploit basically happens like this:
- The developer sets up Nginx to pass any URI with a path ending in `.php` to the PHP interpreter.
- PHP, by default, allows you to add extra path info after the actual script name, like `http://example.org/path/to/index.php/additional/stuff`, even if `index.php` doesn't end in `.php`.
- The attacker uploads a PHP script with whatever extension they choose. Let's say they upload a file and it ends up as `http://example.org/uploads/exploit.jpg`.
- The attacker then requests `http://example.org/uploads/exploit.jpg/foo.php`, and because the URI ends in `.php`, `exploit.jpg` ends up getting interpreted as PHP code, because it's the first thing up the path that exists.
Pretty stupid huh?<|eor|><|eols|><|endoftext|> | 9 |
lolphp | adambrenecki | cckkqvp | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>I thought it would be a lol bad config about Apache misinterpreting files with double extensions.
This is nonetheless more a lol bad config.<|eor|><|soopr|>It's a lol php default config.<|eoopr|><|sor|>Not "default", since Nginx doesn't default to having PHP enabled at all, so much as "recommended by every PHP tutorial in the world even though it's insecure".
So, in other words, just another Friday in the PHP world.<|eor|><|eols|><|endoftext|> | 9 |
lolphp | youstolemyname | cckau6j | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>>In short, the attacker uploaded a malicious script disguised as an image; he then requested a page that contained this avatar image; the web server went to retrieve the image, realized it was actually a PHP script and executed his malicious script. This type of attack is possible when PHP's cgi.fix_pathinfo is enabled (i.e. set to 1). It must be disabled (set it to 0) to prevent this type of attack.
WTF... This sounds a lot like what Microsoft used to do in its MSIE browser, and on some other places too: download a text file, and recognize it as html, and thus show it as html. Or change the type of an image. All in order to be "helpful" to stupid webmasters who couldn't get their settings right.
But I don't get it... Isn't it Apache who first gets to decide what to do about content delivery, before PHP even gets to look at it? Aren't image files special-cased anyway?<|eor|><|sor|>They are using nginx and php-fmp(?). Nginx spawns php proccesses to handle reqursts. In the end the fault lies in php.<|eor|><|sor|>[deleted]<|eor|><|sor|>> you change the images name to random glibberish
If PHP executed "avatar.jpg", renaming the file to "a7675f322.dat" doesn't offer that much protection.
> or you save the Images as Blob inside a Database
I just felt a great disturbance in the force, as if a million DBAs and sysadmins cried out...
Static asset requests should be served from flat files on disk. Fetching them using a dynamic language from a database adds orders of magnitude of overhead. Simply printing out "hello world" from a PHP script is at least an order of magnitude slower than serving a flat file, to say nothing of actually connecting to a database and retrieving a BLOB. Your HTTPD can serve static files in well under 10 milliseconds. It's what it was built to do. No PHP code you can write can possibly match the efficiency of nginx serving flat files.
Really, you only have to do one thing: store images (and any other user uploads) separately from your code, and configure your HTTPD to NEVER treat anything in your directory of user uploaded images as executable. In a large enough environment, you should even be keeping user uploaded content on its own set of isolated servers that probably don't have PHP installed at all.<|eor|><|sor|>[deleted]<|eor|><|sor|>>But there is no way how the hacker could possible find that image.
Its an avatar. It loads when you look at a post by the user or their profile no matter what the file name is.<|eor|><|eols|><|endoftext|> | 6 |
lolphp | arand | cck60qg | <|sols|><|sot|>PHP helpfully executes code in an image... BitcoinTalk forums uberhacked. LOL PHP.<|eot|><|sol|>http://www.reddit.com/r/Bitcoin/comments/1nmdq4/bitcointalk_hacked/cck0gag<|eol|><|sor|>>In short, the attacker uploaded a malicious script disguised as an image; he then requested a page that contained this avatar image; the web server went to retrieve the image, realized it was actually a PHP script and executed his malicious script. This type of attack is possible when PHP's cgi.fix_pathinfo is enabled (i.e. set to 1). It must be disabled (set it to 0) to prevent this type of attack.
WTF... This sounds a lot like what Microsoft used to do in its MSIE browser, and on some other places too: download a text file, and recognize it as html, and thus show it as html. Or change the type of an image. All in order to be "helpful" to stupid webmasters who couldn't get their settings right.
But I don't get it... Isn't it Apache who first gets to decide what to do about content delivery, before PHP even gets to look at it? Aren't image files special-cased anyway?<|eor|><|sor|>They are using nginx and php-fmp(?). Nginx spawns php proccesses to handle reqursts. In the end the fault lies in php.<|eor|><|eols|><|endoftext|> | 5 |
lolphp | koro666 | 19okxv | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|eols|><|endoftext|> | 68 |
lolphp | avar | c8q1gpj | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|sor|>No it doesn't, it returns NULL, which compares as "0" with the "==" operator.
Here it is returning 0:
$ php -r 'var_dump(strcmp("foo", "foo"));'
int(0)
And here it is returning NULL:
$ php -r 'var_dump(strcmp(array(), array()));'
PHP Warning: strcmp() expects parameter 1 to be string, array given in Command line code on line 1
PHP Stack trace:
PHP 1. {main}() Command line code:0
PHP 2. strcmp() Command line code:1
NULL
$ php -r 'var_dump(0 === NULL);'
bool(false)
$ php -r 'var_dump(0 == NULL);'
bool(true)
<|eor|><|eols|><|endoftext|> | 32 |
lolphp | ladaghini | c8q5sc8 | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|sor|>Ready for today's stupid question?
Why use
strcmp($a,$b) == 0
instead of
$a == $b
?<|eor|><|sor|>Because [`'9223372036854775807' == '9223372036854775808'`](http://www.reddit.com/r/lolphp/comments/s64mm/9223372036854775807_9223372036854775808/).<|eor|><|eols|><|endoftext|> | 19 |
lolphp | PhantomRacer | c8q4lml | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|sor|>Ready for today's stupid question?
Why use
strcmp($a,$b) == 0
instead of
$a == $b
?<|eor|><|sor|>Why use
$a == $b
instead of
$a === $b
?<|eor|><|eols|><|endoftext|> | 12 |
lolphp | jeanlucpikachu | c8q4fo2 | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|sor|>Ready for today's stupid question?
Why use
strcmp($a,$b) == 0
instead of
$a == $b
?<|eor|><|eols|><|endoftext|> | 8 |
lolphp | EdiX | c8vq3hj | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|sor|>The fact that a high level programming language with a string type has "strcmp" as a function is bewildering.<|eor|><|eols|><|endoftext|> | 6 |
lolphp | Andryu67 | c8q0ab2 | <|sols|><|sot|>strcmp() will return 0 on error, can be used to bypass authentication<|eot|><|sol|>http://danuxx.blogspot.ca/2013/03/unauthorized-access-bypassing-php-strcmp.html<|eol|><|sor|>Ooh, I keep forgetting about that whole "do x[] to mess things up" deal... Thanks for the reminder.<|eor|><|eols|><|endoftext|> | 5 |
lolphp | Sebbe | 12ns15 | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|eols|><|endoftext|> | 69 |
lolphp | kingguru | c6wob88 | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>I think the real WTF is that writing:
<script language="php">
</script>
Is valid PHP since the same tags are used for client side script, e.g. Javascript.
Since writing PHP usually ends up with a huge mix of static HTML, server side PHP and client side Javascript, that is bound to confuse. To give credit to PHP though, a comment in the stackoverflow post linked said that usage was "not recommended".<|eor|><|eols|><|endoftext|> | 27 |
lolphp | kingguru | c6wro2y | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>And? That's like laughing at the fact that int main(){} is valid c.<|eor|><|sor|>Not really. On the other hand, if something like this was valid C:
int main() {
/* stuff... */
END
You would have a point. Of course you could do:
#define END }
And make it valid C, but then you should not be allowed to write code ever again. :-)<|eor|><|eols|><|endoftext|> | 14 |
lolphp | Legolas-the-elf | c6wpbgb | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>I think the real WTF is that writing:
<script language="php">
</script>
Is valid PHP since the same tags are used for client side script, e.g. Javascript.
Since writing PHP usually ends up with a huge mix of static HTML, server side PHP and client side Javascript, that is bound to confuse. To give credit to PHP though, a comment in the stackoverflow post linked said that usage was "not recommended".<|eor|><|sor|>I'm sure they thought it was a good idea at the time.<|eor|><|sor|>If I recall correctly, they were copying classic ASP which has something like `<script runat="server">`.
<|eor|><|eols|><|endoftext|> | 11 |
lolphp | kingguru | c6wu9fg | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>And? That's like laughing at the fact that int main(){} is valid c.<|eor|><|sor|>Not really. On the other hand, if something like this was valid C:
int main() {
/* stuff... */
END
You would have a point. Of course you could do:
#define END }
And make it valid C, but then you should not be allowed to write code ever again. :-)<|eor|><|sor|>?> is like the right curly.<|eor|><|sor|>I understand that. The problem is that you have a number of start-tokens matching another number of end-tokens. So:
<?, <?php and <script language="php">
all match:
?> and </script>
And that's just evil. [Here](http://php.net/manual/en/control-structures.elseif.php) is a slightly similar though worse example posted a while ago to this subreddit.<|eor|><|sor|>My point is </script> is an XML problem, not PHP. PHP has it's issues, that is not one of them :)<|eor|><|sor|>What does this have to do with XML? PHP is not parsed by an XML parser.
The problem the original post is about, is that a tag can end with something unrelated to the start tag.
<?
<!-- Stuff here -->
</script>
Is thankfully *not* valid XML.<|eor|><|eols|><|endoftext|> | 10 |
lolphp | kingguru | c6wur24 | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>And? That's like laughing at the fact that int main(){} is valid c.<|eor|><|sor|>Not really. On the other hand, if something like this was valid C:
int main() {
/* stuff... */
END
You would have a point. Of course you could do:
#define END }
And make it valid C, but then you should not be allowed to write code ever again. :-)<|eor|><|sor|>?> is like the right curly.<|eor|><|sor|>I understand that. The problem is that you have a number of start-tokens matching another number of end-tokens. So:
<?, <?php and <script language="php">
all match:
?> and </script>
And that's just evil. [Here](http://php.net/manual/en/control-structures.elseif.php) is a slightly similar though worse example posted a while ago to this subreddit.<|eor|><|sor|>My point is </script> is an XML problem, not PHP. PHP has it's issues, that is not one of them :)<|eor|><|sor|>What does this have to do with XML? PHP is not parsed by an XML parser.
The problem the original post is about, is that a tag can end with something unrelated to the start tag.
<?
<!-- Stuff here -->
</script>
Is thankfully *not* valid XML.<|eor|><|sor|> <?php <-- PHP handles it
</script> <-- PHP handles it as per XML standard notation
?> <-- Handled by the HTML (XML) Parser (the browser).<|eor|><|sor|>> <?php <-- PHP handles it
Correct, that's from where the PHP "parser" tries to parse the PHP script.
> </script> <-- PHP handles it as per XML standard notation
"Handling" an end tag called </foo> without an opening tag called <foo> is *not* XML standard notation.
> ?> <-- Handled by the HTML (XML) Parser (the browser).
Which doesn't really handle it since i'ts not a valid HTML tag.<|eor|><|eols|><|endoftext|> | 10 |
lolphp | midir | c6wsib0 | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>I think the real WTF is that writing:
<script language="php">
</script>
Is valid PHP since the same tags are used for client side script, e.g. Javascript.
Since writing PHP usually ends up with a huge mix of static HTML, server side PHP and client side Javascript, that is bound to confuse. To give credit to PHP though, a comment in the stackoverflow post linked said that usage was "not recommended".<|eor|><|sor|>I'm sure they thought it was a good idea at the time.<|eor|><|sor|>It was to appease WYSIWYG editors that would otherwise complain about the code tags.<|eor|><|eols|><|endoftext|> | 7 |
lolphp | kingguru | c6wsv25 | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>And? That's like laughing at the fact that int main(){} is valid c.<|eor|><|sor|>Not really. On the other hand, if something like this was valid C:
int main() {
/* stuff... */
END
You would have a point. Of course you could do:
#define END }
And make it valid C, but then you should not be allowed to write code ever again. :-)<|eor|><|sor|>?> is like the right curly.<|eor|><|sor|>I understand that. The problem is that you have a number of start-tokens matching another number of end-tokens. So:
<?, <?php and <script language="php">
all match:
?> and </script>
And that's just evil. [Here](http://php.net/manual/en/control-structures.elseif.php) is a slightly similar though worse example posted a while ago to this subreddit.<|eor|><|eols|><|endoftext|> | 7 |
lolphp | kingguru | c6wpqwl | <|sols|><|sot|>So, <?php </script> ?> is perfectly valid PHP<|eot|><|sol|>http://stackoverflow.com/q/13228306/79061<|eol|><|sor|>I think the real WTF is that writing:
<script language="php">
</script>
Is valid PHP since the same tags are used for client side script, e.g. Javascript.
Since writing PHP usually ends up with a huge mix of static HTML, server side PHP and client side Javascript, that is bound to confuse. To give credit to PHP though, a comment in the stackoverflow post linked said that usage was "not recommended".<|eor|><|sor|>I'm sure they thought it was a good idea at the time.<|eor|><|sor|>If I recall correctly, they were copying classic ASP which has something like `<script runat="server">`.
<|eor|><|sor|>Yikes, that sounds even worse.<|eor|><|eols|><|endoftext|> | 5 |
lolphp | tells_the_truth_an | 4v6ibc | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|eols|><|endoftext|> | 63 |
lolphp | trout_fucker | d5vwof8 | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>From the video by Lerdorf:
"You have a request that comes in and says 'Hey what database are you using' 'MySQL' 'ok let's load up the MySQL class that loads up things thing that loads up this thing that initializes the ORM and tells it to use this database'. 2 milliseconds later the next request comes in and says 'Hey what database are we using?' 'Still MySQL'...."
Fucking what?
God I hate this language.
<|eor|><|eols|><|endoftext|> | 34 |
lolphp | killerstorm | d5w0c2r | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>From the video by Lerdorf:
"You have a request that comes in and says 'Hey what database are you using' 'MySQL' 'ok let's load up the MySQL class that loads up things thing that loads up this thing that initializes the ORM and tells it to use this database'. 2 milliseconds later the next request comes in and says 'Hey what database are we using?' 'Still MySQL'...."
Fucking what?
God I hate this language.
<|eor|><|sor|>Rasmus agrees: "It's a scripting language... there are many things which happen at runtime which shouldn't happen at runtime".
<|eor|><|eols|><|endoftext|> | 25 |
lolphp | blueskin | d5wce8b | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>That's like saying plague is better than dysentery.<|eor|><|eols|><|endoftext|> | 22 |
lolphp | maweki | d5wh7ce | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>I love how the bot that tells that the post has been linked from /r/lolphp has the most upvotes in this whole thread.<|eor|><|eols|><|endoftext|> | 18 |
lolphp | dagbrown | d5wn1c8 | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>> Alternatively, go play with Wordpress...probably closer to your skillset.
WordPress is PHP to the power of PHP with a side order of PHP followed by a chaser of some high-tension PHP.<|eor|><|eols|><|endoftext|> | 17 |
lolphp | InconsiderateBastard | d5woey5 | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>From the video by Lerdorf:
"You have a request that comes in and says 'Hey what database are you using' 'MySQL' 'ok let's load up the MySQL class that loads up things thing that loads up this thing that initializes the ORM and tells it to use this database'. 2 milliseconds later the next request comes in and says 'Hey what database are we using?' 'Still MySQL'...."
Fucking what?
God I hate this language.
<|eor|><|sor|>You want database connection pooling? That means PHP is probably too hard for you and you should use something that holds your hand.<|eor|><|eols|><|endoftext|> | 12 |
lolphp | 00Davo | d5wy7h6 | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>They're not wrong. Programming in PHP is a lot harder than programming in Ruby, since it's littered with so many more beartraps and edge cases.<|eor|><|eols|><|endoftext|> | 11 |
lolphp | 00Davo | d5wy68i | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>From the video by Lerdorf:
"You have a request that comes in and says 'Hey what database are you using' 'MySQL' 'ok let's load up the MySQL class that loads up things thing that loads up this thing that initializes the ORM and tells it to use this database'. 2 milliseconds later the next request comes in and says 'Hey what database are we using?' 'Still MySQL'...."
Fucking what?
God I hate this language.
<|eor|><|sor|>This is par for the course when you've got classic-CGI-style invocation of scripting languages, as far as I know. So, for example, perl likely behaves similarly.<|eor|><|sor|>Well, there are ways to write Perl so that your app persists across requests, including [friendly Sinatra-style frameworks](http://mojolicious.org/). Of course, Perl on web is generally in the form of tiny CGI scripts, but there's nothing about the language that demands the CGI process model.
PHP by contrast *needs* separate invocations for separate requests to function. All the popular frameworks are built under the assumption you're using it this way, and it doesn't handle long-running processes well by design. This is the language whose author said "Ill just restart Apache every 10 requests" and it shows.<|eor|><|eols|><|endoftext|> | 7 |
lolphp | miasmic | d5wk4k9 | <|sols|><|sot|>Php is too hard for you. You should try Ruby.<|eot|><|sol|>https://www.reddit.com/r/PHP/comments/4v65ss/your_first_question_should_be_should_we_use_a/d5vtqmo<|eol|><|sor|>What a shitshow<|eor|><|eols|><|endoftext|> | 5 |
lolphp | psychob | 4uuk1o | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|eols|><|endoftext|> | 67 |
lolphp | feketegy | d5sy6dl | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>This works:
$null = null;
var_dump($null['test']);
No errors, but this gives you an undefined index error:
$arr = [];
var_dump($arr['test']);
Also you can set array items on null :))
$null = null;
$null['test'] = 'blah';
var_dump($null['test']);
<|eor|><|eols|><|endoftext|> | 35 |
lolphp | shvelo | d5tib9h | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>This works:
$null = null;
var_dump($null['test']);
No errors, but this gives you an undefined index error:
$arr = [];
var_dump($arr['test']);
Also you can set array items on null :))
$null = null;
$null['test'] = 'blah';
var_dump($null['test']);
<|eor|><|sor|>wat<|eor|><|eols|><|endoftext|> | 14 |
lolphp | the_alias_of_andrea | d5to7qw | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>PHP loves treating different kinds of things as being the same thing. That might be helpful, but it never does it consistently. And so.<|eor|><|eols|><|endoftext|> | 10 |
lolphp | carlos_vini | d5ttrn0 | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>you can also read keys from bool and int, wtf?! https://3v4l.org/WCkTk
Edit: do anobody know why it doesn't throw any error?
$bool = true;
var_dump($bool['test']);
$int = 1;
var_dump($int['test']);
<|eor|><|eols|><|endoftext|> | 8 |
lolphp | bart2019 | d5txj5t | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>I think that PHP treats null (and false) as nice initialisation values for arrays, so you can just set array items in it without warning, and then the array will just spring into life.
In Perl that is called "autovivification", BTW, but there it goes much further: you can increment or append to a non-existing item, push onto it or assign to an item of a non-existing subarray.
The reason is that it makes life much easier as you don't constantly have to worry "does it already exist?"<|eor|><|eols|><|endoftext|> | 7 |
lolphp | kovensky | d5u0ulg | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>I think that PHP treats null (and false) as nice initialisation values for arrays, so you can just set array items in it without warning, and then the array will just spring into life.
In Perl that is called "autovivification", BTW, but there it goes much further: you can increment or append to a non-existing item, push onto it or assign to an item of a non-existing subarray.
The reason is that it makes life much easier as you don't constantly have to worry "does it already exist?"<|eor|><|sor|>(strict) Perl only autovivifies if the top level index access goes into an already existing array; it doesn't convert a scalar (which null is) into an array.
Though it helps that scalars and arrays have technically separate namespaces in Perl. `$ary[0]` accesses `@ary`, not `$ary`.<|eor|><|eols|><|endoftext|> | 6 |
lolphp | feketegy | d5u4zwm | <|sols|><|sot|>TIL: You can read from NULL, just like from regular array.<|eot|><|sol|>https://3v4l.org/C8mMJ<|eol|><|sor|>This works:
$null = null;
var_dump($null['test']);
No errors, but this gives you an undefined index error:
$arr = [];
var_dump($arr['test']);
Also you can set array items on null :))
$null = null;
$null['test'] = 'blah';
var_dump($null['test']);
<|eor|><|sor|>What happens to `$null` itself; is it still `null` or `array`?<|eor|><|sor|>I thought that null is converted to an array, but it doesn't behave like one, because it should throw a undefined index error if it were a true array<|eor|><|eols|><|endoftext|> | 5 |
lolphp | valinor4 | s64mm | <|sols|><|sot|>'9223372036854775807' == '9223372036854775808'<|eot|><|sol|>https://bugs.php.net/bug.php?id=54547<|eol|><|eols|><|endoftext|> | 66 |
lolphp | Rotten194 | c4bkny7 | <|sols|><|sot|>'9223372036854775807' == '9223372036854775808'<|eot|><|sol|>https://bugs.php.net/bug.php?id=54547<|eol|><|sor|>The best part:
>What you see here in this issue is one of the edge cases (how often do you use large numbers in PHP?) where it does not work well.
"Guys it's ok, just don't use large numbers!"<|eor|><|eols|><|endoftext|> | 18 |
lolphp | audaxxx | c4bghwd | <|sols|><|sot|>'9223372036854775807' == '9223372036854775808'<|eot|><|sol|>https://bugs.php.net/bug.php?id=54547<|eol|><|sor|>From that same page...
"@Jeff: You have to understand in PHP 1, 1.0 and "1.0" all are equivalent (in most situations). That's by design."<|eor|><|sor|>And may I add: This design sucks.<|eor|><|eols|><|endoftext|> | 12 |
lolphp | gwynjudd | c4biu79 | <|sols|><|sot|>'9223372036854775807' == '9223372036854775808'<|eot|><|sol|>https://bugs.php.net/bug.php?id=54547<|eol|><|sor|>From that same page...
"@Jeff: You have to understand in PHP 1, 1.0 and "1.0" all are equivalent (in most situations). That's by design."<|eor|><|sor|>And may I add: This design sucks.<|eor|><|sor|>I've gotta say, the behaviour of == in PHP is WORSE than the behaviour of == in javascript. And that's saying something.<|eor|><|eols|><|endoftext|> | 10 |
lolphp | jibbist | c4bfgir | <|sols|><|sot|>'9223372036854775807' == '9223372036854775808'<|eot|><|sol|>https://bugs.php.net/bug.php?id=54547<|eol|><|sor|>From that same page...
"@Jeff: You have to understand in PHP 1, 1.0 and "1.0" all are equivalent (in most situations). That's by design."<|eor|><|eols|><|endoftext|> | 6 |
lolphp | ealf | c4bj83u | <|sols|><|sot|>'9223372036854775807' == '9223372036854775808'<|eot|><|sol|>https://bugs.php.net/bug.php?id=54547<|eol|><|sor|>Best part: [all the people defending this in /r/programming](http://www.reddit.com/r/programming/comments/s6477/9223372036854775807_9223372036854775808/)<|eor|><|eols|><|endoftext|> | 6 |
lolphp | barubary | tqze5w | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|eoss|><|endoftext|> | 64 |
lolphp | barubary | i2kujw8 | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>Cool. Could someone also explain why it's returning 'a', 'outer.B', 'c'?<|eor|><|soopr|>That's because PHP's generators/iterators are modeled on arrays, and PHP's "arrays" are lookup tables. That's why you can say `yield $key => $value` to produce a key/value pair from a generator function.
If your generator function does not specify explicit keys with its `yield` operations, they're autogenerated as sequential integers starting from 0. In the example above, `inner_gen` is equivalent to:
yield 0 => 'a';
yield 1 => 'b';
yield 2 => 'c';
The same applies to the `yield` operations in `outer_gen`, which expand to `yield 0 => 'outer.A'` and `yield 1 => 'outer.B'`, respectively.
`iterator_to_array()` collects all key/value pairs and stores them in an array. As a side effect, later values overwrite earlier values if they have the same keys. In the example above, `0 => 'a'` overwrites `0 => 'outer.A'` and `1 => 'outer.B'` overwrites `1 => 'b'`.
To avoid this, you need to pass a false value as the second argument to `iterator_to_array()`. That makes it throw away all the collected keys and renumber everything from 0, which means you won't lose any values.<|eoopr|><|eoss|><|endoftext|> | 26 |
lolphp | FruitdealerF | i2kjvuq | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>> Wrong! This is PHP. What were you thinking?!
Im thinking that the documentation probably has a flag hidden somewhere that will give me the expected behavior<|eor|><|sor|>yep if you pass true as the 2nd argument to iterator_to_array you get the result you expect. The problem is that secretly this code also returns array keys and that some values will occupy the same key.<|eor|><|eoss|><|endoftext|> | 16 |
lolphp | barubary | i2nhiif | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>"yield from" is lexed as a single token to avoid the BC break of making "from" a keyword. It's not as silly as you think.<|eor|><|soopr|>Contextual keywords are a thing. You wouldn't have to make `from` a reserved word to make this work in a sane way.
Besides, the other multi-part token silliness that is [casts in PHP](/r/lolphp/comments/cwl72a/casts_are_not_sure_whether_theyre_a_single_token/) does not quite work this way. You can't put line breaks in the middle of a cast, but you can put `yield` and `from` on different lines. At minimum it's inconsistent.
Besides, if you're going to abuse the lexer like this, why not go all the way and allow comments as well as spaces, tabs, and newlines in the middle of this "token"? Comments have a regular syntax that is already recognized by the lexer. It might make the internals slightly awkward, but it could have been made seamless at the surface syntax.
Except now it can't be fixed without breaking backward compatibility anymore:
<?php
function from($x) {
return 'surprise!';
}
function generator() {
yield 'a';
yield 'b';
}
function oh_god_what() {
yield
from (generator());
yield // let's do it again
from (generator());
}
var_dump( iterator_to_array(oh_god_what(), false) );
That's the same `yield` statement two times, but one of them has a comment in the middle. And that's enough to make it do completely different things. Bonus lolphp, I guess?<|eoopr|><|eoss|><|endoftext|> | 12 |
lolphp | wchill | i2l2xws | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>Cool. Could someone also explain why it's returning 'a', 'outer.B', 'c'?<|eor|><|soopr|>That's because PHP's generators/iterators are modeled on arrays, and PHP's "arrays" are lookup tables. That's why you can say `yield $key => $value` to produce a key/value pair from a generator function.
If your generator function does not specify explicit keys with its `yield` operations, they're autogenerated as sequential integers starting from 0. In the example above, `inner_gen` is equivalent to:
yield 0 => 'a';
yield 1 => 'b';
yield 2 => 'c';
The same applies to the `yield` operations in `outer_gen`, which expand to `yield 0 => 'outer.A'` and `yield 1 => 'outer.B'`, respectively.
`iterator_to_array()` collects all key/value pairs and stores them in an array. As a side effect, later values overwrite earlier values if they have the same keys. In the example above, `0 => 'a'` overwrites `0 => 'outer.A'` and `1 => 'outer.B'` overwrites `1 => 'b'`.
To avoid this, you need to pass a false value as the second argument to `iterator_to_array()`. That makes it throw away all the collected keys and renumber everything from 0, which means you won't lose any values.<|eoopr|><|sor|>That's kind of disgusting. Is there even a reason why you would want that behavior specifically?<|eor|><|eoss|><|endoftext|> | 12 |
lolphp | IluTov | i2mg44m | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>"yield from" is lexed as a single token to avoid the BC break of making "from" a keyword. It's not as silly as you think.<|eor|><|eoss|><|endoftext|> | 10 |
lolphp | Sarcastinator | i2kskza | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>Cool. Could someone also explain why it's returning 'a', 'outer.B', 'c'?<|eor|><|eoss|><|endoftext|> | 9 |
lolphp | wchill | i2l7s7d | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>Cool. Could someone also explain why it's returning 'a', 'outer.B', 'c'?<|eor|><|soopr|>That's because PHP's generators/iterators are modeled on arrays, and PHP's "arrays" are lookup tables. That's why you can say `yield $key => $value` to produce a key/value pair from a generator function.
If your generator function does not specify explicit keys with its `yield` operations, they're autogenerated as sequential integers starting from 0. In the example above, `inner_gen` is equivalent to:
yield 0 => 'a';
yield 1 => 'b';
yield 2 => 'c';
The same applies to the `yield` operations in `outer_gen`, which expand to `yield 0 => 'outer.A'` and `yield 1 => 'outer.B'`, respectively.
`iterator_to_array()` collects all key/value pairs and stores them in an array. As a side effect, later values overwrite earlier values if they have the same keys. In the example above, `0 => 'a'` overwrites `0 => 'outer.A'` and `1 => 'outer.B'` overwrites `1 => 'b'`.
To avoid this, you need to pass a false value as the second argument to `iterator_to_array()`. That makes it throw away all the collected keys and renumber everything from 0, which means you won't lose any values.<|eoopr|><|sor|>That's kind of disgusting. Is there even a reason why you would want that behavior specifically?<|eor|><|soopr|>I don't think so, but I only learned about generators today, so ... :shrug:<|eoopr|><|sor|>I use them extensively in python and they have the "sane" behavior<|eor|><|eoss|><|endoftext|> | 7 |
lolphp | GoTriggerYourself | i2kdseu | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>> Wrong! This is PHP. What were you thinking?!
Im thinking that the documentation probably has a flag hidden somewhere that will give me the expected behavior<|eor|><|eoss|><|endoftext|> | 6 |
lolphp | Sarcastinator | i2pay7u | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>Cool. Could someone also explain why it's returning 'a', 'outer.B', 'c'?<|eor|><|soopr|>That's because PHP's generators/iterators are modeled on arrays, and PHP's "arrays" are lookup tables. That's why you can say `yield $key => $value` to produce a key/value pair from a generator function.
If your generator function does not specify explicit keys with its `yield` operations, they're autogenerated as sequential integers starting from 0. In the example above, `inner_gen` is equivalent to:
yield 0 => 'a';
yield 1 => 'b';
yield 2 => 'c';
The same applies to the `yield` operations in `outer_gen`, which expand to `yield 0 => 'outer.A'` and `yield 1 => 'outer.B'`, respectively.
`iterator_to_array()` collects all key/value pairs and stores them in an array. As a side effect, later values overwrite earlier values if they have the same keys. In the example above, `0 => 'a'` overwrites `0 => 'outer.A'` and `1 => 'outer.B'` overwrites `1 => 'b'`.
To avoid this, you need to pass a false value as the second argument to `iterator_to_array()`. That makes it throw away all the collected keys and renumber everything from 0, which means you won't lose any values.<|eoopr|><|sor|>Oh fuck that's bad. Iterators behave in a way that you *almost never want* and when you do want that behavior it's trivial to implement anyway.<|eor|><|eoss|><|endoftext|> | 6 |
lolphp | helloworder | i2mkq2z | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>`yield from` is a single keyword, hence the first wtf moment.
the second wtf reason is not generator behaviour per se, but the `iterator_to_array()` function really.
Mostly you want to use generators in `foreach ()` loops, not converting them to arrays (that makes it pointless to use generators in the first place), but yeah, the behaviour is unexpected and silly.<|eor|><|eoss|><|endoftext|> | 5 |
lolphp | barubary | i2o0xhe | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>> Wrong! This is PHP. What were you thinking?!
Im thinking that the documentation probably has a flag hidden somewhere that will give me the expected behavior<|eor|><|sor|>yep if you pass true as the 2nd argument to iterator_to_array you get the result you expect. The problem is that secretly this code also returns array keys and that some values will occupy the same key.<|eor|><|soopr|>Slight correction: The optional 2nd argument of `iterator_to_array` defaults to `true` and you have to pass a false value to get the result you expect.<|eoopr|><|eoss|><|endoftext|> | 5 |
lolphp | barubary | i2l7ixa | <|soss|><|sot|>using generators can yield surprises<|eot|><|sost|>You may have heard that PHP supports [generator functions](https://www.php.net/manual/en/language.generators.syntax.php) that can yield multiple values. (Behind the scenes a generator function returns an iterator object that executes slices of the function body (up to the next `yield`) step by step.)
There is also a `yield from` construct that transparently forwards the results of another generator to the current generator.
Given this information, what would you expect the following code to output?
<?php
function inner_gen() {
yield 'a';
yield 'b';
yield 'c';
}
function outer_gen() {
yield 'outer.A';
yield // pass through values from nested generator
from inner_gen();
yield 'outer.B';
}
var_dump( iterator_to_array(outer_gen()) );
Make your guess now!
---
If your answer is something like
array(5) {
[0]=>
string(7) "outer.A"
[1]=>
string(1) "a"
[2]=>
string(1) "b"
[3]=>
string(1) "c"
[4]=>
string(7) "outer.B"
}
then you might be new here. Wrong! This is PHP. What were you thinking?!
---
If, on the other hand, your answer is
array(3) {
[0]=>
string(1) "a"
[1]=>
string(7) "outer.B"
[2]=>
string(1) "c"
}
then you clearly have some PHP experience. You're close! Unfortunately, it's also wrong.
---
The correct answer is:
Parse error: syntax error, unexpected 'inner_gen' (T_STRING) in wtf.php on line 12
[Live demo.](https://3v4l.org/Eod1j)
This is because of the comment in the code.<|eost|><|sor|>Cool. Could someone also explain why it's returning 'a', 'outer.B', 'c'?<|eor|><|soopr|>That's because PHP's generators/iterators are modeled on arrays, and PHP's "arrays" are lookup tables. That's why you can say `yield $key => $value` to produce a key/value pair from a generator function.
If your generator function does not specify explicit keys with its `yield` operations, they're autogenerated as sequential integers starting from 0. In the example above, `inner_gen` is equivalent to:
yield 0 => 'a';
yield 1 => 'b';
yield 2 => 'c';
The same applies to the `yield` operations in `outer_gen`, which expand to `yield 0 => 'outer.A'` and `yield 1 => 'outer.B'`, respectively.
`iterator_to_array()` collects all key/value pairs and stores them in an array. As a side effect, later values overwrite earlier values if they have the same keys. In the example above, `0 => 'a'` overwrites `0 => 'outer.A'` and `1 => 'outer.B'` overwrites `1 => 'b'`.
To avoid this, you need to pass a false value as the second argument to `iterator_to_array()`. That makes it throw away all the collected keys and renumber everything from 0, which means you won't lose any values.<|eoopr|><|sor|>That's kind of disgusting. Is there even a reason why you would want that behavior specifically?<|eor|><|soopr|>I don't think so, but I only learned about generators today, so ... :shrug:<|eoopr|><|eoss|><|endoftext|> | 5 |
lolphp | barubary | dfj4nm | <|soss|><|sot|>What even is variable interpolation<|eot|><|sost|> <?php
define('foo', 'bar');
$foo = array('yes');
$b = 'no';
echo "r1: $foo[0]\n";
echo "r2: ${foo[0]}\n";
echo "r3: ${ foo[0]}\n";
echo "r4: ${foo[0] }\n";
echo "r5: ${foo [0]}\n";
echo "r6: ${foo[0 ]}\n";
echo "r7: ${ foo [ 0 ] }\n";
Output:
r1: yes
r2: yes
r3: no
r4: yes
r5: no
r6: yes
r7: no
([Live demo](https://3v4l.org/SrLF1))
I don't even know anymore.
PS: I also tried `"$foo[0 ]"`, but that's a syntax error, obviously:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']'
The `unexpected ''` part is slightly worrying: If you don't expect empty substrings to show up everywhere in a string, you may have issues parsing anything at all. \\\_()_/<|eost|><|eoss|><|endoftext|> | 66 |
lolphp | barubary | f33uvob | <|soss|><|sot|>What even is variable interpolation<|eot|><|sost|> <?php
define('foo', 'bar');
$foo = array('yes');
$b = 'no';
echo "r1: $foo[0]\n";
echo "r2: ${foo[0]}\n";
echo "r3: ${ foo[0]}\n";
echo "r4: ${foo[0] }\n";
echo "r5: ${foo [0]}\n";
echo "r6: ${foo[0 ]}\n";
echo "r7: ${ foo [ 0 ] }\n";
Output:
r1: yes
r2: yes
r3: no
r4: yes
r5: no
r6: yes
r7: no
([Live demo](https://3v4l.org/SrLF1))
I don't even know anymore.
PS: I also tried `"$foo[0 ]"`, but that's a syntax error, obviously:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']'
The `unexpected ''` part is slightly worrying: If you don't expect empty substrings to show up everywhere in a string, you may have issues parsing anything at all. \\\_()_/<|eost|><|sor|>Seriously, can someone explain to me where the actual fuck the `no` is coming from?
> `T_ENCAPSED_AND_WHITESPACE`
I guess as a bonus lol, what does *encapsed* mean? Neither [Merriam-Webster](https://www.merriam-webster.com/dictionary/encapsed) nor [Oxford](https://www.lexico.com/en/search?utf8=%E2%9C%93&filter=dictionary&dictionary=en&query=encapsed) seem to know <|eor|><|soopr|>> can someone explain to me where the actual fuck the `no` is coming from?
Nothing easier than that!
${ foo [ 0 ] }
// define('foo', 'bar')
// foo ==> 'bar'
${ 'bar' [ 0 ] }
// 'bar'[0] ==> 'b'
${ 'b' }
// ${'b'} ==> $b
$b
// $b = 'no'
'no'<|eoopr|><|eoss|><|endoftext|> | 30 |
lolphp | barubary | f33w5po | <|soss|><|sot|>What even is variable interpolation<|eot|><|sost|> <?php
define('foo', 'bar');
$foo = array('yes');
$b = 'no';
echo "r1: $foo[0]\n";
echo "r2: ${foo[0]}\n";
echo "r3: ${ foo[0]}\n";
echo "r4: ${foo[0] }\n";
echo "r5: ${foo [0]}\n";
echo "r6: ${foo[0 ]}\n";
echo "r7: ${ foo [ 0 ] }\n";
Output:
r1: yes
r2: yes
r3: no
r4: yes
r5: no
r6: yes
r7: no
([Live demo](https://3v4l.org/SrLF1))
I don't even know anymore.
PS: I also tried `"$foo[0 ]"`, but that's a syntax error, obviously:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']'
The `unexpected ''` part is slightly worrying: If you don't expect empty substrings to show up everywhere in a string, you may have issues parsing anything at all. \\\_()_/<|eost|><|soopr|>Turns out there are actually three different behaviors at work here, not just two:
<?php
define('lol', 'greetings');
$s = ' from PHP!';
$lol = [
'010' => 'Hello',
'8' => 'World',
];
echo "$lol[010] ";
echo "${lol[010] }";
echo "${ lol[010]}\n";
[Output:](https://3v4l.org/KLC1A)
Hello World from PHP!<|eoopr|><|eoss|><|endoftext|> | 22 |
lolphp | Regimardyl | f33svcy | <|soss|><|sot|>What even is variable interpolation<|eot|><|sost|> <?php
define('foo', 'bar');
$foo = array('yes');
$b = 'no';
echo "r1: $foo[0]\n";
echo "r2: ${foo[0]}\n";
echo "r3: ${ foo[0]}\n";
echo "r4: ${foo[0] }\n";
echo "r5: ${foo [0]}\n";
echo "r6: ${foo[0 ]}\n";
echo "r7: ${ foo [ 0 ] }\n";
Output:
r1: yes
r2: yes
r3: no
r4: yes
r5: no
r6: yes
r7: no
([Live demo](https://3v4l.org/SrLF1))
I don't even know anymore.
PS: I also tried `"$foo[0 ]"`, but that's a syntax error, obviously:
Parse error: syntax error, unexpected '' (T_ENCAPSED_AND_WHITESPACE), expecting ']'
The `unexpected ''` part is slightly worrying: If you don't expect empty substrings to show up everywhere in a string, you may have issues parsing anything at all. \\\_()_/<|eost|><|sor|>Seriously, can someone explain to me where the actual fuck the `no` is coming from?
> `T_ENCAPSED_AND_WHITESPACE`
I guess as a bonus lol, what does *encapsed* mean? Neither [Merriam-Webster](https://www.merriam-webster.com/dictionary/encapsed) nor [Oxford](https://www.lexico.com/en/search?utf8=%E2%9C%93&filter=dictionary&dictionary=en&query=encapsed) seem to know <|eor|><|eoss|><|endoftext|> | 14 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.