Scalar Variables and Data
July 21, 2000
We've seen how a PHP page looks, and how the code can be structured --
but that's all. Only one thing missing ... the language itself! And so
begins the rest of our journey. The PHP language shares much in common
with other popular programming languages, especially Perl and C. If
you have any experience with these, PHP will be a snap. Non-programmers
will find PHP straightforward, but must understand that the art of
programming is a separate matter from the syntax of a particular
language -- just as cooking a gourmet meal requires more than just a
list of ingredients, and writing a work of literature requires more than
a knowledge of spelling and grammar.
In most cases, your PHP scripts will work with data -- to process,
]manipulate, and output. Data generally consists of numbers (numeric
values) and letters or words (string values), although there are other
types of data that PHP understands.
- Integers and floating-point numbers are the two numeric data types in
PHP.
An integer is any whole number such as 5, 10, 0, or -3.
A floating-point number is any number with a decimal portion, such as 5.25
or -43.2339 and so on.
- String data is composed of any alphanumeric characters, such as words
or symbols. Examples of strings include "pepsi", "The Simpsons",
"23 skidoo", "Yahoo!", and so on.
Integer, floating-point, and string values are all known as
scalar data because they are single data points. This is another
way of saying that the number 5, or the word "hockey", is just
"one thing" (as opposed to, say, a list of things which we will see
shortly).
In PHP as with most programming languages, we use variables to contain
a value -- a variable is like a label that refers to a value. The
value assigned to a variable can change over time, as a result of
processing and manipulation such as arithmetic. Variable names are
usually words and are preceded with a dollar sign ($) in PHP. Let's
assign the value 100 to a variable named $price:
$price = 100;
Code snippets such as the above are examples, but remember that PHP
code in a page must always appear within <?php ... ?> tags, even
though we won't always write those tags around every single example
snippet.
Also notice that good practice suggests we end PHP statements with a
semicolon, as seen above. A statement is sort of like an English
sentence -- it is one full programming "thought". PHP statements are
made up of expressions, where an expression is a meaningful
fragment of code. The previous example represents an assignment
expression, because a value has been assigned to a variable. In this
case, the statement happens to be made of only the one expression,
which is perfectly fine. Many PHP statements are made up of several
expressions, which we'll see later.
PHP determines the data type of a variable based on context, such as
the context of the assignment expression:
$price = 100;
$firstName = "Martin";
$breed = 'ferret';
$name = $firstName;
In the first line above, which we've already seen, PHP assumes that
100 is an integer. In the second line, PHP understands
Martin as a string because it is enclosed in double quotation
marks. The same applies to ferret, although it is enclosed in
single quotation marks. In the fourth example, $name is assigned
whatever value is currently held by $firstName, which happens
to be a string, so $name also contains a string value
(Martin).
Note that in the fourth example, $name receives a copy of the
value in $firstName. Thus, if we then change the value of
$name, this has no effect on the value held by
$firstName.
Important:
Variable names in PHP are case-sensitive. In other words,
the variable $firstName is completely independent from a
variable named $firstname.
Why does a value's data type matter to PHP, or to us for that matter?
It matters when you need to perform an operation on one or more
values. Operations include arithmetic such as addition and subtraction.
For instance, you can add an integer and a floating-point value to
yield another floating-point value:
$total = $price + 3.95; #flat rate shipping
Above, $total will contain the value of whatever $price
contained plus 3.95. Also notice the use of a comment marked by a pound
sign (#) -- PHP will ignore everything from the pound sign to the end
of that line, so that we can add annotations to clarify the purpose of
a line of code.
But suppose you tried to add an integer and a string value?
$total = $price + "fruit juice";
Of course that doesn't make any sense! PHP will try to resolve this
problem by converting a string to a number, if the string looks like
a number such as "4.50" (converts to 4.50) or even "10 apples"
(converts to 10). In the above example, PHP can't make any number out
of "fruit juice", so it will be given the value 0, and 0 is what will
be added to $price.
Variables are a crucial component of most programming languages
including PHP -- you will use them to store pieces of data, manipulate
the data, and usually output some of the data.
PHP Structure
Welcome to PHP
Data collection: Arrays
|