1. Computing

Getting started with PHP

By

So you're completely and totally new to PHP, and you aren't quite sure how to get started. Well don't worry, we'll be gentle with you. To start out you're going to need some sort of plain text editor. This can be a program you downloaded on the internet, or it can be something like Notepad (PC) or TextEdit (Mac) that comes free with your computer. Most tutorials also recommend you need a basic grasp of HTML, but what you need to know you can learn on the fly, so although it is helpful, it's isn't strictly speaking necessary to get started.

Let us first start with the most basic and traditional of programs, every language's first shout out, the "Hello World".

Start your program with an open PHP bracket. It looks like this:

<?php

Next we are going to add the code that actually says "Hello World" in our program. There are two ways you can do that, one is print and the other is echo. Here is an example of how to use print:

print "Hello World!";

And here is an example of how to use echo:

echo "Hello World!";

As you can see each starts with PHP code (print or echo in this case), then has the text we want to display within quotes, and ends with a semicolon (;) which indicates the end of the line.

We then end our code by closing the PHP tag that we opened at the beginning of the program. We do that like this:

?>

So our finished program should end up looking like this:

<?php
echo "Hello World";
?>

You can also add comments into your program using a double backslash. Anything on the line with the double backslash will be ignored when the PHP is run, it is only there as notes for the programmer. Here is an example:

<?php
// this could be print or echo
print "Hello World";
?>

Now, let's write the same program again using a variable! Don't pretend like you aren't excited, I know you are…

First we need to learn how to assign a variable. In PHP variables always start with a dollar sign. So, for example $name could be a variable, but name itself is not. You can then assign a value to the variable simply using the equals sign. So, we can make our greeting a variable like this:

$greet = "Hello World";

We can then use the variable within the print or echo simply by typing it. Let's look at our program again using this new variable:

<?php
$greet = "Hello World";
// this could be print or echo
echo $greet;
?>

Notice that we did not have to put our variable in quotations, however we CAN put it in if we want to. Here is an example of how we could use a variable and text within the same print or echo.

<?php
$greet = "Hello";
// this could be print or echo
print "$greet World!";
?>

But how do you format the text? Well that's where the HTML comes into play. You use HTML to format your PHP, which is why it's recommend that you know a bit when learning PHP… to make it pretty!

The HTML can be placed inside the PHP or around the PHP. I'll give you an example of both. Let's just do something simple like make the text bold. Here is an example with the HTML outside of the PHP:

<b>
<?php
$greet = "Hello";
// this could be print or echo
echo "$greet World!";
?>
</b>

And here is an example with the HTML right inside of the PHP:

<?php 
$greet = "Hello";
// this could be print or echo
print "<b>$greet World!</b>";
?>

Simple right? Just make sure that if you put the HTML inside of the PHP, it's always inside of the quotes of a print or echo!

The above program combines everything we have learned in this lesson. It uses print or echo (you're choice), it contains a line that is commented out, and it uses a variable. It also shows you how to add HTML to your program for formatting!

Now save the program in PLAIN TEXT format, with the .php extension. If you have PHP installed on your computer you should be able to view it, if not, you'll have to upload it to your server first.

Congratulations, you've just written your first PHP program!

  1. About.com
  2. Computing
  3. PHP / MySQL
  4. Learn PHP
  5. Getting started with PHP

©2014 About.com. All rights reserved.