Jump into PHP – Installing and Getting Started

Hopefully this series of articles, “Jump into PHP”, will help beginners find their way into the world of PHP, and also serve as a great place for experienced developers to find some great tips to help them out.

This article will cover setting up a PHP server and some of the very basics in PHP, and programming for that matter.

What is PHP?

PHP is a scripting language that can be embedded into HTML webpages to create dynamic web pages. PHP can also be run in a page of it’s own.

When the PHP page is then accessed the server that the page is on will interpret the code. Once the code has been interpreted the page is then displayed to the Web Browser that is viewing the page. PHP can also be accessed through a Shell on a server, or a computer.

As mentioned above PHP runs on a server. Most often a LAMPP stack. LAMPP is an Acronym for Linux, Apache, MySQL, Perl, Python/PHP. Linux is an Open Source Operating System that a Server might run on. Apache is a server, it handles the requests and responses between a client Web Browser and a Server. MySQL is a free Open Source, not for long, version of MySQL. Perl is another scripting language, as is Python the two both run through the Common Gateway Interface (CGI). Also, it uses PHP, which is what we shall be concentrating on.

I hear you ask the question, how do I get all of this onto my computer!? Well, your questions are about to be answered.

Installing PHP

Before getting started with any language you will need to know how to install it onto your computer. With PHP, however, you will need to install a stack of software to be able to use PHP.

There are a variety of different pieces of software that allow you to run PHP on your machine. My personal preference, and the example to follow, will use XAMPP. XAMPP is a piece of software that will turn you personal computer into a server that will allow you to develop in PHP.

If you have a server of your own, having XAMPP set up on your computer as you will be able to FTP your files up to the server and then run from there.

Downloading the files

Head on over to Apache Friends and download the relevant version of XAMPP for your Operating System. In my case I am downloading XAMPP for Mac OS X. There are also versions for Windows, Solaris and Linux. Take your pick!

Fortunately XAMPP have a great set of instructions fo follow when you download the file. You should be taken to them but here is a link to them:

The long and short of it is when you have installed and started up your XAMPP server and typed “localhost”, or “127.0.0.1″ into your Web Browser you should be met with a screen asking you to choose your language. When you are all set up, you should see this screen:

Congratulations, you have set up XAMPP!

Now for the fun parts!

Your First PHP File

It’s now time to make your first PHP file. It is important you know where your “htdocs” folder is. What is the “htdocs” folder? If you are familiar with a web server you will know there is a folder where all of your files go. Well, this is what the “htdocs” folder is. All of your projects will be put in there. So, lets set about finding where your “htdocs” folder is.

Mac OS X

On my Mac I found that a shortcut to my XAMPP folder had been created when I installed it. After a bit of digging around I found the “htdocs” folder was here: “/Applications/XAMPP/xamppfiles/htdocs”.

Windows

On Windows the “htdocs” file will be within the XAMPP installation as so: “xampp/htdocs”. That’s where you put your files.

Linux

On Linux you will be able to find your “htdocs” folder in this location: “/opt/lampp/htdocs/”

Solaris

As with Linux you will find your “htdocs” folder here: “/opt/xampp/htdocs/”

So, now you know where to put your stuff. Let’s get doing some PHP.

Basic PHP

Now you know where your “htdocs” folder is, you can create a new Folder. Let’s call it “myfirstphp”. When you have made this folder type “localhost/myfirstphp” into your Web Browser. You should see this page:

This screen shows you that there is something there. Open your favourite Text Editor / Web Development tool and create an empty file named “index.php”. Now when you go back to that page and refresh the page you will see an empty web page.

Congratulations! You have made your first PHP page. Now lets do some programming. We’ll start our file off with a basic “Hello World!” example.

Here is the code for it:

Every single piece of PHP we write must be written in between a set of tags, in our case we have . If our PHP wasn’t written between the tags the server wouldn’t know that it is dealing with PHP and it would just output the code as if it were HTML. We also have the statement:

This is just a simple piece of PHP to “echo”, as is suggested something to the screen. If you go back to your Web Browser and refresh the screen you should see this:

Yay, your PHP worked. Good job soldier.

Now it’s time to do something a little bit meatier. Have you ever heard of a loop? Not Fruit Loops, or Spaghetti Loops, but a programming Loop. Basically, a loop will repeat a task until a certain condition has been met. Then the loop will stop. Type this code into your program, underneath your “Hello World!” bit of code.

