Powered by Blogger.

Variables


There’s a simple metaphor that will help you understand what PHP variables are all
about. Just think of them as little (or big) matchboxes! That’s right—matchboxes that
you’ve painted over and written names on.
String variables
Imagine you have a matchbox on which you have written the word username. You then
write Fred Smith on a piece of paper and place it into the box (see Figure 3-2). Well,
that’s the same process as assigning a string value to a variable, like this:
$username = "Fred Smith";
Figure 3-2. You can think of variables as matchboxes containing items
The quotation marks indicate that “Fred Smith” is a string of characters. You must enclose
each string in either quotation marks or apostrophes (single quotes), although
there is a subtle difference between the two types of quote, which is explained later.
When you want to see what’s in the box, you open it, take the piece of paper out, and
read it. In PHP, doing so looks like this:
echo $username;
Or you can assign it to another variable (photocopy the paper and place the copy in
another matchbox), like this:
$current_user = $username;
50 | Chapter 3: Introduction to PHP
Basic Syntax
PHP is quite a simple language with roots in C and Perl, yet it looks more like Java. It
is also very flexible, but there are a few rules that you need to learn about its syntax and
structure.
Semicolons
You may have noticed in the previous examples that the PHP commands ended with a
semicolon, like this:
$x += 10;
Probably the most common cause of errors you will encounter with PHP is forgetting
this semicolon. This causes PHP to treat multiple statements like one statement, which
it is unable to understand, prompting it to produce a Parse error message.
The $ symbol
The $ symbol has come to be used in many different ways by different programming
languages. For example, if you have ever written in the BASIC language, you will have
used the $ to terminate variable names to denote them as strings.
In PHP, however, you must place a $ in front of all variables. This is required to make
the PHP parser faster, as it instantly knows whenever it comes across a variable. Whether
your variables are numbers, strings, or arrays, they should all look something like those
in Example 3-3.
Example 3-3. Three different types of variable assignment
<?php
$mycounter = 1;
$mystring = "Hello";
$myarray = array("One", "Two", "Three");
?>
Your PHP program is responsible for passing back a clean file suitable for display in a
web browser. At its very simplest, a PHP document will output only HTML. To prove
this, you can take any normal HTML document such as an index.html file and save it
as index.php, and it will display identically to the original.
To trigger the PHP commands, you need to learn a new tag. The first part is:
<?php
The first thing you may notice is that the tag has not been closed. This is because entire
sections of PHP can be placed inside this tag, and they finish only when the closing part
is encountered, which looks like this:
?>
A small PHP “Hello World” program might look like Example 3-1.
Example 3-1. Invoking PHP
<?php
echo "Hello world";
?>
The way you use this tag is quite flexible. Some programmers open the tag at the start
of a document and close it right at the end, outputting any HTML directly from PHP
commands.
Others, however, choose to insert only the smallest possible fragments of PHP within
these tags wherever dynamic scripting is required, leaving the rest of the document in
standard HTML.
The latter type of programmer generally argues that their style of coding results in faster
code, while the former says that the speed increase is so minimal that it doesn’t justify
the additional complexity of dropping in and out of PHP many times in a single
document.
As you learn more, you will surely discover your preferred style of PHP development,
but for the sake of making the examples in this book easier to follow, I have adopted the
approach of keeping the number of transfers between PHP and HTML to a minimum—
generally only once or twice in a document.
By the way, there is a slight variation to the PHP syntax. If you browse the Internet for
PHP examples, you may also encounter code where the opening and closing syntax
looks like this:
<?
echo "Hello world";
?>

We’re going to cover quite a lot of ground in this section. It’s not too difficult, but I
recommend that you work your way through it carefully, as it sets the foundation for
everything else in this book. As always, there are some useful questions at the end of
the chapter that you can use to test how much you’ve learned.
Using Comments
There are two ways in which you can add comments to your PHP code. The first turns
a single line into a comment by preceding it with a pair of forward slashes, like this:
// This is a comment
This version of the comment feature is a great way to temporarily remove a line of code
from a program that is giving you errors. For example, you could use such a comment
to hide a debugging line of code until you need it, like this:
// echo "X equals $x";
You can also use this type of comment directly after a line of code to describe its action,
like this:
$x += 10; // Increment $x by 10
When you need multiple-line comments, there’s a second type of comment, which looks
like Example 3-2.
Example 3-2. A multiline comment
<?php
/* This is a section
of multiline comments
which will not be
interpreted */
?>
You can use the /* and */ pairs of characters to open and close comments almost
anywhere you like inside your code. Most, if not all, programmers use this construct to
temporarily comment out entire sections of code that do not work or that, for one reason
or another, they do not wish to be interpreted.