にしても、なにをどうやれば 4 という結果になるのか不思議だったのですが
(コードは追いかけなかった)、なるほどこういう動作だったのね。
+ 以降を二回スキャンしてたんだ。
PHP adds hexadecimal numbers incorrectly : programming
For the curious but lazy: it's the lexer. Dealing with "0x00+0x02" it skipped the 0x00,
strtol'ed "+0x02" expecting the rest of the LHS and then carried on correctly.
I've nothing good to say about PHP, so I won't say any more.
In case anyone else is going crazy looking at the code linked to from the bug trying
to spot the bug: the link in the bug is to the trunk version of the code, which is now
fixed. Here's the version before the fix, in which you can also play "spot the bug"
yourself (though the parent's comment pretty much gives it away).
http://lxr.php.net/opengrok/xref/PHP_TRUNK/Zend/zend_language_scanner.l?r=322378#1515
1. Parser encounters 0x00+0x02
2. ...attempt to lex 0x00:
2a. ...skip 0x
2b. ...skip leading 0 until it encounters + # len is 0 at this point
2c. ...strtol("+0x02...") => 2 # len is 0 at this point
3. ...attempt to parse + => Add operator
4. ...attempt to parse/lex 0x02 => 2
4a. ...strtol("02") => 2
So 2 + 2 = 4
bug was introduced in 2006 in php 5.2 era
http://svn.php.net/viewvc?view=revision&revision=225847
Here's the commit, yet another sacrifice to a tin god.
is_numeric_string() optimization
Original Patch by Matt Wilmas
https://github.com/php/php-src/commit/0accb4b0094b8fdda905e0a374843f0c775f4537
I'm a webdev thinking of switching away from PHP. Got any recommendations? If so, why do you recommend it/them?
You might consider switching to Python.
I'll also recommend python.
Unlike some alternatives it'll be easy to pick up after php. You'll be productive after few hours.
It's very, very widely used and not only for web development. It has bindings for every
piece of software and libraries for every purpose imaginable. Again, this could be a
problem with some of the alternatives.
It's just a damn good language. I'm really trying hard to find any nasty surprises or
limitations or just anything to dislike and I can think of 3 or 4 things. That's much,
much less than for any other language I ever used extensively. C++ comes close, except
it took me 5+ years to understand why the ways some things work make sense. This means
that after you understand the basics, you can be really productive and your code won't
be a ticking time bomb.
A different python user, but here's my list:
Python 3/Python 2 incompatibilities. Python 3 is the new version with new features, but
it's not backwards compatible with Python 2,and code needs some clunky constructs to work
on both. A lot of libraries are still Python 2, so it's impossible to just go python 3.
Some of the older parts of the standard lib are not as well done as newer parts. unittest
violates pep8, the coding standard for pretty much every other python program ever. So
while everything else in the stdlib uses underscored_names, unittest uses camelCase.
Urllib/urllib2 are a disorganised mess in python 2,and a shuffled around unorganised
mess in python 3. If you need these, look at the requests library to see if you can
possibly use that.
No anonymous functions. If you've done much javascript or any functional language,
you'll know these can be useful. Now, it's easy to declare functions in python, and you
can just declare it in the line before using it, but it's still a workaround, much
like functors are a workaround for the lack of functions outside classes in Java.
No anonymous functions.
There are lambdas, what more could you want?
More than one line.
I agree; however, I just encountered a nasty problem. Python sucks as concurrency within
a single script. You can do some things but due to how the Python interpreter works, it is
very hard to see true concurrency gains. I ended up having to restructure to allow a
parameter that specifies which portion of the data set to run over and then run multiple
Python instances. With the way software is heading, this could grow into a larger problem.
A serious question: how can you love a language like that? Perhaps I am a bit of an old-school
guy, but my major reason for liking languages and techniques comes from their elegance. A
language that is an ill-thought-through bag-of-tricks, without any attempt at coherence, like
PHP feels like an abomination to me.
I'm struggling to understand your perspective.
It's easy to fall in love with the first girl that sucks your dick. Lots of PHP programmers
have never been exposed to anything better.
Not entirely true.. its more about the falling in love with the first girl that is popular
with everyone and you feel comfortable with... Some of the reasons why people (usually
newbies) end up using php are:
1) Popularity: It is one of the most widely used programming language. Many multi-million
$$$ apps were designed on it. Million's of projects available on the internet to study
and review from.
2) Learning Curve: It is really easy to learn, flexible and you can hack together complex
tasks in minutes rather than hours.
3) Support: Have you seen the php docs? One of the best laid out documentation I have ever
come across, not to mention the fact that the added user comments are majorly helpful.
If I ever get stuck, I could do google search for that specific task in php and surely
someone has written a tutorial on it or inquired about it on stackflow.
4) Functions and Extension: functions like strtotime? that shit is like magic, throw in any
date or time in any format and it parses it with an accuracy of about 99%! Tons and tons
of extensions.
5) Readily available and comes installed with most hostings.
So yes, it is pretty much like the new VB6 of the internet.
I don't think PHP is a terrible language, and it's ridiculously vilified. I think a
large part of that comes from:
A. History - PHP was historically a somewhat crippled language due to a lot of missing
programming constructs & conventions and a lack of real OOP. I think a lot of
people dismissed it out of hand for these reasons. A lot (but not all) of this has
been addressed in recent years.
B. Ubiquity - as choice #1 for people learning how to script beyond client-side Javascript,
it was met with derision by people with "real" programming skills. The logic
(presumably) being "since so many people ARE doing it, anyone CAN do it, therefore
it's a weak language."
C. Language design - still probably valid, but you can work around this if it bothers you
that much. Yeah, some functions are verb_action, action_verb, verbaction, action2verb,
whatever. And that's sloppy and stupid, particularly if you're used to a very short
selection of pre-defined functions. But it isn't some insurmountable issue.
Would I recommend it as a first language? No. Would I recommend it for an application?
Maybe ... the resource pool is enough to offset any functional or perceived deficiencies
in the language.
I have also seen more than my share of ugly Python code (my language of choice) to know
that this isn't necessarily an issue with the language.
History - PHP was historically a somewhat crippled language due to a lot of missing
programming constructs & conventions and a lack of real OOP.
The problem here is that the language has been evolved by people who were OK with its huge
flaws to begin with.
The logic (presumably) being "since so many people ARE doing it, anyone CAN do it,
therefore it's a weak language."
The logic is that anybody who is OK with its gaping flaws is the sort of person who should
not be allowed near important things.
The root of the problem is that PHP from the start was a subset of the worst parts of Perl.
When PHP was started, Perl already had 'use strict', good namespace support, lexical
variables, and a workable object system. But Perl had a lot of historical cruft, too.
Formats absolutely don't belong in the core language, for instance.
The subset of that language known as Modern Perl recognizes the flaws and tries to deal
with them; escew the bad and promote the good. A 'use strict' is mandatory, lexical scoped
variables should always be used unless you have a very good reason. Formats are forgotten
as the weird, archaic tempting system that it is.
In contrast, PHP took basically the opposite subset of the Perl language. Programmers who
are OK with this aren't OK with me.
PHP: The Visual Basic of this century