-3

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 to if { ... }, etc.

  • for (expr1; expr2; expr3) { statements; } desugars to expr1; 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 means echo '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!)

jameshfisher
  • 34,029
  • 31
  • 121
  • 167
  • I usually write `= 5 ?>` as opposed to `` – saada Dec 16 '13 at 02:47
  • 3
    You really feel that `for` is syntactic sugar? :) – Ja͢ck Dec 16 '13 at 02:47
  • 1
    Completely incorrect on your *blah* example. `?>blah – Phil Dec 16 '13 at 02:49
  • @Jack well, it might not be -- are there cases where they are different? – jameshfisher Dec 16 '13 at 02:51
  • @Phil is that not what `echo` does, too? – jameshfisher Dec 16 '13 at 02:52
  • @jameshfisher Not at all. `echo` is a PHP language construct, thus requiring a PHP context to execute within. – Phil Dec 16 '13 at 02:55
  • @Phil could you give me an example where `?>blah – jameshfisher Dec 16 '13 at 02:58
  • @jameshfisher the end result may be the same but the process of getting there is completely different. The former doesn't involve the pre-processor at all. – Phil Dec 16 '13 at 03:00
  • @Phil ah, I suppose you're talking about observable difference in terms of runtime, memory usage, etc. I'm conveniently ignoring such matters as I'm only concerned with semantics, in the sense that `if 1 = 1 then S1 else S2` is semantically identical to `S1`, though they might be compiled/interpreted differently. – jameshfisher Dec 16 '13 at 03:06
  • Also, if anyone could tell me why this is being down-voted, I'd appreciate it. – jameshfisher Dec 16 '13 at 03:06
  • 1
    @jameshfisher See: http://stackoverflow.com/help/dont-ask for the most likely reason you are being downvoted. – Tomdarkness Dec 16 '13 at 03:11
  • @Tomdarkness thanks; what guidelines there does this question break? – jameshfisher Dec 16 '13 at 03:20
  • 1
    @jameshfisher I'm afraid this question is too broad; a good answer would have to desugar _every_ PHP construct, nor did you indicate what statements you consider sugar-free. – John Dvorak Dec 16 '13 at 03:24
  • @jameshfisher Quoted from the link: "every answer is equally valid", "your answer is provided along with the question, and you expect more answers", and "there is no actual problem to be solved". Even if someone posted just one answer, it would be too long to list all the potential syntatical sugar available and the desugared version. – Tomdarkness Dec 16 '13 at 03:28
  • @JanDvorak, Tomdarkness, I see, thanks; I've edited it to ask for a list. – jameshfisher Dec 16 '13 at 03:31
  • 1
    @jameshfisher still too broad; a list answer is too long by definition. Moreover, you can't ever get a _complete_ list, because each PHP version brings more and more sugar. – John Dvorak Dec 16 '13 at 03:32
  • @JanDvorak "nor did you indicate what statements you consider sugar-free" I didn't realize I'd have to explain that. I'm just using "sugar" in the familiar sense: a syntactic form which can be compiled to another form which is semantically identical but more general. – jameshfisher Dec 16 '13 at 03:34
  • @JanDvorak then why the success of [this question](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php)? – jameshfisher Dec 16 '13 at 03:43
  • @jameshfisher the question states its purpose; "The main idea is to have links to existing questions on Stack Overflow, so it's easier for us to reference them, not to copy over content from the PHP Manual" - it _is_ very broad and it wouldn't have started off without the deliberation of the answerers, but since it's extremely useful, it's better left open. Your question has no such intention for general usefulness, nor there is any need to build a list like this. I don't think the other question is a great example of what to ask. Also, I don't think you are going to build an index of answers. – John Dvorak Dec 16 '13 at 03:53
  • It's also interesting that you see "for" as PHP-specific sugar to "while" even if that applies to many languages and is so common, hardly anyone would probably explicitly call it "sugar" – shaedrich Aug 23 '23 at 09:09

1 Answers1

8

That way, there are less syntactic forms for me to define the semantics for.

I think you're approaching this from the wrong angle. Let's start with an example:

$words = array('hello', 'wonderful', 'world');
foreach ($words as $word) {
    echo $word;
}

Nice, but that's too high level; let's replace foreach with a simpler for because our array is not associative:

for ($i = 0; $i < count($words); ++$i) {
    echo $words[$i];
}

Now drop the for:

$i = 0;
while ($i < count($words) {
    echo $words[$i];
    ++$i;
}

That while is kind of annoying:

function doPrint($words, $i = 0)
{
    if ($i < count($words)) {
        echo $words[$i];
    }
    doPrint($words, $i + 1);
}

doPrint($words);

Do we really have to use a function?

echo $words[0];
echo $words[1];
echo $words[2];

What's up with those variables?

echo "hellowonderfulworld";

Bottom line

Don't try to break down a language down to the basics; expressiveness in a language is there for a reason, to be used in the right situation. Learn the basics first and then learn as many "sugars" as you can.

In order to be fluent in a language, you should at least have tried a large majority of what the language has to offer. Practice, practice, practice!

Ja͢ck
  • 170,779
  • 38
  • 263
  • 309
  • 1
    Does `foreach` really desugar that way? I think it iterates over key-value maps, which are more general than your `for` loop. Also, perhaps you've misunderstood me. 'Learn the basics first and then learn as many "sugars" as you can.' That's precisely what I'm aiming at: figuring out the semantics for "the basics", then learning the more trivial syntactic sugar. However, I need to identify what "the basics" are first. :-) – jameshfisher Dec 16 '13 at 03:23
  • @jameshfisher It can be more generically decomposed with `reset()` and `each()`; also, it's just an example :) – Ja͢ck Dec 16 '13 at 03:26
  • @jameshfisher your question is asking for the sugar; the suggestion is that you should start by looking at the non-sugar – John Dvorak Dec 16 '13 at 03:26
  • 1
    @Jack, ah, that's useful, thanks! I didn't know the array cursor was accessible via the API. – jameshfisher Dec 16 '13 at 03:40