Part 15 - Learning PHP for Beginners

Written by Eric Muss-Barnes, 22 December 2018

Finally, we get to one of my favorite technologies of web development - server-side languages. There are about a dozen server-side languages out there. One of the most popular and common is called PHP.

Why do I love server-side languages so much?

Two reasons.

1.) They are dynamic and automated languages.

2.) They let you use "variables" and "include files".

WHAT ARE DYNAMIC AND AUTOMATED LANGUAGES?

The easiest example of this was shown in the previous article. Suppose you have a website where you need to change a date every year. For example, if you have a biography and someone has 20 years of experience in a particular field, you don't want to update that page every year with 21 year, 22 years, 23 years and so forth.

With a language like PHP, you can include a snippet of code which does some math and calculates the current year, minus a particular year, and that way, the "20 years of experience" will automatically update itself every year. You won't need to change the code every January.

Another thing you can do in an "automated" sense is to loop through data.

What does that mean?

As an example, I have a website with a bunch of skateboards called www.CaliforniaGirlsSkateboards.com. The block of code which displays the skateboard, and all the parameters of that board, is something I only wrote one time. Even though there are about a dozen skateboards, I didn't duplicate the code twelve times. I just wrote it once, and there is a feature in PHP that lets me pull information from a database, wherein it loops through and replicates that code block, for every skateboard entered in the database.

This makes for an incredibly easy-to-maintain website. If I want to change anything or reconstruct anything, I only have to change the code in one spot. In a future article, explaining XML files, I will go into more detail on how those "loops" work.

WHAT ARE VARIABLES AND INCLUDE FILES?

You already know about variables in CSS. The "classes" and the "id" tags are all variables. But, with PHP you can write variables that are blocks of HTML content. One of the best examples I can give for this is a phone number. In my career, I have worked on many business websites. Sometimes, a business will change the main phone number of their company. They may have that phone number repeated dozens of times on their website. If you are forced to do a search/replace across the whole site, you may break something on accident.

What if you could save the phone number as a variable, and just print that variable everywhere the phone number is displayed? That way, if the number ever changes, you simply change the variable. One location. One file. BAM! You're done and nothing breaks!

As for "include files", they are one of the best things ever, in terms of programming.

Include files are the same as a variable, but they are with an entire block of code, instead of a single word or a sentence. So, let's say you have a site with a very complicated hunk of code for the header. Your header contains a logo, and a navigational element, maybe a little gallery showing various images. That header needs to appear on every page of your website. Instead of copying and pasting that code on each individual page, you just create an include file, write your code in the include file, and link to the include file on each page. The advantage to this is obvious. For example, if you need to change the navigation bar, you only alter the include file, and your modification automatically appears across your entire website. Awesome!

PART 15 - LESSON A - VARIABLES AND INCLUDES

<!DOCTYPE HTML> <html> <!-- Below you see the link to the include file of the variables. This is fairly self-explanatory. Just make sure you get the file path correct inside the quotation marks. That is the one thing that can trip you up and get a little confusing from time to time. --> <?php include("includes/variables.php") ?> <head> <!-- Inside the TITLE tag below, you see how a variable is called. The word "echo" just tells the server to print the variable. Look in the variables file and you will see $PageTitle is defined there. --> <title><?php echo $PageTitle ?></title> </head> <body> <!-- Again, below, we have another include file. Instead of containing variables, this include file contains a block of HTML code. So, PHP doesn't care what you put in your include files. You can include HTML or PHP or CSS or JavaScript or anything you want. --> <?php include("includes/header.php") ?> <!-- Once more, we have an example below of calling a variable name from the include file. These variables could have just as easily been defined on this page instead of in an include file. I just wanted to define them in an include to give you a better idea of the structure of a properly built site. --> This is the body text of the FIRST page and it contains the contact information for Jenny at <?php echo $Phone ?>. </body> </html>

Another cool thing you can do with PHP is generate random numbers.

I know. That seems weird, right? Why is that cool?

Random numbers? For what? Who cares?

Well, you may wonder what you could do with that, but the implications are clear in a few seconds. Why would you ever want to generate random numbers for a webpage? Because random numbers can allow you to display different information. For example, maybe you want a photograph to change at random, anytime a user visits the website. I use a PHP random number generator, to print links on the very bottom of this webpage. Let me give you an example of how that works.

PART 15 - LESSON B - RANDOM NUMBER

