Last time we talked about the bare basics of PHP. The Opening/Closing tags and the echo command. Now we’ll move over to what next. Variables!
Variables are actually extremely simple to understand and use. Variables are just ways of placing strings or data into a smaller term, to be used later. Such as,
$a=”foo”;
Anytime you echo or use the variable $a your going to see the word foo. For example,
$a=”foo”;
$b=”bar”;
echo “$a $b”;
Which would display “foo bar” on your screen, just without the ” “.
You can store anything in a variable, even math equations and algorithms. In some more advanced techniques which I’ll explain later, variables can be created “on the fly” and constantly updated/changed over the course of one statement/command. Like in a for() or while() statement. But we’ll get more into that later…
Examples of variable useage:
$a=1;
$b=2;
$c=$a+$b;
echo “$c”;
Mysql queries are often stored in variables to be handled/called later in a script. variable stay within a page, and can be reused as many times as you like within the same page, as long as you don’t overwrite them.
$a=mysql_query(“SELECT * FROM tablename WHERE something=something”);
$b=mysql_fetch_assoc($a);
Where then you can call any field from the table that matches your conditions in the query. Such as if the tablename table containted a column named email. To display that, you would just do this:
echo “$b[email]“;
or
echo “The email is “.$b[email].” you requested.”;
Variables must always start with the $ symbol, and have any name you choose. Such as, $frank, $use, $blank, $Bob. But watch out for capitalizing variable names, they are case sensitive!!
More to come later. This article is far from finished. Check back daily, for updates to it.