PHP Tutorial: Learning PHP (Part 1)

Comments 2 Standard

When the Internet exploded in the .com age people were just starting to realize the potential of online programming languages. PHP, or Hypertext Preprocessing, is one of my favorite programming languages. One of the best things about PHP is that it’s easy enough for people to learn even if they’ve had little to no prior programming experience. It’s weakly typed so beginners don’t have the additional complexity of trying to put strings in integers, or understand the difference between a double and float.

So here’s a little PHP lesson for anyone whose decided they’d like to try their hand at it.

Requirements
In order for PHP to work you need a few things.

  1. A place to host all of your files while you test it out. Can’t afford hosting? No problem check out XAMPP!
  2. Your hosting provider needs to have PHP version 4.0 or later installed.
  3. Access to an FTP program, or control panel so you can upload your files to your web space.
  4. Knowledge of basic HTML concepts is a plus. If you want to brush up on your HTML skills try http://www.lissaexplains.com

Creating a PHP File
This is super easy.

  1. Navigate to a place on your computer where you’d like to keep all your PHP files.
  2. Create a new folder for all of your files. Call it something like PHP tutorial, or something you can remember.
  3. Right click and select create a new text file. If you don’t see that option you can always go to any notepad or wordpad program and when it opens you’ll have a blank file in front of you. Either way you should end up with a blank window in some kind of text editor program.
  4. Go to the file menu at the top of the text editor and choose file > save as. You can give the file any name you want, just make sure you append .php to the end of the file. For instance, you might put in demofile.php
  5. Save your file in the folder you created for it. You should see that the file now has a .php extension. That’s exactly what you want.

Your First Script

You can use PHP to tell you all sorts of interesting things about the server you are using with your hosting provider. This comes in handy as you continue to work with PHP and need to know which features are installed, or what software it’s running on.

    1. Open the file you created earlier. Inside it you want to write:
<?php

//this will display information about the PHP setup on your web space

phpinfo();

?>
  1. After you’ve entered this in your file go ahead and save it.
  2. Upload the file to your web space.
  3. Navigate to that file on your web space and view it. You should see something that looks kind of like this:

 

phpinfo()

This is all generated by what you wrote in the file. Now to explain a little bit about what we did.

PHP has start tags and end tags, a lot like HTML. With HTML you can make text change color, fonts get bigger and smaller, etc. To do this you use tags. For instance, to make something bold you would write <b>text that you want bold</b> in your HTML file. PHP works on a similar idea. When you start writing PHP code you have to use <?php and when you’re done using PHP you end the tag with ?>.

Sounds easy enough.

Now lets talk about the comment you see. A comment is something that you can write in your PHP code that allows you to document what you did, or remind yourself of something. Because PHP is server-side only, everything you write in your PHP file is invisible to other users.

Try this! Right click on the screen with the purple PHP information and select view source code. What do you see? You’ll notice you don’t see any of the code you wrote into your file. That’s what makes PHP server-side only. Some other online languages, like Javascript for instance, are not server-side only. If you were to right click on a file with Javascript in it and select view source code you could see all of the Javascript code you wrote in the file.

Comments are special. Unlike other code, a comment doesn’t actually do anything. You can and should use comments to explain what you are doing in your code and why you’re doing it. The more you comment your code the more it can help you later. Let’s say you start working on a piece of code, and then get busy doing something else for a few weeks. When you come back to the piece of code your comments can refresh your memory and help you continue where you left off.

Finally, we’ll look at the last thing in the file, phpinfo(). A lot of programming languages use functions and PHP is one of them. phpinfo() is a function defined by the PHP language which displays information about the version of PHP you are using, any add on capabilities, and some information about the server you’re on. But this isn’t a tutorial about functions, so we’ll leave the explaining there and move on to more of the basics.

One other thing you may notice is the semicolon ; after the phpinfo() function. A semicolon in PHP is a lot like a period at the end of a sentence. A semicolon tells PHP that you’ve come to the end of a line in your code.

Did You Know? One of the most common problems you’ll run into as you learn how to program in PHP are syntax errors, like forgetting to put a semicolon at the end of a line of code. There are a lot of other syntax errors you’ll learn about as you keep programming. But don’t be discouraged! Even experienced programmers get syntax errors, they just have more familiarity with errors which helps them fix them faster.

Variables

The easiest way to think about a variable is to imagine a container. A variable, just like a container, can store things. However, a variable, just like a container, has some limits as well.

A variable is used to hold information. Sometimes the variable has values in it, like a container might have some cookies inside, and sometimes it’s empty. We say that an empty variable has a null value. Just like a container, you can put values into a variable and take them out. A variable in PHP looks something like this:

$variablename;

The dollar sign $ at the beginning of the variable tells PHP that the name following the dollar sign is a variable. The semicolon at the end of the variable name lets PHP know the variable is null. A variable can have any name you want it to as long as it starts with a letter, number, or underscore and doesn’t have any other characters in it. Let’s look at some more variables:

$red = “color”;

$number = 7;

$empty = null;

$otherempty = ;

One of the things we try to do with variable is to give them a descriptive name. A variable with a descriptive name will help you understand what’s going on in your code. However, it’s probably not a good practice to make a variable with a really really long name either. The longer your variable name is the easier it is for you to misspell the name which would generate a syntax error. All of the variables you see above are valid even though they have a different variable name.

Syntax Error – in the simplest terms, when PHP cannot understand what the code is telling it to do. PHP files go through something called an interpreter. An interpreter takes the code you’ve written and translates it into code a computer understands. When the syntax in your file is incorrect, the interpreter cannot translate the file like it needs to and this is what generates the error.

If-Else Statements

Programming languages are all about logic. They usually deal with things that happen in steps, and decision making. For instance, lets say you are going to the grocery store and you only have $15.

<?php

$money = 15; //how much money you have

if ($money > 5) //you have more then 5 dollars

{

echo "I'm buying some potato chips. ";

$money = $money - 5; //subtract the cost of chips from how much money you have

echo "I now have " . $money . " dollars.";

}

else

{

echo "I cannot afford potato chips";

}

?>

So, lets break this down. First we created a variable that we called $money to represent how much money we have. We gave the money variable the value of 15 dollars. Next, we used an if statement to check if we had more then 5 dollars. If have more then 5 dollars, then on the page we’ll see the text we have inside of the brackets {} in the if statement. Can you guess how much money it will print out on the screen? If you said 10 then you’re right! The statement $money = $money – 5; is the same thing as saying $money = 15 – 5; because the value of $money is 15.

So… what do you think happens if you changed the $money variable at the top of the pay to 3 dollars? Or, what happens if you change $money = $money – 5; to $money -= 5; Try it and find out!

Continue to Part 2 >>

Advertisements

2 thoughts on “PHP Tutorial: Learning PHP (Part 1)

  1. Thank you for this tutorial – I like it because its down to earth and ‘my level’. Very funny as well I love humor and learning. 🙂 Thanks again!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s