3 ms·
So, you are technically correct when you say PHP accepts a trailing newline, but it doesn't mean it refutes the comment and the context we are discussing. This
by jasonlotito 3y ago
So, you are technically correct when you say PHP accepts a trailing newline, but it doesn't mean it refutes the comment and the context we are discussing.
This is easily demonstrated with an example.
<?php
var_dump(preg_match("/^[a-z0-9 ]+\$/", "hello\n"));
int(1)
versus
<?php
var_dump(preg_match("/^[a-z0-9 ]+\$/", "hello\nworld"));
int(0)
versus
<?php
var_dump(preg_match("/^[a-z0-9 ]+\$/", "hello\n\n"));
int(0)
Which all makes sense, as by default PHP doesn't operate in multiline mode. So, by default, PHP is not going to fall prey to the same problem being discussed here. In addition, the first \n would be apart of the first line it's on, so including it as a part of the string would make sense. More to the point, in this context, $ does mean end of the string in PHP. You can prove otherwise by getting the 2nd and 3rd example above to output a 1 instead of a 0 without going into multiline mode.
- dwheeler 3y agoI think we're using different definitions for "end of string". In PHP, the following is considered true: > var_dump(preg_match("/^[a-z0-9 ]+\$/", "hello\n")); That is clear proof that "$" does NOT just match the end of the string; it also accepts an extra newline at the end of the string. In PHP you need to use \z if you want to match the end of the string, or use the "D" flag when using "$". That definition of "$" is often reasonable when you read files a line-at-a-time from a file, which is why Perl changed its definition. However, PHP is often used for server-side web applications. In this case, you are often NOT reading a line-at-a-time from a file. In such cases, allowing an extra newline at the end could be disastrous. The MediaWiki code (written in PHP) deals with this by adding the "D" flag when it uses "$", but I'm not sure it always uses it, and I doubt all PHP programs use this flag when they should.