PHP statements end with a
semicolon.
PHP supports comments the
same as C and C++ using
the methods shown below:
<?php
// This is a comment
echo "This is a test of
PHP!";
?>
<?php
/* Line 1 of this comment
Line 2 of this comment */
echo "This is a test of
PHP!";
?>
PHP's basic syntax is
familiar.
<?php
echo "Hello,
World!";
?>
produces
Hello, World!
Variables are marked with a
preceding $. You could write
"Hello, World!" like this:
<?php
$message
= "Hello, World!";
echo $message;
?>
String concatenation is done
with . (a period); other
arithmetic operators are what
you would expect:
<?php
$greeting
= "Hello ";
$num = 3
+ 2;
$num++;
echo "$greeting
$num people!";
?>
produces
Hello 6 people!
PHP has the full complement of
operators, and they work just as
you'd expect them to--especially
if you come from a background of
C or C++. A good rule of thumb
for PHP: "When in doubt, try it;
it will probably work."
Just as in Perl, a string
surrounded with double quotes
causes variables inside it to be
interpolated, but a string
surrounded with single quotes
does not. So,
<?php
$name = 'Susannah';
$greeting_1
= "Hello, $name!";
$greeting_2
= 'Hello, $name!';
echo "$greeting_1\n";
echo "$greeting_2\n";
?>
produces
Hello, Susannah!
Hello, $name!
Note that the \n in
the string turns into a new
line, just as in Perl or in C.
This only works in double-quoted
strings, however.