Learning PHP - Part 1: introduction
Posted on 2009-10-24 - Comments
So, you’ve got to grips with the basics of HTML and CSS, and now you want to actually make your web page do something? Well, PHP is a great way to do that. In this series of blog posts, I will introduce the PHP language and teach many useful techniques that are used in professional PHP development.
What is PHP?
PHP is a general purpose scripting language. At its most basic level, i t can be used to embed logic into web pages. If HTML is the meaning and CSS is the presentation, PHP is the engine that makes it work. As you will see later in the series, it also has powerful object oriented abilities that help you produce reusable and easily maintainable code. For a detailed history of PHP, see the excellent Wikipedia entry.
What you need to get started
First, you’ll need a web server. You won’t get very far unless you can actually see the web pages you create. Personally, I prefer using Apache – and indeed, this guide assumes you are using Apache.
My preferred platform for the web is Linux – as far as I’m concerned, Linux is the OS of the web. Whilst Windows is an excellent general purpose OS, it has some major differences with Linux that can catch out the unwary. It’s dead easy to either get a Linux-based web host, or set up a virtual machine on Windows.
Finally, you need an editor. Some purists prefer to use simple text editors, the best of which give you syntax highlighting to make your life easier. Personally, I use NetBeans. Handily, NetBeans is available for the Mac, Windows and Linux. I prefer NetBeans because it automatically highlights any PHP errors as you type – an invaluable feature. It also offers some useful advanced features that will appear in this blog later.
By default, Apache will execute PHP embedded into any file with a .php extension. Create a new file called, for example, my-first.php and enter the following:
<?php echo 'My first PHP file!'; ?>
?> are the opening and closing tags respectively.
By default, PHP uses an XML style syntax to denote where your PHP starts
and stops. Anything outside these tags is sent to the browser verbatim.
You could have HTML markup outside these tags for example.
Anything within the tags is intercepted by the PHP engine before the page is sent to the browser. In this case, the ‘echo’ causes the content inbetween the single quotes to be sent to the browser. Note the semicolon at the end of the line – PHP uses the semicolon to signify the end of the line. It’s a very common mistake to leave out the semicolon, which is why using a good editor like netbeans will highlight the mistake for you.
To run your first PHP script, you’ll need to put it on your server. This
will mean copying it to your document root, or uploading it using FTP.
Once the file is in place, you should be able to run it by going to
http://localhost/my-first.php if it is on your local machine, or
http://whatever-your-host-is.com/my-first.php if you’re hosting it
If all went to plan, you should see a blank page with the text ‘My first PHP file!’ on it. It’s not much, but an important first step into the world of the dynamic web.
- Next generation console wishlist 2013-02-18
- Digital conversations preserved 2011-02-21
- Pastel de Nata recipe 2011-01-10
- Fixing a bricked D-Link DSL-G624T 2010-01-22
- Learning PHP - Part 6: functions 2009-11-03
- Learning PHP - Part 5: your first dynamic web page 2009-11-03
- Learning PHP - Part 4: controlling flow 2009-11-02
- Symptoms of a Wordpress hack 2009-11-02
- Learning PHP - Part 3: array basics 2009-10-26
- Learning PHP - Part 2: variable basics 2009-10-25
- Learning PHP - Part 1: introduction 2009-10-24
- Unsetting HTTP headers in PHP 2008-08-06
- Intermittent 1px gap in Firefox 3 2008-07-30
- Understanding Linux file permissions 2008-07-29
- Step by step: Moving code between Subversion repositories 2008-07-23
- Novell client on OpenSuse 10.3 2007-10-08
- Removing Windows from Apple's Bootcamp 2007-10-01
- HTTP authentication in PHP 2007-06-12
- Microformats and me 2007-06-11