PHP 101 (Part 1) - Secret Agent Man - A Case Of Identity (
Page 3 of 5 )
Variables are the
bread and butter of every programming language...and PHP has them too. A
variable can be thought of as a programming construct used to store both numeric
and non-numeric data; this data can then be used in different places in your PHP
scripts.
PHP supports a number of different variable types: integers,
floating point numbers, strings and arrays. In many languages, it's essential to
specify the variable type before using it; for example, a variable may need to
be specified as type "integer" or type "array". Give PHP credit for a little
intelligence, though - the language can automagically determine variable type by
the context in which it is being used.
Every variable has a name - in
PHP, a variable name is preceded by a dollar [$] sign and must begin with a
letter, optionally followed by more letters and numbers. For
example,
$popeye $one $INCOME are all valid PHP variables.
Note
that variable names in PHP are case sensitive - so
$me
is
different from
$Me
or
$ME
Here's a simple example
which demonstrates PHP's variables:
<html>
<head>
<title>Bonding With PHP</title>
</head>
<body>
So who do you think you are, anyhow?
<br>
<?
// set up some variables
$fname = "James";
$lname = "Bond";
?>
<b><? echo "The name's $lname...$fname $lname!"; ?></b>
</body>
</html>
In this case, the variables $fname and $lname are first
defined with string values, and then substituted in the echo() function call.
Just as an aside...the echo() function is another important PHP function, and
one that you'll be using a great deal over the next few lessons. It is commonly
used to display output.
Synonymous to echo() is print(), which does
exactly the same thing - take a look at the example below, which demonstrates
how to use it.
<html>
<head>
<title>Bonding With PHP</title>
</head>
<body>
So who do you think you are, anyhow?
<br>
<?
// set up some variables
$fname = "James";
$lname = "Bond";
?>
<? print("<b>The name's $lname...$fname $lname! </b>"); ?>
</body>
</html>
Note how we've included the HTML <b> tag within the
string to be displayed in this example...you can do this too.
Really.