Holy Moly, there are “Hello World!”‘s everywhere! You have just encountered the “For loop”. This cool piece of code declares a variable, “$i = 0;”, $i as 0. Then it says “For $i is less than 10 do the code in between the curly braces”, $i < 10. When the code between the curly braces has been done, add 1 to the variable $i.

We will take a brief look at another type of loop the “While Loop”. This loop is different to a “Foor Loop” as the only parameter in the loop is the condition that the loop will be done for. Let’s add this bit of code to your program:

If you refresh your webpage now, you have a screen with 21 “Hello Worlds!”‘s on it. What does this show you? Ignoring the first “Hello World!” I have demonstrated to you one of the key concepts of programming, the Loop, I have shown that it can be achieved in multiple ways, that achieve exactly the same thing. Pretty neat, huh?

What else have you learnt? Well, not too much really, but a couple more key concepts. For example, you now know how to declare a variable in PHP, “$i = 0;”, you now also know how to increment a numeric variable “$i++”. Also, pretty neat. You also know how to echo something to the webpage the PHP is in, this is crucial for showing users information you have gathered, from a database query, for example.

Other than programming you have learnt a bit about PHP and it’s uses, you have also learnt how to set up a server.

All in all, not a bad first lesson. From teaching myself PHP and from learning on the Job, I think I have covered a couple of important points. Next lesson I plan on covering more programming than this one. Think, Arrays, Functions, If Statements, Return Statements, Switch Statements.

I hope you enjoyed the read! And please, follow me on Twitter.

Welcome to Java – An Introduction

Welcome to “Welcome to Java”, this is part one of a string of articles about Java, what it is and how to use it. I plan on adding to “Welcome to Java” a couple of times a week. One of the articles will act as a write up from a lesson at University, this article for example, the second article will deal with some more complex areas of Java that I have been teaching myself over the past months.

What is Java?

Java is a popular Object Orientated Language, it is similar to C++ but it has fewer low-level aspects than C++, pointers for example. Java is able to run on any computer that has the correct Java Virtual Machine installed. When compared to C++, which has to be compiled for certain types of processors, this makes Java a very appealing programming Language.

C++ is compiled into Machine code that can only run on the type of computer architecture that it is being compiled for. Java, however, is compiled into bytecode which can be run on the Java Virtual Machine regardless of the computer architecture. Because of this a programmer only has to make an application once for it to work on all devices.

Why use Java?

Java has commonly been the language of choice for the implementation of business applications in large companies. There are many reasons why a team of developers would choose Java as the language they wish to develop their applications in.

Easy to learn

Java is a simple language for a developer to learn, especially if the developer has come from working in a C++ environment. This means that is there is a large group of developers working on a business application it won’t take long to get all developers up to a decent level of competency using Java.

Variety of choice

As a business it is better to use an existing library within their application rather than to develop their own library to do what they need. Fortunately, Java has a vast amount of libraries and frameworks developed by other developers that can be used in an application developed by the company. Another benefit to the the Java “Ecosystem” is the amount of different types of IDE’s that are available to developers. This means that employees within the business are able to use an application that they are familiar with to help them to develop the application.

They are just two of many reasons why Java is a good language to use within a business environment. Java also used outside of a business environment. One prime example of this is the Android Mobile Operating system. The SDK provided by Google to assist with the development of Android apps is written in Java. In turn, this means that most Apps written for Android use Java.

Using Java

The basics

As mentioned above Java is an Object Orientated language, so every piece of code must be contained within a class. Below is an example Java application that would print “Hello World!” into the console when it is debugged.

So what does this code do?

The code is all enclosed within a class “ExampleClass”:

This code alone will not do anything. “ExampleClass” will be where we put our “main” method, this is where the initial code will be run from. As a rule, the “main” method within out application should not do any of the hard work in an application, it should solely be used to declare classes and call methods from these classes. Our application does something really simple and outputs to the console. That’s okay.

The main methods should take an array of Strings as commands. This means that when the application is run from the command line it knows what do with itself. I shall cover that in more detail soon.

Here is the “main” in our example application:

That is a pretty basic application.

Download the zip.

I this gives you a good idea of the background of Java, and an idea of the very basics of Java.

A Magic Trick

I am a huge fan of Magic and I like Maths. So when I read about a magic trick that involves a bit of maths I thought it was pretty cool.

