Monday, December 23, 2013

Learn PHP from Scratch

Holding the title of “number one”, PHP is the most popular language among developers. Even still, many prefer different languages. Yours truly, for example, is most comfortable when working in the ASP.NET environment. However, because of the enormous success of WordPress, more and more developers have decided to expand their horizons and learn yet another language.
I happen to be one of those very people. As my clients increasingly ask for WordPress implementation, learning PHP has become a requirement. I’m not alone in this endeavor. For those in the same boat, why not take the time and learn with me?

The Mission Statement

Over the course of the next few articles – to be posted each Wednesday – it is my intention to create a “work-out regimen” for all of us. If you’ve been meaning to learn, but haven’t gotten around to it yet, now is the time! On the flip side, for those of you who are PHP ninjas, I respectfully request that you get involved and offer advice to the rest of us. If you’ve benefited from the dozens of tutorials on this site, take a few moments to give back, via the comments section. This will be your one-stop resource for everything PHP. Each Wednesday, I’ll post a training article, as well as a list of resources that will help to explain the concepts in the lesson further. The key here is that I’m a beginner just like everyone else, relatively speaking. We can motivate one another to learn as quickly, and efficiently as possible.
So, why would you learn from a beginner? Try not to think of it as me teaching you. Think of these articles as a community effort where we all help each other. I’ll be learning from many of you in the same way that you learn from me.

What Is PHP?

PHP stands for Hypertext Preprocessor. While other languages, like Javascript, function on the client-side, your PHP code will execute on the server level. It works seamlessly with our HTML. Furthermore, your PHP can be embedded within your HTML and vice versa. The important thing to remember is that, no matter how complicated your PHP is, it will ultimately be output as simple HTML.

Why Would I Use PHP?

HTML is 100% static. By implementing PHP into your code, we can create dynamic sites that will change dependent upon specified conditions. With a community base second to none, this open-source language has proven itself over the years to be one of the best options for dynamic web applications.

Is PHP Similar To Any Other Languages?

Absolutely. I was pleasantly surprised as I began my training. If you have even a modest amount of knowledge when it comes to ASP.NET, Perl, Javascript, or C#, you’ll find that you pick up the syntax quickly.

What Do I Need To Get Started?

You must have the following installed on your computer in order to begin.
  • Apache
  • MySQL
  • Web Browser
  • Text Editor
  • PHP


Yes, I’m sorry to say that there are a few more acronyms to learn. “WAMP” stands for “Windows-Apache-MySQL-PHP”. It is an open source project that will allow us to download everything that we need to get started right away. If you’re a Windows user, visit On the other hand, if you’re using a Mac (MAMP), you’ll want to pay a visit to

Video Training
Our first stop will be at Maybe more than any other resource, has provided me with a wealth of knowledge that I’ll forever be grateful for. For the price of a couple of pizzas, you’ll gain access to a video database that details everything from ASP to SEO – and every acronym in between. When a client requests that I use a piece of software that I’m not completely comfortable with, my first stop is If you’re still unconvinced, why not do a google search for “ free trial”. I guarantee that you’ll come up with something. Just make sure that, if you are more than satisfied with their offerings, you sign up.
After you’ve signed up for an account, or the free trial, go to the site and under the “Subject” drop-down-list, scroll to PHP. For this lesson, we’ll be focusing on the “PHP with MySQL Essential Training” videos. Try to watch the first three chapters this week. That will get you fit and primed for next week’s lesson.

The Basics

In order to alert the server that we are working with PHP, you’ll need to use the following syntax when adding PHP into your HTML documents:
  1. <?php  
  2. ...code goes here  
  3. ?>  
We start and end each PHP declaration with “<?php” and “?>”, respectively. Refer back to your code and insert the following:
  1. <?php echo "This is PHP in action"; ?>  
Notice that in this second example, we kept everything on one line. Remember, PHP is not white-space sensitive. Here, we are telling the server to “echo”, or write “This is PHP in action” onto the page. Each declaration in our code must have a semicolon appended to the end. Although HTML can be forgiving if you accidentally forget a bracket, PHP unfortunately is not. If you don’t use the correct syntax, you’ll receive an error. In this case, when we only have a single declaration, we could technically get away with leaving the semicolon off. But, it’s always important to follow best practices.

Defining Variables

We can assign values to variables quite easily. Rather than using “var” (C# and Javascript), or “dim” (VB), we can declare a variable in PHP by using the “$” symbol. For instance, let’s say that I want to assign the previous string to a variable called “myVariable”. I would write…
  1. <?php $myVariable =  "This is PHP in action";  
  2.   echo $myVariable;  
  3. ?>  
This example will produce the exact same result as the previous two. However, in this scenario, we’ve assigned the string to the variable and then “echoed” the variable instead. Now what if I wanted to concatenate a variable and a string?
  1. <?php $myVariable =  "This is PHP in action.";  
  2.   echo $myVariable . " My name is Jeffrey Way";  
  3. ?>  
By using the period, we can combine variables and/or strings.

Inserting Comments Into Your Code

If you’re familiar with CSS and Javascript, you’ll find that inserting comments in PHP is virtually the same.
  # This is a single line comment.
  // This is the most common way of commenting out your code.
  /* Here is a way to comment over multiple lines. This is the exact
     same way that you would comment in CSS */

Combining HTML With Our PHP

As said previously, remember that PHP and HTML can work in combination. Simply because we’re in the middle of a PHP statement does not mean that we can’t embed elements such as a break or strong tag.
<?php echo "<strong>This text is bold.</strong>"; ?>

Defining Your First Function()

Creating functions in PHP is nearly identical to Javascript’s implementation. The basic syntax is…
function name ($arguments){
your statement goes here;
If we wanted to create a function that “echos” 10 plus 5, we could write…
  1. <?php  
  2. function addNumbers (){  
  3. echo 10 + 5;  
  4. }  
  5. addNumbers();  
  6. ?>  
We’re creating a simple function that will output “15″. We call the function with “addNumbers(). In this case, we aren’t using any arguments. Let’s see how we can implement them in order to make our function more versatile.
  1. <?php  
  2. function addNumbers($firstNumber$secondNumber){  
  3. echo $firstNumber + $secondNumber;  
  4. }  
  5. addNumbers(10, 5);  
  6. ?>  
Now, our code is much more flexible. When we created our “addNumbers()” function, we added two arguments – $firstNumber and $secondNumber. The function will simply echo the sum of these two variables. When the function is called, we’ll need to pass in our two numbers – addNumbers(10, 5). In a real-world situation, the values for these variables might be taken from a couple of textboxes.
That should be enough for this week. If these concepts are still vague, go back and read the article again. Also, be sure to check out the following resources which will help you to further grasp the syntax of PHP.