Like so many things, the answer is of the form "Because X and Y are different things with different purposes".
In this case, the designers of both languages assumed that the users of their languages had a very different set of goals. For C, the primary use case was "portable assembly language", which really means getting down into the nitty-gritty of how the computer is actually managing its resources. There's no practical way to avoid memory address manipulation at the lowest level of abstraction, and so C has robust support for it.
PHP was intended to make dynamic web page content as flexible and painless as possible. This is quite a few steps removed from the super-low level of the C world; managing memory is, for the purposes PHP is intended to address, much too low level to be of much interest. Any kind of automatic memory management would be fine, so long as it is robust and reliable, and stays out of the way. That's exactly the situation you see in PHP; objects are allocated automatically, when needed, and garbage collected when they are no longer useable, and it all happens without the intervention of the PHP programmer.
It's perhaps of some interest to observe that PHP is itself written in C! the nitty gritty of memory management is written in C, which provides the tools needed to do that kind of thing, so that the language created, doesn't require the programmer to do much themselves.
There's not a lot of code here to work with and it's unlikely everyone has a copy of this book, so this is a guess! It would help if you would post more code.
Using your PLEASE HELP
& LEA
example, I'd wager the flags
array is a lookup table indexed by the ASCII value of a character, and at the end would look like:
flags['A'] -> true
flags['B'] -> false
...
flags['E'] -> true
...
flags['L'] -> true
... (everything else would be false)
Then when doing the removed copy the routine would see that
flags['P'] -> false, so copy it
flags['L'] -> true, so don't copy
flags['E'] -> true, so don't copy
etc
'P' is the ASCII value of character P.
Best Answer
PHP was designed (or, rather, evolved) for use with web requests, where you're frequently dealing with string input (URL parameters, or POST requests from a form in a browser). As such, it will automatically cast strings to other types.
A simple example of this is that
'1' + '2'
gives3
, not an error, or'12
', or some other interpretation. By the same logic, the string'0'
can be used as a numeric0
.Meanwhile, like many languages, PHP treats certain values as "falsy" when cast to boolean - ones that are intuitively "empty", as you say. That includes numeric
0
, as well as the empty string''
and the empty array[]
. In anif
statement, the expression is explicitly cast to boolean, soif ( 0 )
is the same asif ( false )
.Putting these two things together, you get a conundrum: on the one hand, as you say
'0'
is a non-empty string; on the other hand, we have said that it can be used as a numeric0
, which is "empty". PHP opts to treat the "zero-ness" as more important than the "stringiness", so that'0'
is considered "falsy".In short:
'0' == 0 == false
; or(bool)'0' === (bool)(int)'0'