As part of learning PHP, I'm trying to formalize its semantics. A great way to simplify this task is to understand as much of its syntax as possible as "syntactic sugar" for more fundamental expressions. That way, there are less syntactic forms for me to define the semantics for.
PHP seems to have a large amount of syntax that can be de-sugared. Some examples:
if/endif
and other "alternative syntaxes" desugar toif { ... }
, etc.for (expr1; expr2; expr3) { statements; }
desugars toexpr1; while (expr2) { statements; expr3; }
.I think the following is true:
$foo
just means${'foo'}
. That is, referencing a variable by name is just an instance of the more general variable lookup by evaluating an expression to a string.I think this:
<p>foo</p> <?php echo 5; ?> <p>bar</p>
desugars to this:
echo "<p>foo</p>\n"; echo 5; echo "<p>bar</p>\n";
Or in general,
?>blah<?php
just meansecho 'blah';
.
There are certainly many, many other cases like this. I'd like to come up with a list that is comprehensive and confirmed to be accurate. (And any of my examples could be inaccurate, so please correct me!)