PHP syntax

Use this information to become familiar with PHP syntax.

By David Sklar and Adam Trachtenberg

PHP's basic syntax is familiar.

echo "Hello, World!";


Hello, World!

Variables are marked with a preceding $. You could write "Hello, World!" like this:

$message = "Hello, World!";
echo $message;

String concatenation is done with . (a period); other arithmetic operators are what you would expect:

$greeting = "Hello ";
$num = 3 + 2;
echo "$greeting $num people!";


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,

$name = 'Susannah';
$greeting_1 = "Hello, $name!";
$greeting_2 = 'Hello, $name!';
echo "$greeting_1\n";
echo "$greeting_2\n";


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.


PHP makes environment variables available to you as regular variables. This includes the environment variables that are set by the server for a CGI program (even if you're running PHP as a module). For this reason, if the page "" contains the code

echo "[$REQUEST_URI]";

it prints out [/farm/cattle/cow-cow-cow.html]


You set off array indices (regular or associative) with square brackets ([ and ]):

$fruit[0] = 'banana';
$fruit[1] = 'papaya';
$favorites['animal'] = 'turtle';
$favorites['monster'] = 'cookie';

If you assign something to an array but leave the index blank, PHP assigns the object onto the end of the array. The statements about $fruit, above, produce the same result as:

$fruit[] = 'banana';
$fruit[] = 'papaya';

You can have multidimensional arrays, too:

$people['David']['shirt'] = 'blue';
$people['David']['car'] = 'minivan';
$people['Adam']['shirt'] = 'white';
$people['Adam']['car'] = 'sedan';

A shortcut for creating arrays is the


$fruit = array('banana','papaya');
$favorites = array('animal' => 'turtle',
                    'monster' => 'cookie);


$people = array ('David' => array('shirt' => 'blue',
                                    'car' => 'minivan'),
                 'Adam' => array('shirt' => 'white',
                                    'car' => 'sedan'));

The built-in function count() tells you how many elements are in an array:

$fruit = array('banana','papaya');
print count($fruit);



Control Structures

You can use looping structures such as for and while:

for ($i = 4; $i < 8; $i++) {
   print "I have eaten $i bagels today.\n"; }


I have eaten 4 bagels today.
I have eaten 5 bagels today.
I have eaten 6 bagels today.
I have eaten 7 bagels today.

So does

$i = 4; while ($i    print "I have eaten $i bagels today.\n";

You can use the control structures if and elseif:

if ($user_count > 200) {
    print "The site is busy right now!";
} elseif ($user_count > 100) {
    print "The site is sort of active right now!";
else {
    print "The site is lonely - only $user_count user logged on.";

The rule of thumb about operators also applies to control structures. You can use switch, do...while, and even the ?: construct.

David Sklar is the CTO of Student.Net Publishing.

Adam Trachtenberg is the Vice President for Production of Student.Net Publishing.

Editor's Picks

Free Newsletters, In your Inbox