You could invent a simple syntax for the English language at first. (But don't call it syntax just yet)
It's all arbitrary
For instance
- Make up a rule what it means for something to be a word
- define a verb to be one of: like, want
- define what a subject is: I, He/She
- now define what a sentence is: A subject, an optional "don't", a verb, a word, a dot.
Now have them build sentences that conform to these rules.
Important things to point out:
but only the rules, really
Now, explain that someone made up rules for what it means for a program to be valid. And unless they know what these rules are and follow them to the letter, they will not have a valid program.
Make clear that programs are not magical, they are governed by very precise rules that can be learned, understood and combined.
It's like Lego
Show them similarity in terms of "form" and how complex things are made up of smaller elements:
$number = 2;
$sum = 3 + $number;
$product_of_sums = $sum * (4 + $number);
Explain, that the programming language doesn't "know" how to assign a sum to a variable, it doesn't "know" how to multiply a variable by the sum of a number and variable.
Explain, that you can assign a value to a variable, how you can combine two values into a single value (binary operators), which is then assigned to a variable. Draw boxes (colored) around these results: the expressions.
$number = (2);
$sum = ((3) + ($number));
I have found that many beginners seem to struggle with these fundamental similarities, how the form of all of these things is the same essentially.
Probably, I would not try to explain the semicolon. If someone asks, just say: "Remember how you couldn't say 'He likes bananas'?" and explain that someone made the rules this way.
It's turtles all the way down here, too
Stress that the concept of values permeates the entire language: while($variable == true) isn't a special construct. Make them understand that while(<value/expression>) is special, but $variable == True is exactly the same as the values thing above: An operator combining two values into one value.
It is in no way connected to while, the program doesn't "know" the thing inside parentheses represents a condition. That is just our interpretation. For all it is concerned, it wants a value. (same story for if)
Really do stress that == is not connected to if and while, show them that they can do things like: $is_four = (4 == $maybe_four); to strengthen the point.
From this, you show that it is equivalent to writing while($variable) or $while((($variable == true) == false) == false). It may look complicated, but show them that there's nothing magical about it by expanding the expression step by step from within, once for $variable = true and once for false.
I hope I could give a good idea how to approach this. Some things you can expand on:
- Why it's necessary to have keywords
- Why it's necessary to have
{} to group things together
- What makes an identifier
- What constitutes as a value
- literals
- variables
- function calls
- the for-loop is just a specialization of the while loop, how it captures a common pattern. Show that you don't need the for but that it's very handy to have.
Why PHP is a suboptimal teaching vessel
Unfortunately, PHP's expression model doesn't compose too well.
The entire notion that expressions can be combined freely doesn't hold up in PHP:
func()[0]; // Syntax error
Doesn't work before PHP 5.4. One might think that you could trick PHP by doing this:
(func())[0];
but no, this doesn't work either, nor does it work in PHP 5.4.
It seems like PHP doesn't think in terms of expressions and applying operators to them, instead it some hardcoded combinations of what is possible and what isn't.
Another example: You can call functions by assigning the name to a variable, and calling it:
$var = "func";
$var();
But trying to invoke the function from a different string expression fails:
"func"(); // Syntax error
("other_" . $var)(); // Syntax error
($var)(); // Syntax error
Even worse, in PHP 5.3 functions became 1.61-class functions as opposed to first-class functions with the introduction of closures:
<?php
function test(){
return function() {
echo "test";
};
}
test()(); // Syntax error
$f = test();
$f(); // Works
To me, such inconsistencies make the language feel very "hackish", and I believe if used for teaching, you'll not want to have to answer questions such as explaining why the above doesn't work, if a student really is trying to combine expressions arbitrarily that "should make sense" - at least in the beautiful model of expressions and operators.
Of course, you can say "Well,()/[]are not operators, they are syntactic elements, which is why their usage is constrained arbitrarily by the language designers. Apparently, their rules say that the former can only follow a variable or an identifier, whereas the second can only follow a function call or a variable."
However, the language allows to overload these "operators" for user-defined classes...