Beginner’s Guide to PHP – Part One

First things first, we might as well get out of the what PHP is not:

  • PHP is not a replacement for HTML.
  • PHP is not a synonym for MySQL. Although they work well together, they’re not the same thing.
  • PHP is not all about layout-based includes.
  • PHP is not magically going to make your website better, faster or more entertaining.

That said, PHP is lots of other things. It’s a powerful scripting language that can power anything from a full-featured, whizz-bang-whollop content management system for your weblog entries and articles to a simple poll asking visitors which football team they support. It’s flexible, relatively easy to learn and can provide hours of fun (well, if you’re into that kind of thing).

PHP stands for “PHP: Hypertext Preprocessor”. Apparently it’s a recursive acronym, i.e. an acronym that includes the acronym in its definition. I think that’s a bit whacky personally, but there we go. PHP is a scripting language — don’t call it a programming language because you make “real” programmers mad. I’m led to believe its syntax is based upon other languages like Perl and C, but because I’m far too lazy to learn those you’ll have to just nod and agree at this point.

Checking for Support

Before we even get into the actual code though, you need to check that your server supports PHP. If you’re already running something like WordPress or one of my fantastic scripts then yes, your server supports PHP. Remember though: unless you have a special server application installed on your computer, you will not have offline support and will need to upload anything you create. To check for PHP support create a file with a .php extension — anything.php, boobies.php, test.php — something along those lines will do. Inside the file, type:

<?php
phpinfo();

?>

Upload the file to your web space and then visit it in your browser. If you get a page with lots of purple-background tables on, stuffed full of information, then your host supports PHP and you can continue. If not then this tutorial/article is not for you! (Sorry.)

The Basics — Understanding Terms Used

There are a lot of common terms/words that people use to refer to specific parts of PHP (and other) scripts and for someone who’s never programmed or scripted before these can be confusing. Learn these first and it’ll help prevent problems later on…

Statement
A statement is a line, or series of lines, that instructs PHP to do something. It’s usually these series of lines that make up a script.
Data Type
Easy peasy: a type of data! Not hard to grasp that one.
String
A string — one of four data types — is a series of characters. Something as basic as “hello world!” or something as complex has “jemjabella.co.uk is the best website in the whole wide world and jem really rocks”.
Integer
Integers are another data type. An integer is a whole number: 5, 83, -1204. Integers do not include fractions or numbers with decimal points.
Floating point number
Another data type. The floating point number is similar to an integer but must contain a decimal point. For example, my IQ: 187.45921 or my brother’s IQ: 0.9134. I’ll leave guessing which brother up to you…
Boolean
The last data type (and often the most useful) a “boolean” is a TRUE or FALSE value (sometimes represented by 1 or 0, or NULL for false). Boolean values can only be one or the other.
Variable
A variable is like a little “store”. Variables all sorts of data of the types mentioned above. Variables are very important, and we’ll learn more about them shortly…

Variables — The Brain Cells of PHP

I like to think of variables as the brain cells of PHP because they store the data that is key to making our scripts work (like our brains store our knowledge). Variables can be added together (if they contain numbers), edited, joined together — all sorts of fancy crap.

Variables generally look like this: $variable_name. They start with the dollar sign, they can contain letters, numbers and underscores (but must not start with a number) and can generally be any length (although a short sensible name will be easier to remember later on than a long random one). One thing to watch out for with variables is that they are case sensitive: $vArIaBlE, $VARIABLE and $variable are three different variables.

Variables are created simply by assigning data to them. Data can be any one of the types I briefly mentioned before. Let’s look at some examples of creating variables:
<?php
$name = "Jem"; // this is a string
$age = 20;     // this is an integer
?>

There are two variables. The string is stored within the quotation marks, and we end the statement with a semi-colon. The integer is not surrounded by quotation marks (else PHP assumes it’s a string). The semi-colon is very important — miss it and you’ll get errors spewed at you! We now have two variables which we can manipulate as we wish (which we will, in the next “lesson”.)

And there you have it: a little background on PHP including the basic terms used and a beginners look at variables. In the next part we’ll take a look at outputting data (including our new variables) to the browser and how to go about it.

php beginner's guideThis article is part of the Beginner’s Guide to PHP series. You can see all the guides or jump to Part Two.

