PHP Guide 1: PHP basics for beginners

A guide to getting started with PHP. Learn about the syntax and basic statements and functions.

Index of Contents

»Setting up your server
»Your first steps & the echo construct
»Math (Arithmetic) Operators
»Variables

Setting up your server

In order to start using PHP you will need to have some sort of server with PHP installed. This can be a shared or dedicated web server, or a server set up on your own computer. Two of the most commonly used personal servers are Microsoft IIS and Apache. I'm not going to go into setting these up or selecting them in this guide. I recommend doing a Google search as there are a great deal of resources available for choosing and installing the right web server. Here, use this conveniently placed search box:


Once your have your web server up and running, you will want to install PHP, which can be found on the PHP Official website here. Again, I'm not going to go into the details of installing PHP or what options to select. The latest version of PHP (5.3.0 as of this writing) has made installation much easier as the most commonly desired components are selected for installation by default.

Your first steps & the echo construct

This guide assumes you already have a basic knowledge of HTML and can make a web site. If not, you may want to check out my beginner web site guide first.

Now you're ready to start writing PHP! The first thing you need to know is that PHP files have a .php file extension, and not a .html file extension. You can change a current web page from HTML over to PHP simply by changing the extension. The page should not be displayed differently (as long as your server is working properly), and there does not need to be PHP code in a file with .php extension for it to display properly.

Here is a basic HTML page with some PHP code. Let's call it test.php, although you don't actually have to create it if you don't want to. The "//" sections are comments, where I will do most of my explaining. With PHP, comments are very useful to describe parts of your code. Comments are not displayed when the page is ran (it will not display in the page source either).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Form data</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta name="description" content="Form data">
<meta name="keywords" content="php, get, post, form, data">
</head>
<body>

<?php
// Blocks of PHP code can go anywhere in an HTML document.
// Each block begins with the above tag and ends with the below tag
// Note that these comments are inside a PHP code block.
// If they weren't, they would be displayed as regular HTML.
// Only PHP code that occurs inside code blocks will be parsed as PHP.
?>


<?php
// The first thing you're going to learn is the echo construct:

echo 'Hello world!';

// echo is a construct that writes to the HTML document. In this case we are simply writing Hello world!
// Hello world! is static and doesn't do anything we couldn't accomplish with plain HTML, but later on we
// will learn to write something dynamic with the echo statement!

// Before we do that, let's take a closer look at the code we just wrote.
// In PHP, text that you want to write needs to be surrounded by either single or double quotes.
// Variables do not need to be surrounded by quotes, and neither do numbers (when used as numbers).
// Most lines need to be ended with a semi-colon as well.

// Let's add some more text to our page using PHP!

echo '<br>How are you?';

// We can combine lines together using a period. This is called concatenation. This will come in handy later
// when we learn variables.

echo '<br>' . 'I am doing ' . 'well!';

// Below is an example using double quotes.

echo "<br><br>Some more text!" . "<br>";

// The main difference between the two types of quotes is that when
// using double quotes you will be able to write variables inline with text without having to end quote and
// concatenate. I recommend using single quotes as it allows you to write the $ symbol without PHP thinking
// you're writing a variable, and so that you can write HTML tags using the proper double quote.
// Keep in mind that if you use single quotes and try to write a word that uses single quotes, such as
// doesn't, you will have to use &#39; instead of ', or else it will interrupt your code.

?>

</body>
</html>

If you copy and paste the above document into a PHP file and run it on your server, you will gain a better idea of the concepts I just explained. Try doing that now, and try changing the text, adding more echo statements, and seeing what happens each time you make a change.

Math (Arithmetic) Operators

<?php

// PHP has 5 primary math operators: addition, subtraction, multiplication, division, and modulus
// These should be self explanatory with the exception of modulus. Modulus gives you the remainder of
// a division. For example, the modulus of 5 and 2 is 1, since 1 is the remainder. Here are examples:

echo 3 + 1; // Addition, output would be 4

echo 3 - 1; // Subtraction, output would be 2

echo 3 * 2; // Multiplication, output would be 6

echo 6 / 2; // Division, output would be 3

echo 7 % 4; // Modulus, output would be 3

// When dividing numbers, PHP will automatically give you a decimal number if there is a remainder.

// Notice above that I did not surround the numbers with quotes. Strictly speaking, a number in a programming
// language that is surrounded by quotes is no longer a number, but a string. PHP will, however, still perform
// mathematical operations on numbers inside of quotes, but it is recommended to avoid doing this.
// Here is an example of this:

echo '7' * '3';

// Will have the same output as this:

echo 7 * 3;

?>

Variables

So far we've learned how to write comments, use echo to write text to our HTML page, and perform math operations. Now let's discuss variables.

Variables are a very powerful part of any programming langauge. They allow us to do just about anything. Think of them as temporary storage for data. We can assign any number of things to a variable, and then call that data back up by referencing the variable. We can also dynamically assign data to variables.

<?php

// Let's create a variable. In PHP, all variables must be proceeded by the $ symbol.

$myfirstvariable = 'Hello';

// We have just assigned the word Hello to the variable $myfirstvariable
// Let's use the echo construct to write a variable and some text to the document.

echo $myfirstvariable . ', how are you?';

// The above line will output: Hello, how are you?
// You are using echo to write the contents of $myfirstvariable and then concatenating it with the text.
// Notice that variables should not be surrounded by quotes (unless you are using double quotes as I
// mentioned above), while plain text should be.

// Let's do some math with variables!

$first = 5;
$second = 4;
$third = 3;
echo $first * ($second + $third);

// This will give you the output equivalent to 5 * (4 + 3), or 35

?>

Now go practice with variables some more. When we get into loops, conditionals, and other operations, variables' usefulness will become more clear.

Next Guide: Loops and Arrays

© Nick Vogt 2012