PHP Basics : Introduction To PHP
Before we doing any coding, let’s talk about what PHP is. PHP (Hypertext Preprocessor) is a widely used scripting language that is designed for web development and can be embedded directly into HTML.
One of the strongest features of PHP is its ability to connect to a wide range of databases. Those features will be touched on in later, more advanced tutorials.
This tutorial and any further PHP tutorials will assume that you will be using a server that supports PHP, and that you know HTML. If you don’t know HTML then I strongly recommend not reading this tutorial until you’ve learned it.
Now let’s begin writing your first PHP script.
- Note – There are several programs you can use for writing PHP, but if you don’t have a specific program such as Adobe Dreamweaver or Panic Coda, you can simply use your computer’s most basic text editor.
First off, create a new file and save it as helloworld.php. Yes the infamous ‘Hello World’. If you don’t know what I’m talking about and plan on learning other programming languages later, then get used to seeing ‘Hello World’ as your first tutorial.
I’ll start by showing you the code, then explaining it piece by piece after you’ve looked through it. I’ve found that’s the best way to work with stuff like this.
Yup, that’s it.
We start by creating a blank html document with all of the standard tags. Only one thing, there’s something new in the body tag.
Whenever you write PHP code, you have to start with “<?php” and end it with “?>”. This lets the server know where the PHP code is within the HTML file.
- Note – “<?php” tags can be placed anywhere in the HTML document.
The only line within the PHP tag is “echo ‘Hello World’;”. The ‘echo’ command tells PHP to write on the screen whatever text you put within the following quotes.
- Note – You can use either single quotes or double quotes for ‘echo’, but whichever one you open with, you must close with as well.
- Note – You can also put HTML code within the quotes following ‘echo’. This will become very helpful later.
The last thing in the line is the semi-colon, which has to be placed at the end of every PHP line, to let PHP know that the command is done, and to move on to the next line. Without the semi-colon, the page would result in an error.
Here’s what your page should look like after opening it in your browser :
That’s really all there is too it. Thanks for taking the time to read this tutorial and I hope you enjoyed writing your very first PHP script. If you have any questions, please comment below.
- PHP Basics : Variables
- Easy PHP Guestbook Part 5 – Retrieving from the Database
- Easy PHP Guestbook Part 1 – Does my server meet the minimum requirements ?
- Easy PHP Guestbook Part 3 – The Form
- Easy PHP Guestbook Part 4 – Submitting to the Database