35 Comments

  1. I’m a ‘real’ programmer. At least, that’s my aim in life. I consider PHP a programming language in many respects because it is has the imperative core and works with some of the basic fundamentals involved with other programming langauges such as objects and classes that are commonplace in OOP and languages like Java, and uses familiar C-style syntax. Most people consider PHP a tool for webdesigners. Why, yes, it does make for damn good websites in the way that an interface can be implemented using X/HTML – a way for the user to interact with such scripts – and it makes our lives a whole bunch easier, too. But there comes the issue that PHP should be separated from HTML content (Smarty, I believe, has this in mind) which more implies that PHP is a programming language and HTML just acts like a front-end. You can run PHP from the commandline to solve mathematical problems and suchlike – so in that way it doesn’t differ from the methods of programming that I’m currently using in my Java tutorials. Just because it doesn’t have a GUI doesn’t make it any less powerful as a programming tool. But, the CLI for PHP was created for things like shell scripting, things that would otherwise be done by batch scripts. For example, resizing images in a directory in one fel swoop. But yes, from a programmer’s prospective, PHP is a bit ‘pansy’ – there are much more viable options out there. Cleaner, faster and more efficient languages like Java and C++. Most universities these days teach Java because of the fact it’s ‘internet-ready’ and therefore easier to implement in the growing cyber market – and also for mobile devices such as cellphones and PDAs. However, it too has its niggles. I’m learning all about those recently :D Well, there’s my little lecture for today. I’m sorry it’s not very n00b-orientated so your visitors may be subsequently /dis/orientated by it. I’ll be sitting around waiting to see what else you can come up with in your little guide. XD

  2. Nice and clear, very easy to understand. You rock at explaining things simply and without condescension.

  3. I find PHP incredibly useful when it comes to scripting for sites. I don’t know where I rank it in terms of programming languages; I don’t have enough experience with it to make a valid judgement. Now, if someone were to say Javascript is a programming language, I might have a little fit because I loathe most Javascript that appears on people’s sites.

  4. I have to laugh at the PHP acronym. Apparently, it originally stood for ‘personal home page’. Then when the language took off, a more impressive-sounding name was needed, and ‘Hypertext Pre-processor’ was the closest they could get to the initials. Heh. Interesting article, thanks. I should start to look at PHP again. I did buy a book on it before realising that my then server didn’t support it, but my current server does support it. So I should really start to learn it.

  5. I, too, would consider PHP closer to the programming side of things. Yes, it is interpreted (compiled on the fly) and lets you get away with and ignore lots of things that would wreak havoc in C. But then, Java is also less strict than C and (partially) interpreted. It is designed for use on the web with HTML as the UI, so there are lots of things it cannot do in terms of user interactivity, but it can do all of the OOP stuff that I can actually comprehend (note to self: perhaps I should start using what I do understand in that area). (And, yes, I can only think of may 3 or 5 syntax differences from C — $vars, double quotes, and a few more less common). Not that I’m an expert on what makes a programming language. (On a side note, I’ve recently discoveed that JavaScript and even ActionScript [Flash] are way more powerful than I ever gave them credit for). To stay on topic: You are so much better at explaining how things work — and this looks to be a very good start for the beginner. I particularly like the intro about the !magic. PS — a teacher in one of my classes (it’s basically a survey of web technologies, hence the Flash) asked for some good ‘learn PHP’ web references (that’s how I learned, and I say it’s better than any book if you have the discipline). If you’ve got past the basic programming intro by then (we’ve already covered that with JavaScript), I may have to point them over here!

  6. Thanks so much for this first tutorial Jem. It goes well with my new book on PHP (which I’m actually beginning to understand ;) ) Looking forward to part II

  7. Finally I’ve had different types of data and variables explained to me in a way that I can actually understand. This beginner’s guide is awesome, Jem. I can’t wait for part two! :)

  8. Wow, you couldn’t have chosen a better time for starting a guide like this. :D I just started my IT studies a month ago and this is pretty much how far we’ve covered the basics of any programming (through pseudo code and Visual Basic .Net, so far). I’ve always wanted to learn PHP properly but never did, so this is an excellent kick-start for me. It also supports what we learn here at school. Speaking of which, I better start paying some attention again… In a nutshell: thanks! :) I’m looking forward to the following parts as well.

  9. Jem, you’re my IDOL! :D I can’t wait to second part of your Beginner’s Guide to PHP :)

  10. Easy and just a liiiittlee BIT hard to understand for me since, maybe my age or it’s just that I’m perfectly clueless. D: But this neat, I might even be understanding (even not much) PHP with this. Cannot wait for the part 2! ^^

  11. I’m so happy you explained what it is/isn’t, it really irritates me when people go “lol wtf iframes suck use php instead” -_-

  12. @ Jenny; ofc ECMAScript based languages are powerful, just in a different way to other languages in that they interact with the DOM. :D I’ve had hours of fun with them in the past. But… there are other issues with those, such as accessibility and cross-browser compatibility (since they are clientside) that make them not usually the scripting language of choice for website designs. PHP and serverside scripting ftw.

  13. Well this came really on time. Yesterday I thought (while looking for some PHP scripts) that I have to learn PHP myself too. Waiting lesson 2.

  14. I have just started my voyage of Java discovery having just gone to University. I was hoping we’d cover PHP this year, but looks like I’ll have to wait until second or third year for that. Thanks for the beginnings of whats going to be a fantastic tutorial :)

  15. short and sweet, i’ll see where this goes. @above comments: The main difference in C is that it can directly manipulate memory with the use of pointers etc and it has struct’s etc. Where as PHP all memory is carved out for you and handled by garbage cleanup, sure you can pass by reference etc but it’s not the same as using malloc() and no-where near as powerful. PHP inside CLI is great and hopefully one day PERL shell scripters will realise and give PHP more credit instead of turning their noses up.

  16. Jem

    11 Oct at 4:54 pm

    @Loadx: “short and sweet, i’ll see where this goes.” – you’re free to add your thoughts if you feel I miss anything. :)

  17. I had a nice long entry to post about Java, PHP and C and a comparison that details why PHP could be considered programming since Java is considered programming, and why something that C has doesn’t imply that all programming languages have to have it… but your spam filters didn’t agree.

  18. /me tries to repost in bits @ Loadx – Java uses garbage collection (that is the correct term, not cleanup), too, and it works out quite a bit more efficient, because you don’t have to worry about what expires when and where. Pointers confuse me to no end. The good thing about Java over C is that it does the same stuff, albeit a bit slower, but takes infinitely less time to create ;) (RAD – Rapid Application Development) and since time is money, this is good for businesses employing programmers. Java is becoming a valuable thing to have on your CV these days but if you started off somewhere like C it should be a piece of cake ;)

  19. Hey wait, you were comparing C to PHP… :P yeah, I overlooked the fact that C uses pointers – and also, PHP is weakly typed and dynamic as opposed to strongly typed and static to add some more differences between C and PHP :D In relation to the ‘scripting v. programming’ debate, I have apparently unleashed a holy war by revealing that direct memory access is not a requirement for being a programming language – because Java doesn’t have it. It’s just a case of systems v. application programming. You could use C for systems programming but not Java, nor PHP. So it seems like Java and PHP are almost in the same boat… I think there’s a thin line between programming and scripting.

  20. I’m sending this blog to a friend of mine that was convinced about…a month ago that she didn’t want to replace the HTML. My reaction was not good…I couldn’t really ‘explain’ it, since I was a PHP newbie (still am).

  21. I’m an old-school programmer. I was programming before the web even came along. Back in those days a scripting language was a language that automated existing tasks. It essentially invoked pre-compiled commands with arguments in a specific order. PHP is a proper programming language, so rest easy. Parts of it are compiled at runtime, and parts are interpreted. Either way it’s bona fide.

  22. My knowledge of php is quite little. But after reading that I think it is going to change. There are many people who know those things but not so many who can also explain it so well. Keeping an eye on your future posts :)

  23. Thanks for doing this! Not to long ago (a month back) I borrowed a book from the library that was supposed to teach you PHP in an easy way. But I never got to page 10 because there the author forgot to put quotationmarks in the scriptexample. I could actually see the error myself (Im not very good at php no) before even going in to writing and understanding it. Hah, I returned it the day after *sigh* Im waiting for part two! You are doing me a huge favor! :D

  24. I ended up here due to your boobies.php reference. Gotta love how well that will bring you up in the search engines! :) http://www.vboogieman.com/boobies.php?text=PHP

  25. Interestingly, the creator recanted and stated that PHP can just be PHP, like PERL is just PERL. You may say PERL does stand for something, and you’ll be right, but I’m also right. So that means we are both write. PHP can mean Personal Home Pages, PHP: Hypertext Preprocessor, or just PHP to mean PHP. Me no thinks kachii knows what he/she is talking about. Java is a systems language and not restricted to the web and can also be used on Mobile phones. I would read up more about Virtual Machines and Language Theory, I used to have trouble also.

  26. Jem

    14 Oct at 10:13 pm

    @Jacob: kachii mentions in her first comment that Java can be used on mobile phones. :)

  27. PHP also uses a Garbage Collector, that is the only way it would be able to handle all of the dynamically created data.

  28. Sorry, I didn’t pay attention the name. If this was a forum environment, I’ll be more inclined to go into detail. It appears, um, she is a student. Damn, contradictory paragraphs! I thought I was horrible at clarity, which I am. Yeah, I loved how you print out to the command line in Java. I believe someone created a helper object to encapsulate and make printing using new PrintOut(text). I just like, oh yeah, I’ll add alignment. I did, halfso, it worked only on “the right widths”. Teacher was impressed.

  29. when will you bring out part 2?

  30. Jem, you own my soul. Would you like that “to-go”, or would you prefer to eat it here? ;D

  31. I think this is a generally helpful article. I started out using PHP for includes and progressed to installing scripts. I’ve gotten to the point where I modified some little code snippets to do my bidding and I think I’ll delve deeper and maybe this section will help. The only problem I have with this one is that your definition of terms might be more helpful with the example directly next to it. Having a definitions and example helps to cement it in the mind a little better than defining it and then showing it further down the page for the first time. Just a thought.

  32. Hey – Great post – Can’t wait to check out the rest of your site.

    I’m a big fan of computing languages and, especially, porting between them.

    Thanks for the fine detail. Pretty soon I’ll be adding php to my arsenal :)

    Cheers,

    Mike

  33. […] Jemjabella’s PHP Primer […]

  34. nice :)
    good tutorial :)
    wish i had a good beginner lessons when i started learn Php :)

  35. Very helpful Jem :)
    Now i have a little insight of PHP Scripts