PHP or Hypertext Preprocessor is a multi-purpose scripting language that is used on websites all over the web. Common websites include Facebook, TheTechGame, GameTuts and Se7enSins. Before we can begin, as PHP is a server side language, we need a web server to run the scripts. There are a few choices you have when it comes to this, they are:
- Local Development Environment
- Remote Development
- XAMPP -
- WAMP -
- MAMP -
So, you should now have your development environment setup if your running locally or if you're working remotely, ensure you have an area created for this course. The next step before we can begin writing code is the text editor. Now, there are many options here, a lot of you may want to go with Notepad++, that is absolutely fine. However I will be using Zend Studio for this course, note that it isn't free. If you wish to go with an IDE (Integrated Development Environment) over a text editor, then here are some great free choices:
Hopefully by now you should have your development environment set up and you're ready to start programming. Well, we're almost there, one last thing we need to do is create a folder to store all our work in. Let's do this now. So, open your text editor or IDE and create a new folder titled "php-basics" in the htdocs or public_html directory of your XAMPP, WAMP or MAMP installation. I'll demonstrate with Zend Studio.
First, I need to create a new local PHP project:
For this course I'll be using PHP 5.5, however you can use PHP 5.6. As for PHP 5.4 the only problem you may have is when we build the forum software due to the password_hash function. You can however use the password_compatibility library for this, I will cover this when we get there in later tutorials.
As you can see next to where it says "Location" it is currently pointing to my workspaces folder. As I haven't added a PHP server to Zend Studio, I need to change this to one in our XAMPP, WAMP or MAMP installation. You may have already done this, but I'm going to create this folder when changing the location from within Zend Studio.
I'm going to create the new folder titled "php-basics" now.
Click "OK" and now we have our location successfully set!
So, I'll click finish, and as you can see we now have our folder in the left hand panel.
Now, we can begin writing code, it's about time anyway
So, first things first, let's create a new PHP file called "hello_world.php". The extension .php tells Apache, or the web server that you are running that the file contains PHP code and is to be interpreted. While I'm on the subject, this is what separates a programming language from a scripting language. A programming language such as Java needs to be compiled, whereas a scripting language such as PHP is interpreted at run-time by the web server.
So, you should now have this file, if you haven't already, open it. So, we now have a blank file:
Before we write any PHP, let's turn on our web server. I'm using XAMPP. At this stage we only need Apache, so do not bother starting MySQL.
Now, to the code. We open our PHP tags with
echo 'Hello World';
We end a statement in PHP with a semi-colon, as you can see. Now, let's load this up in our web browser. Navigate to the file. So my path is: localhost/php-basics/php-basics/hello_world.php. The reason mine does it twice is because I created a folder when I was creating a new PHP project. This is due to the fact Zend Studio creates the folder itself Silly me. Anyway, yours should be similar, minus the second "php-basics" folder.
If you browse to this file in your browser you should see the following:
If by any chance you see an error, you may want to check your syntax against mine above. Now, we just outputted a string, and we did so using single quotes. You may be wondering, well can we use double quotes? The answer is yes, but double quotes parse variables which means it will analyse the string for PHP entities, and if found will convert them into their stored value. I will come onto variables and demonstrate this usage in the next tutorial, however if you'd like to try with double quotes, go ahead, however I recommend you stick with single quotes unless you wish to use PHP entities, such as variables in your string.
Now, we can also output integers, we can do this without the quotes, i.e.
and in our browser:
Well, that doesn't look right??? Ah, well PHP doesn't read white space, so there's nothing telling PHP to separate these two onto separate lines. If we want to do that, we can add a break tag, i.e.
echo 'Hello World<br />'; echo 7;
Now, reload your browser, and...
Hot potato pie, it works
Play around with this, and in the next tutorial we'll cover: concatenation, variables and arrays.
Please let me know what you thought, anything that can be improved etc...