Don't touch PHP again
Note to self, in case of thinking about coding in PHP you need to read this:
Extract from PHP: a fractal of bad design
Operators
==
converts to numbers when possible(123 == "123foo")
, which means it converts to floats when possible. So large hex strings (like, say, password hashes) may occasionally compareTRUE
when they’re not. So"6" == " 6"
,"4.2" == "4.20"
, and"133" == "0133"
. But note that133 != 0133
, because0133
is octal. But"0x10" == "16"
and"1e3" == "1000"
!===
compares values and type… except with objects, where===
is only true if both operands are actually the same object! For objects,==
compares both value (of every attribute) and type, which is what===
does for every other type.It’s not even consistent:
NULL < -1
, andNULL == 0
.<
and>
operators try to sort arrays, two different ways: first by length, then by elements. If they have the same number of elements but different sets of keys, though, they are uncomparable. Objects compare as greater than anything else… except other objects, which they are neither less than nor greater than.For a more type-safe
==
, we have===
. For a more type-safe<
, we have… nothing."123" < "0124"
, always, no matter what you do. Casting doesn’t help, either.
Variables
There is no way to declare a variable without a value
Global variables need a
global
declaration before they can be used.There are no references. What PHP calls references are really aliases
Once a variable is made a reference (which can happen anywhere), it’s stuck as a reference. It cannot bw overwritten by, say a string.
Constants are defined by a function call taking a string;
Variable names are case-sensitive. Function and class names are not.
Appending to an array is done with
$foo[] = $bar
Last updated