When the magic trick is performed our Magician would stack four cards, a heart, a club, a diamond and a spade. All facing upwards at the top. He would then ask a spectator to blindfold him and then shuffle and flip over the deck of four cards. When the spectator has finished manipulating the deck of four cards the magician is able to predict what card is facing upwards.

How does the magician do that?

Well, whilst the Magician is blindfolded he issues a set of instructions that the spectator must follow, but the instructions give the spectator the impression that they actually have full control over how the deck of four cards is being shuffled.

Here are the instructions the Magician gives to the spectator:

  1. Turn the spade (the uppermost card) face down
  2. Move any number of cards, one at a time, from the top of the pack to the bottom
  3. Turn over the top two cards as one
  4. Move any number of cards, one at a time, from the top of the packet
  5. Turn over the top two cards as one
  6. Move any number of cards, one at a time, from the top of the packet to the bottom
  7. Either turn over the entire stack or do not – your choice
  8. Turn over the topmost card
  9. Turn over the top two cards as one
  10. Turn over the top three cards as one

This trick, whether the magician knows it or not relies on a little bit of maths. When the spectator is shuffling the pack, they are actually preserving the properties of the deck of 4 cards that the magician cares about. This is a part of Discrete Mathematics called “Invariance of Properties”.

Here is the example above being solved, in this case, the letters in capital represent upward facing cards, and lowercase represent cards face down:

  • Start: HCDS
  • Step 1: HCDs
  • Step 2: DsHC x2 Cards moved
  • Step 3: Dshc
  • Step 4: chDs x2 Cards moved
  • Step 5: chSd
  • Step 6: Sdch x2 Cards moved
  • Step 7: HCDs Entire deck flipped over
  • Step 8: HCDS
  • Step 9: HCsd
  • Step 10: HDSc

As you can see the the card facing downwards, or opposite, to the other cards is the club. How did this happen?

This Magic Trick can be turned into a mathematical proof:

Prove that for all n >= 0, after n of these shuffles, there will still be one card facing the wrong way from the rest.

Where P(n) is the above statement.

P(0) refers to the pack before any shuffles have taken place, P(0) is true as the first step before any shuffles take place is: HCDs. Which shows that the spades club is facing downwards.

If we assume that m is the number of shuffles made by the Magician we can check to if Proof is correct for m >= 1 so: P(0), P(1).

We know that P(0) is true. How about P(1)? In our case we know that after two cards have been shuffled and then the top two cards have been flipped that there is still at least one card facing the opposite direction to the rest of the pack.: Dshc.

As we have checked this for both P(1) and P(0) we know that all instances of this proof will be true. So: P(1), P(0) … P(m – 1) will all be true.

Now for the shuffle m. There are three different possible endings for the pack of cards.

Case 1

If the mth shuffle cuts the packet, then no cards are turned over, so the one card that was turned the wrong way before this last shuffle will still be facing the wrong way after the shuffle.

Case 2

If the mth shuffle reverses the entire packet, then all the cards are turned over, so the one card that was facing the wrong way before this last shuffle will still be turned the wrong way after the shuffle.

Case 3

If the mth shuffle flips the top two cards as one, then things are a bit more complicated. If the card that is facing the wrong way before this final shuffle is among the top two in the packet, then the mth shuffle creates a new, singe wrong-way card in the same position in the packet. On the other hand, if the card that is facing the wrong way before this final shuffle is among the bottom two in the packet, then the mth shuffle creates a new, single wrong-way card in a different position among the bottom two cards in the packet.

This proves that for P(m) there is a single wrong-way card. This proves that however the pack is shuffled there will be a wrong way card within the pack.

I’ve got a new website!

It has finally happened, after months of me saying I will redesign my website I have finally done it!

My plan is to expand as time goes along, that will include a “Labs” section, as I need some where to showcase what I spend my spare time working on.

My main plan is to work on my Blog, as it will be a great platform for me to write about and demonstrate some of my non web based projects.

As my new site isn’t, and probably won’t ever be, complete I will feel as if I will have to spend time to work on it and improve to the best I can.

I plan on blogging about what I learn at University, what I do at Work, what I teach myself and blog tutorials explaining how to do things that I have pulled my hair out for hours trying to work out, just so you guys don’t have to go through the pain yourselves.

Enjoy it, and I would love to hear your feedback.