A note on computer languages

By now you have become familiar with 2 computer languages, HTML and CSS. Like all languages, they have a defined syntax (form) and semantics (meaning). For instance,

Computers don't think

Computers are machines at base, following fixed procedures, so languages for them must be clear and precise. Our sentence above presents a difficulty, as a dictionary reveals that time, flies, and like can all be verbs. Nuances such as irony would be completely lost on a computer, which would believe we were serious in saying, "Time flies like a snail."

All this means that we cannot instruct computers to do things using English, French, or even Chinese. Best we use a language with strict syntax and unambiguous meaning.
If you have already programmed in C, C++ or Java, skip this intro and go right on to PHP howto.

Syntax of C (and PHP, JavaScript)

The next two languages we will study are based upon actions (rather than describing how we want a page to look). By being active, they can change what is presented on a page. Code in these languages consists of a sequence of statements that are executed one after the other.

Statements

Assignment

An assignment statement gives a value to a variable. What is the final value of $mynum?
$mynum = 20 + 1;
$mynum = $mynum + $mynum;

Output (print)

Print statements write some output. In our case, the statement writes to the html page. In PHP the name of the statement is print or echo, in Javascript, it is document.write
print "Hello World";
print $mynum;

Control statements

Decisions can be made based upon conditions. If the condition is true, the next statement is carried out (it may be a {compound statement} ), otherwise control skips over to the following statement.
if ($mynum < 42)
print "there must be some mistake";
while ($mynum > 0)
{ print $mynum;
$mynum = $mynum - 1;
} // should write 42414039...321 and then stop

Note: The parentheses are required, there is no semicolon

A quirk: == vs. =

The designers of C had a problem: If an equal sign means assignment, then (because computers are stupid) it shouldn't also mean is equal to. For that, they decided to double it, like this: ==. A common mistake is to forget this, and strange things happen
$networth = 1234;
if ($networth = 0) // wrong!!
print "You ar busted!"; // does not print (not a surprise), but...
print "You are worth $networth"; // prints: You are worth 0

[PHP only] PHP will recognize variable names inside "double quotes" and replace them with their values. Because they start with $. This is not true with 'single quotes', nor in C or Java.

All the comparisons:

 ==
is equal to
!=
not equal
<
less than
<=
less than or equal
>
greater than
>=
greater than or equal

if (...)  ... else ...

You may ask, "What if I want to do some test, and then do one thing or another? Or more than 2 things. Well, any if statement may optionally have an else clause, to be done only when the condition is false.

if ($temperature > 0)
print "It might rain";
else
print "It might snow";

The second (false) statement can even be another if, giving an "else if", which can be formatted as follows:

if ($number == 12)
print "That's a dozen";
else if ($number == 3 or $number == 7)
print "$number is a lucky number";
else if ($number == 13)
print "$number is an unlucky number";
else // unconditional else is optional
print "$number is boring";
What is TRUTH? [Bertrand Russell wrote a book about it.] In C, 0 (zero) is FALSE, all other numbers are TRUE. Comparisons give the value 0 or 1 (one, TRUE).
In PHP, every value is TRUE except: 0, 0.0, "0", "" (the empty string), undefined, FALSE, and NULL

Confused? So may be the poor stupid computer.

You can expect to get error messages if your syntax is not exact. For example, if you forget a semicolon, the computer gets lost and produces a cryptic error message on the next line. Mismatched brackets or quotes cause other muddles. Thankfully, modern text editors do syntax highlighting, and often show the matching bracket, pay attention to this.

There are lots of tutorial introductions on the web. For my part, go on to read about arrays, or directly to the howto.


Lin Jensen