May 25, 2011

PHP 101! Hello, World!

Hey there everyone! You ready to get your first lesson in PHP? You bet your darn patooty you are! So you're here, you have an interest in the mystical land of PHP. But first, what in the world is PHP? PHP or Hypertext Pre-Processor is a very widely-used scripting language, practically developed for use with HTML. The meaning is in the name. Hypertext Pre-Processor, meaning that THIS comes BEFORE, THAT. And if you still don't get it: PHP is processed by the server before anything else. This opens up an incredible amount of opportunities for the PHP programmer! Of course this is all post-PERL but that's another story.

We have a very basic knowledge of what PHP is. Now we're going to learn how to use it! You're first program hooray! What you're going to need:

  1. Your favorite text editor(mine's TextMate); Notepad is a timeless classic.
  2. A server running PHP, XAMPP is great if you're into not paying for things!
  3. The ability to learn and study! Woo! Studying!
How about we get started already? I want you to open up your favorite text editor and type in these lines:

What do these do you ask? This is how you declare that you are writing a PHP script. These tags are the most important to any PHP programmer. Without them we are useless and all of our code will show up as text in the browser. Lets start by making our program say something:


Now, I'm not going to assume that you're an idiot. It's clear what this program does, but again we find ourselves asking "but Ditoway, why the hell are you making me do something this simple?" That's easy. This is your first understanding of the syntax of this programming language. First we have our opening tag <?php next comes the most common function in almost every programming language, the PRINT function. You can guess what this does. Yes. It print's the input you give it, in this case we give the PRINT function a string of characters wrapped in quotation marks and followed by a semi-colon. The semi-colon denotes the end of a general program statement.

Now I know at first this is all hard to understand, you'll have questions like "but why the less-than symbols and question marks? what's with the semi-colon". If you're asking yourselves these questions then you need to stop and re-read this entire post. I've explained it already. And if you still don't understand it re-read this fucking post. By now you know what we're talking about. So save that file as anything.php and move the file to your server or run it in your terminal if you know how. But before you do that try and guess the output.

If you got that right you're ready to move on to the next lesson. If you still have any questions leave me a comment and I will answer to the best of my ability. Thanks for reading and I'll see you next time!

11 comments:

  1. AnonymousMay 26, 2011

    best program ever haha i guess everyone knows how to do that

    ReplyDelete
  2. AnonymousMay 26, 2011

    Using print in PHP is bad practice, you should be using echo instead.

    ReplyDelete
  3. I'm aware Anonymous, but for the purpose of the tutorial I chose to use print.

    ReplyDelete
  4. OMGOMGOMG. I'm a mere mortal, so I don't know programming. I'm a chemist. But I took a bioinformatics class last year and had to lrn2 python. I learned a lot, but there's still a whole lot I don't know. I'm favoriting so I can read it when I've got time. Thanks!

    ReplyDelete
  5. I look forward to seeing more tutorials. I took a basic web design course several years ago, but we didn't get into PHP.

    ReplyDelete
  6. Thanks for the information, I always thought PHP was the norm?

    ReplyDelete
  7. Ruby is a lot funnier.

    Textmate is the best text editor I've ever seen. Is like a lvl 85 in a <10 lvl world.

    ReplyDelete
  8. oh jeez my brain hurts from trying to take this all in

    ReplyDelete
  9. This is very confusing to me :s

    ReplyDelete
  10. Basically look at it like this:

    TURN ON COMPUTER
    MAKE COMPUTER OUTPUT: "Hello, World!"
    TURN OFF COMPUTER

    ReplyDelete
  11. got to try this when i get home..

    ReplyDelete