<!DOCTYPE HTML> <html> <!-- Below you see the link to the include file of the variables. This is fairly self-explanatory. Just make sure you get the file path correct inside the quotation marks. That is the one thing that can trip you up and get a little confusing from time to time. --> <?php include("includes/variables.php") ?> <head> <!-- Inside the TITLE tag below, you see how a variable is called. The word "echo" just tells the server to print the variable. Look in the variables file and you will see $NumbersPageTitle is defined there. --> <title><?php echo $NumbersPageTitle ?></title> </head> <body> <!-- Again, below, we have another include file. Instead of containing variables, this include file contains a block of HTML code. So, PHP doesn't care what you put in your include files. You can include HTML or PHP or CSS or JavaScript or anything you want. --> <?php include("includes/header.php") ?> <!-- Note the link below pulls TWO variables from our variables page. It is pulling "$random_musicvideo" which is the link to the video and it is pulling "$random_videos" which is the random number associated with that video. --> <a href="<?php print("$random_musicvideo[$random_videos]"); ?>" target="_blank">This link will take you to a random music video everytime you reload this page.</a> </body> </html>

Finally, let me show you a simple webform you can create with PHP. Take special note of the folder structure and the filepaths. There will be 3 separate pages - the form itself, the page that processes the information, and the "thank you" or results page. Note the PHP gets a lot more complicated than anything we've used before. But, again, I tried to keep it as simple as possible. I removed any superfluous code and gave you the bare minimum required to make it function. A "proper" form will usually check to make sure fields aren't left blank, that email addresses are legitimate, and so forth. But, I didn't want to include any such functionality, because those features would make it more complex than it needs to be.

PART 15 - LESSON C - WEB FORM

<!DOCTYPE HTML> <html> <!-- Below you see the link to the include file of the variables. This is fairly self-explanatory. Just make sure you get the file path correct inside the quotation marks. That is the one thing that can trip you up and get a little confusing from time to time. --> <?php include("includes/variables.php") ?> <head> <!-- Inside the TITLE tag below, you see how a variable is called. The word "echo" just tells the server to print the variable. Look in the variables file and you will see $FormPageTitle is defined there. --> <title><?php echo $FormPageTitle ?></title> </head> <body> <!-- Again, below, we have another include file. Instead of containing variables, this include file contains a block of HTML code. So, PHP doesn't care what you put in your include files. You can include HTML or PHP or CSS or JavaScript or anything you want. --> <?php include("includes/header.php") ?> <!-- I have not shown you forms yet, but I think you know enough about HTML at this point to start to figure it out on your own. The form begins with a FORM tag and there are various input fields for text, email and a button to submit the form. The "NAME" element of the FORM tag is not necessary unless you are giving the form special features. The "METHOD" element is either "post" or "get". This isn't really important for this demonstration. The main difference between them is that a "post" is just sending text and a "get" is used when your form allows people to upload a file. The "ACTION" element obviously points to the file which will process the form. The elements of the INPUT tags are pretty self-explanatory. "TYPE" descibes what kind of content goes in that field. "NAME" is used to identify that field. "PLACEHOLDER" is not required, but it puts temporary text inside the field which gets replaced when you type. --> Filling out the form below will take you to a thank you page and it will send you an email. <br><br> <form name="thisisthecontactform" method="post" action="includes/send_form_email.php"> <input type="text" name="name" placeholder="Your Name"><br> <input type="email" name="email" placeholder="Email Address"><br> <input value="Submit" type="submit" id="submit"> </form> </body> </html>

That should give you a good overview of some of the things you can do with PHP code. The nomenclature and syntax of PHP is very similar to ASP, so it was pretty easy for me to learn, because I already had 10 years of ASP experience before I touched any PHP code. I don't expect you to learn it really quickly. There are still potential features in PHP that are beyond my skillset. But, that's the whole point of learning these skills! As you advance and grasp more the capabilities of the code, you can become more innovative and creative and build things of greater worth.

And remember, kids, the world owes you nothing... until you create things of value.


Glossary

PHP

PHP is an abbreviation for "Hypertext Preprocessor" and is a server-side scripting language originally created by Rasmus Lerdorf in 1994.

include files

Similar to "variables" but instead of a single word or a phrase, an "include file" represents an entire block of multiple lines of code. Include files (or "includes") can be reused over and over on various pages or areas of a website to make maintaining the site quicker, easier and more reliable.

webform

An interactive form that allows you to type information into a webpage.


Downloads


Other Articles