This post will cover the basics of PHP in this PHP Online Tutorial for Beginners and I will go into great detail about everything that I talk about. Let’s start out by finding out…
What Is PHP ?
PHP is short for PHP: Hypertext Preprocessor. This is called a recursive acronym. (Wikipedia)
PHP is a very powerful programming language used all over the world. It can be used to create dynamic web pages. What is a dynamic web page? A dynamic web page is a web page which can change. Information, texts, details, etc. are typically stored in a database and can be retrieved at any time.
An example of a web page could be a log in page. Let’s say you log in to a website and on the home page, it says “Welcome, ” and then your username. If you logged in with different credentials, the welcoming message would’ve changed to “Welcome, ” and then your other username.
PHP is a very popular programming language and one of the great things about it is how easy it is to use! And it’s also capable of object-oriented programming but I’ll get to that later.
This blog post will be seriously long but you can also come back here if you forgot how to do certain basic things. This is what I’ll cover:
- The basic structure
Before I get on with how to program in PHP, I need to inform you that for PHP code to work on your server, you need PHP installed. Most web hosts have PHP installed but if you were to set up your own server, here’s how to install PHP: http://php.net/manual/en/install.php
If you’re using a web hosting service and PHP is activated, you can just go ahead and continue with this tutorial. Let’s move on!
When you want to write PHP code, you’ll need to have some sort of text editor available. I prefer writing PHP code in Notepad++.
When you want to tell the server that you are writing PHP code, you need to use the PHP tags. All your PHP code must be placed between the opening PHP tag and the ending PHP tag. It’d be quite troublesome if the server had to guess where the PHP code is, right? 😉
Here’s the code for one of the most simple PHP scripts I can think of. I’ll make PHP print out the text I am a PHP programmer.
Here’s the code:
Let’s start with the PHP tags. The <?php and ?> are the PHP tags I was talking about earlier. Between those tags you see the echo statement. We use echo a lot when we want to print something out. Note that you can also mix in html code inside the double quotes. This is possible because when we load a PHP script, the server will execute the code then we’ll get the page in HTML code.
So the PHP code is interpreted by the server and then we only get the html code. That’s why we can’t see PHP code inside the source code of websites with our web browser.
If we could see PHP code in the source code, it’d give hackers an easy way to hack your website. Keep in mind, though, that if you were to download a PHP file onto your computer, you’d see the PHP code because there wouldn’t be anything that would interpret the PHP code. This is assuming you didn’t download the PHP file onto your server.
You might’ve noticed the semi-colon at the end of our echo statement. The semi-colon just signals that the statement ends here. We always need to use semi-colons when we’re done with a statement.
When you’re done writing your PHP code, you need to save your document as a .PHP extension. Afterwards, you just need to upload the file to your web server and it should run. This is assuming you had no errors inside your code, which you will have a lot.
Commenting your code
It’s conventional to comment your code. This is because it can help you organize your code and it can also help others that are reading your code understand what’s happening. So, sometimes you want to add a comment to a part of your code.
To do this, you need to insert two slashes followed by your comment. The comment must be placed on its own line or after the semi-colon of a statement. It can also be placed after a statement which ends with a curly brace but I’ll get to that later. When you add the two slashes, it signals that it is a comment. Keep in mind that the comment will also be interpreted but it won’t do anything. Therefore, you can’t view comments from your PHP code either.
Here’s an example of commenting your code:
When you write a comment, and then start writing on a new line, that won’t be interpreted as a comment. Whenever you start typing on a new line, you’ll need to add two slashes on that line to signal that this is a comment.
That’s pretty much it about comments. Let’s move on to something which we will use a lot in PHP code: Variables!
What is a variable?
A variable is basically something that can hold a value whether it be text or numbers. Think of variables as boxes. Let’s say we want to create a variable which stores our name. Think of it like finding a box, labeling the box whatever you want and then put a sheet of paper with your name on it, inside the box. We just created a variable.
In some programming languages, you need to define what sort of “box” you’ll be declaring. So if you wanted a box that’ll store your age, you’d have to set the box type to integer (or some other variable types which can contain numbers.) However, this is not needed in PHP. You can freely just declare as many variables as you want and you do not have to think about what type of variable it is that you’re going to declare.
We can use variables in many ways. Let’s say we wanted to get some user input. We would want to store the input in variables so we can manipulative or check user input easily.
Variables are easy to declare. You have to start the declaration with a dollar sign. Right after the dollar sign, you need to type in the name of your variable followed by the value. You can assign a value to the variable by using the equals sign. Remember to add a semi-colon at the end of our statement. Here’s an example on some variable declarations:
In our echo statements, you can see that we wrote in the name of our variables, the dollar sign included. This means we referred to our variables. This would print out:
[crayon-5bf0779c05851957759198/] When you want to give a name to a variable, there are a few things you need to keep in mind:
- Variable names can only start with a letter or an underscore
- Variable names should be written in camelCase
- Variable names are case-sensitive
- Variable names can only contain alpha-numeric characters and underscores (Wikipedia)
You can do lots of things with variables such as adding them together, subtracting two variables that contain numbers, etc.
Here are a few examples of what I mean:
Notice how I didn’t put quotes in the echo statement this time? That is because we are just printing out numbers. We are printing out the results from adding, subtracting, multiplying and dividing the x and y variables. If we declared a variable containing text, we would not need to put it in quotes inside the echo statement either because we already put the string in quotes when we declared the variable.
This is what I mean:
The str variable already has double quotes so we don’t need to write them in our echo statement.
Experiment with these for a while and see if you can make something cool out of it!
Let’s move on to something called Arrays.
This might be a bit more complicated than what we’ve done so far. But an array is an array. You can define an array of anything. Sometimes, instead of declaring four different variables, you could make an array that contains four values. So an array can hold elements. What is an element? Well, in arrays, elements are individual items inside the array.
Every element in an array has something called an index. This index just marks the position in an array. So, let’s say we want to create an array that will hold four different numeric values. We’ll create an array, we’ll call it arr, and we’ll give it four different values.
Before I say anything else, please try to at least make an attempt at not laughing your a**es off when you see how terrible my painting skills are.
Moving on, we see that the index number actually starts at 0. So our very first element in the array has an index of 0. Since we know its index number, we can then access that value from the array. This is must better than declaring many variables to do the same thing.
I’ll create a quick PHP script where I show you how you can declare an array and how you can access its elements with the index number.
Another thing that’s great about arrays is how you can loop through the elements in an array and manipulate them. You could do this by using a foreach loop. Here’s an example:
You can see some new stuff here. First of all, you see the foreach statement and you also see curly braces.
So, what does everything mean? Well, the foreach statement takes our array and loops through every element in our array and assigns the value of each and every element to a new variable called $val. So, $val isn’t an array but a temporary variable which will hold the value of every element.
You see that we placed some code between the curly braces. What does this mean? Well, the code between the curly braces is what we are going to do every time we assign a temporary value to the $val variable.
So the first time it loops, the $val variable is assigned the value of the first element in our array. So, the $val will hold the number 2. Then, it’ll multiply the $val value with two. Since there’s no more statements inside the curly braces, that means the loop will end there.
Then it’ll continue to loop again and the $val variable will be assigned the value of the second element in our array. And everything will be the same as before. And it stops looping when there are no elements left. So, after we looped through our last element in the array, the one with the value of 11, the loop will stop. Now, other than multiplying, nothing really happens.
Let’s add one more statement to the code which will print the value of our $val variable every time it loops.
So, this will produce the following output:
[crayon-5bf0779c0586a508871691/] That looks pretty strange, doesn’t it? It looks like a large number, but it’s only the value of $val printed right after each other. We can make it look nicer by adding some html to the mix. We’ll use a dot to connect the echo statement. What does this mean?
Here’s an example where I modify the echo statement of our code above:
What we did here was add an extra value to our echo statement. So instead of only printing out the values of $val, it now prints out the values followed by a new line (<br> means new line in html.) So, for every value assigned to the $val variable, it’ll print it out and a new line afterwards.
It’ll look like this:
[crayon-5bf0779c05871462464311/] Looks a bit nicer, doesn’t it? That was the fundamentals of arrays and variables. Let’s move on to the big stuff.
This is the last thing I will cover in this post. Most PHP scripts have functions in them. We use functions to divide the tasks that the script needs to do. It’s like hiring people to do certain things for you that all contribute to the same cause. So, let’s say we wanted to make a very simple calculator in PHP, we could write the following: (Note: I will break down the code afterwards)
Woah, Sander! That’s a lot of new code. Don’t worry, I’ll break it down for ya!
The very first thing we did was declare two variables. We then called a function called add. The add function then gave back the value of the two variables added together,
When we called the function (not for pizza, ha..ha..), we referred to our variables inside the parenthesis. We did something called passing. We passed in the values of our variables so the function could add those values together and give us the result of the addition.
So, when we created our function, we typed in $num1 and $num2 inside the parenthesis. That just means we allow the function to require two variables. The $num1 and $num2 aren’t actually real variables, but parameters. So, when we called the function add and passed in our own values, we basically gave $num1 the value of $x and $num2 the value of $y. That way we can use the return statement to give back the value of the two values added together.
So the return statement basically says, “Here’s the value of 3 and 5 added together. Take care!”. Scratch that last part. If you’re wondering what the curly braces are, it’s pretty much the same as with the foreach statement. Everything between the curly braces is the code that the function will execute.
Although we called the function add, nothing actually happens. This script will produce no output, but we can change that! If we add echo behind our function call, we print out the value that the function returned. So, here’s the whole script:
So, we have gone through a lot. This will most likely be pretty hard to grasp unless you’re a natural, but keep at it and try to learn one thing at a time. If you want to become good, you need to devote a lot of time to programming.
“Computer science education cannot make anybody an expert programmer any more than studying brushes and pigment can make somebody an expert painter.”
– Eric Raymond