Introduction to PHP for beginners

So you have mastered HTML, CSS, and Javascript. Whats the next step? Sure you can make awesome looking static pages in HTML, but the web is much more than that. The web is ever changing and you need to create you web pages that will compensate for that changing content. Whether it’s a full CMS (content management system) or just a simple form that sends a question to your email, there has to be code behind the scenes making everything work. 9 chances out of 10 this language is PHP; What is PHP? Well it stands for PHP: Hypertext Preprocessed and its a “General purpose scripting language that was originally designed for web development to produce dynamic web pages.” basically it was made to work perfectly within HTML and also uses databases to store information, so it’s the both of both worlds. Let’s get started learning this wonderful programming language.

Testing Environment

As you can imagine, there is a lot more stuff going on behind the scenes with PHP than there would be with just HTML,CSS, Javascript which is built into browser functionality. PHP is a server-side scripting language which requires a PHP server. This could be either on your web server (most paid hosting includes PHP) or you can set one up locally. For this tutorial I will show you how to set up your server on your own computer(locally). To do this we need to download some software.

Mac Users

For Mac users I will direct you to a great program called MAMP (Mac apache Mysql PHP) download and install that, the free version should be more than enough to start out, but if you find yourself needing more the paid version is great.

Windows Users

Windows users may have a little less pretty interface, but it still works! A program call WAMP (Windows Apache Mysql PHP) Just download and install.

I will be doing this tutorial on a Mac so everything will be in MAMP but everything is very similar in WAMP. For both, make sure that your server is up and running. To do this on MAMP click on the app (the one that looks like an elephant) it will ask for the admin password, then you will be connected a web page will open confirming it’s success. With WAMP click on the icon, it will place an icon in the bottom right bar by your time (the icon looks like a meter of some kind). Click on this icon and select start all services. For more information on how to use WAMP check this out WAMP Docs. Now that we have our servers running it’s time to get started with the fun stuff, Writing the PHP.

Bare Bones Basics

This section I will talk about the very basics needed to get your first PHP application going. If you have already seen php code before than you will recognize some things, but go ahead and read over the information because if you mess up these very essential pieces of the PHP picture than nothing you write in PHP will work. So pay attention and you will be fine.

This is what you will use to start and end every bit of PHP you will ever write, Also you can use shorthand tag (sometimes it needs to be enabled on your server to be used):

Cliche Hello World

What kind of tutorial would this be without a Hello World statement. I will show you how to display hello world on your screen, Two ways!

This is the echo statement, you should get used to this on as you will use it a lot throughout your PHP developing career.

The second is print, which basically does the same thing as echo but it can return an true or false boolean value, which you don’t have to worry about just yet. All that is important right now is that you can use either to display something on a webpage.

There you have it, your first PHP script! That wasn’t so hard was it? Let me take you through our tiny script and explain to you what is happening with it. first we opened the PHP tag

Commenting Your Code

One of the best Practices you can get into right from the start, is to comment out your code. You may know what you are saying when you are writing it, but what about when you have to edit it a few months later? You may not have any idea what was going through your head at the time. That is why it’s great to comment your code! There are a few ways to add comments in PHP, Let me show you how.

Intro Into Variables

Variables are found in many different programming languages so if you have some experience in Javascript then this might not be a hard concept for you to catch on to. Basically a variable holds a value, it can be a string like “Hello World”;, integer like 4 or it could be another variable. In PHP variables are defined by the $ sign, and are case sensitive.

Some examples of valid variables.

Some Invalid variable Names

This leaves you with lots of choices to name your variables, here is an example of how to use them.

I am not going to get into this to far until next time but you can do math using variables. Use this example to create you own mathematical equations

I will cover more math in the next tutorial but this will give you a nice example of what you can do with variables. Variables are one of the most important building blocks of PHP, and you will soon become very comfortable with them.

Conclusion

This has been a simple introduction into the world of PHP, and let me tell you we have just scratched the surface of what you can do with this very dynamic programming language. Next time we will get into more in depth examples and also some more core functionality of this great language.

Darren Nickerson the author

Darren Nickerson is a 24 year old web designer & developer from Prince Edward Island, Canada. He has been in the web community for over a year now, and specializes in WordPress, HTML, CSS and PHP. You can follow him on twitter @darrennickerson
  1. NoeG says:

    Nice thanks Darren! I’m about that stage of my learning curve I’m currently learning javascripts on lynda.com and its alot harder than xhtml/css but I kinda dont think I’m gonna use too much javascript in my projects so would it be a good idea to skip it and learn php? or is it best to learn first before php?

  2. It’s good to have some background in javascript or jquery before you start to learn PHP, but of course you can learn PHP without any javascript knowledge. The learning curve will steepen without it because a lot of the same principles are applied to both. variables, if/else statement ect. I would recommend getting some background first with javascript anyways as it always will come up in your web sites anyways

  3. Trevor says:

    If you can program in any language, and so it seems, so far, its a slight change in syntax, and the keywords may be the same, but have different applications. Learning how to use the language is only the first step, how to achieve what you want your program to do, is another thing. Thats what makes programming such a challenge. Whens the next lesson coming?

    • Thats very true, i am positive if you can learn one programming language you can learn them all. And learning the language and and implementing it is a whole different storey. I hope to have the next part of the tutorial on the go soon.

Leave a Reply