A Variable in PHP, simply put, is one
thing that means another thing or things - a “container” if you will. It can
represent text, numbers, calculations, and more.
Variables are quite powerful, and if you mess ‘em up,
they’ll come get you in the middle of the night.
Declaring a variable is easy. No spaces in the variable
name, please - PHP doesn’t like that...
<?php
$This_thing = “The Other Thing”;
?>
Now before you go off saying “What the
heck would I
need THAT for?”, remember that variables are very useful...especially if you
are PHP Include-ing other files (Like the foreshadowing there? Do ya?)
Real World Usage For
Variables
Here’s an example of how you can use a variable in the real
world: show the current date on your website.
<?php
$today = date("F j, Y");
echo "$today";
?>
This example sets the date command as a variable called
“$today”, and uses echo to display it on the screen.
And now, for a quick
tangent...
More about the “DATE” command - it is very versatile and
flexible - see the guide below to use it to it’s potential!
And now...back to
Variables!
ECHOing more that one
Variable at a time
You can use the ECHO command we learned earlier to display
more than one variable at a time. Combining variables can be extremely useful. Take
a look at this example...
<?php
$phrase1 = "That's No Moon,";
$phrase2 = "It's a Space Station!";
echo "$phrase1 $phrase2";
?>
Neat, huh?
So, you can see where this might be useful, I hope?
You can also echo text and variables in the same statement
by putting periods around the variable....like so...
<?php
$items = 3;
echo "You have purchased ".$items."
items.";
No comments:
Post a Comment