Part 15 - Learning PHP for BeginnersWritten 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?
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
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
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
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.
PHPPHP is an abbreviation for "Hypertext Preprocessor" and is a server-side scripting language originally created by Rasmus Lerdorf in 1994.
include filesSimilar 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.
webformAn interactive form that allows you to type information into a webpage.