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,
- in English, a sentence such as: "Time flies like an arrow."
starts with a capital letter, and ends with a period. A sentence
must contain a verb, and often a sebject, object and various
modifiers. That is syntax. The semantics, once a human decides
on the verb, is given by the particular words. One needs some
concept of what an arrow is, for instance.
- in HTML, tags, such as <p> ... </p> have the
syntax of opening and closing brackets, and the requirement that
the start and ending pairs be nested. The semantics of this one
is that the text between (where I have put ...) should be
considered a paragraph, and so rendered on a screen or paper.
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.
- A simple statement
ends with a semicolon (;)
- Statements may be grouped into a "compound statement" by using braces { ... }
- Comments are meant
for the human reader of a program, and are ignored by the
computer.
- You can write a comment by using 2 slashes (//) the rest of
the line is considered comments.
- A possibly multi-line comment starts with /* and ends with
*/
- Variables are names
of "memory cells" that can hold values, and the values can be
assigned and re-assigned, hence, they "vary" as the program
unfolds. Variable names start with a letter, and may then
contain letters, numbers, and the underscore character (_). In
PHP, variables are further identified by a preceding dollar sign
($)
- Values can either be
numbers, or strings of
characters, a string is delimited by quotes:
"this is a string"
'another string' 'you can't do this(why?)'
- Case sensitive: $mynum
and
$MyNum are different
variables, also IF
means nothing
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.
- An if statement
causes the following statement to be executed zero or one time.
- A while statement
"loops", it comes back to the condition, until it becomes false.
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