Because of it’s wide availabilty on web servers and it’s free nature, PHP has become the choice scripting language in the internet. If you would like to get a good grasp on its precepts using variables, read on…
Intro to PHP – What is PHP?
PHP is recursive acronym for “PHP: Hypertext Preprocessor”. Originally it stood for “Personal Home Pages”, it has outgrown its description years ago. Overall it’s a robust server side scripting language that works great within web development and can easily be inserted in HTML code. In order to execute PHP scripts, you will need a server that supports it. Luckily, virtually all UNIX web hosting companies have PHP already installed (check with your host to what version they are running). If you have full access to your server or want to install it on your Linux machine, you can hop over to http://www.php.net/manual/en/installation.php for installation information. For Windows users (like me), you can try XAMPP – it will give you Apache server, MySQL database, PHP and Perl all in one very simple and convenient installation package.
Scripting With PHP
Creating a minimal PHP test script is effortless:
?php
echo "This Works!";
?
Notice the opening characters? The ?php
tag is used to tell the server that PHP is going to be used, so it stops recognizing the code as regular HTML and runs it as PHP; since it’s all on the server, after script execution it goes back to the client’s browser it returns everything as HTML.
The echo
in the next line is used to display strings or text. In this case, it’s plain text — the phrase “This Works!” Closing out the PHP script is the closing ?%
tag. Everything residing after the closing tag is regarded as normal HTML.
Simply save the file as *.php, upload it to your server, and bring up the page on your browser. If you’ve used the above code, you should see “This Works!” in your browser window.
Variables
Next, we’ll assemble the building blocks of more functional scripts by using variables. You can think of a variable as a “storage container” in which to store values that you’ll use on your web page, such as text and numbers.
To declare variables, you use the $
symbol following the equal sign (=
) with the variables value in quotes, like this:
$varName = "varValue"
You all remember the echo command? We’ll use that to display variables:
?php
$devWS="Welcome to My Website"
echo ("$devWS - which I have just created using PHP");
?
Dynamic Variables
The best way to understand what dynamic variables are is through an example, such as this:
?php
$user = "John";
$holder = "user";
echo "${$holder}";
// would print John..
?
You can see that in the second line of this script declares a variable called $user
and assigns him the value “John”. The third line declares a second variable named $holder
. When it gets to the fourth line, this is where the actual “work” of the code occurs: the server will see the brackets ({}) it first translates what is inside ($holder = "user"
), after that, it takes "user"
and adds a “$” symbol, and what finally comes out is the variable $user
, which translates into “John”. The fifth line, which begins with //
, is just a comment on what the outcore of this is going to be.