Introduction to PHP and MYSQL – Part 2
PHP on the half-shell
[Part 1]
PHP is a server-side language. Simply put, this means that instead of creating a static HTML page that surfers will see over and over again, PHP code is processed by your server to build HTML pages on the fly before delivering them to the client.PHP on the half-shell
[Part 1]
PHP is a server-side language. Simply put, this means that instead of creating a static HTML page that surfers will see over and over again, PHP code is processed by your server to build HTML pages on the fly before delivering them to the client. This article will focus on the syntax and fundamentals of PHP, and will require that PHP be installed on your server. Please note that there are many ways to accomplish the same tasks in PHP. This article is a quick start guide and I encourage the user to consult http://www.php.net for a more in-depth look at the language itself.
Using PHP
First, let’s examine how PHP is used within an HTML page. Let us consider these two code listings:
File 1: helloworld.html
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
<BODY>
<center>Hello World</center>
</BODY>
</HTML> File 2: helloworld.php
<HTML>
<HEAD>
<TITLE>Hello World</TITLE>
<BODY>
<?PHP
//This is our code
Print “<center>Hello World</center>”;
?>
</BODY>
</HTML>
As an HTML developer, File 1 will look familiar to you. Both of these files output the same exact information to the browser, and when looking at the source for File 1 it will look indistinguishable from the source of File 2. The important difference is that File2 has been generated in a dynamic fashion with PHP code. Please note that all PHP code must appear between the <?PHP ?> tags. Each line of code must end with the semi-colon (;), and the comment block “//This is our code” will never appear in our final browser output. I strongly recommend using comments within your code as an internal documentation resource.
Variables
Variables are a temporary repository of data. They are flexible in their use and can be used to hold alpha, numeric or alphanumeric data. Consider the following code:
<?PHP
//Sample of using Variables
$myname = “Bob”;
$myage = 43;
$nextyear = $myage + 1;
echo “My name is “.$myname. ” and I am “.$myage. ” years old. Next year I will be “.$nextyear;
?>
Note that I have used the echo command to output information to the browser, instead of the print command in this listing. We are using a combination of literal and variable values to output the result of this code:
My name is Bob and I am 43 years old. Next year I will be 44
The literal value of “My name is “ is concatenated, or joined to, the variable value $myname by the use of the period. Variables are case sensitive, meaning simply that $myname and $Myname would be considered two different variables. Finally, variables can be used in mathematical equations as demonstrated with $nextyear=$myage+1.
Repetitive Iterations
Since the earliest days of computers, one of the most powerful uses of the computer was to repeat a series of repetitive actions. PHP offers several ways to accomplish this, these are called FOR LOOPS, WHILE LOOPS and DO WHILE LOOPS. We will examine the FOR LOOP here and I suggest you look into the subtle differences between the three methodologies on your own to decide what is best for any given situation.
Consider the following code snippet:
<?PHP
//Sample of the FOR LOOP
for($myvalue=2; $myvalue<=20; $myvalue += 2)
{
echo “The value is now set to “.$myvalue.”<br>”;
}
?>
The FOR LOOP, allows us to set three parameters. The loop sets the initial value of our variable, how high the value will increment and the amount by which we will increment that value. Execute this code on your server and you will get this output:
The value is now set to 2
The value is now set to 4
The value is now set to 6
The value is now set to 8
The value is now set to 10
The value is now set to 12
The value is now set to 14
The value is now set to 16
The value is now set to 18
The value is now set to 20
Conditions and Branches
The second thing that computers have always been good at, is evaluating a given set of data and making simplistic decisions on what to do based on that information. The two basic forms of this in PHP are the IF statement and the SWITCH statement. I will cover the IF statement here and suggest that readers research the use of the SWITCH statement as it is a very powerful feature to learn to use.
Examine this code:
<?PHP
//Sample of Conditions and Branching
for($myvalue=2; $myvalue<= 20; $myvalue += 2)
{
if($myvalue < 10)
{
echo “The value “.$myvalue. ” is less than 10<br>”;
}
elseif($myvalue >= 10 && $myvalue < 16)
{
echo “The value “.$myvalue. ” is greater than or equal to 10 and less than 16<br>”;
}
else
{
echo “The value “.$myvalue.” is greater than or equal to 16<br>”;
}
}
?>
The If statement allows you to set up multiple conditions. If this = that, else if this = those, or else this = something all together. Note in the elseif() block, that I have set two conditions. This >= that AND this < those. With the If statement, you can create complex branches based upon decisions made depending upon the data you are evaluating. The output of this code will look like this:
The value 2 is less than 10
The value 4 is less than 10
The value 6 is less than 10
The value 8 is less than 10
The value 10 is greater than or equal to 10 and less than 16
The value 12 is greater than or equal to 10 and less than 16
The value 14 is greater than or equal to 10 and less than 16
The value 16 is greater than or equal to 16
The value 18 is greater than or equal to 16
The value 20 is greater than or equal to 16
Summary
PHP is a powerful language that offers dynamic delivery of content. PHP has syntax, or a language, of it’s own that it uses to deliver HTML documents to the surfer’s browser and allows you the flexibility through the use of Variables, Repetitive Iterations and Conditional Branches to write professional quality web sites.
Our next chapter will cover processing HTML forms to retrieve and manipulate data that you gather from the user. Until the next time, keep burning the Midnight oil and I will be seeing you on the YNOTMasters discussion boards.
— Midnight
Bob Cristello is a recognized leader in the programming world. His articles on programming have appeared in Byte, InfoWorld, Avatar, Computer Telephony and Visual Basic Programmers Journal. He is a former keynote speaker for the Microsoft nationwide broadcast series. Bob’s clients have included Prodigy Online, Arthur Anderson and Nellie Mae. He recently retired as the Head of the Internet Development Group at REBAR and lives just outside of Boston with his wife, Elsibeth. Most days, Bob and Elsibeth can be found in irc.webmasterlive.com in #adultnetsurprise and are better known as Midnight and Kandi. Bob can be reached by emailing midnighterotica@hotmail.com or via ICQ# 118997859.