PHP Language basics

Language basics – overview

In this session I will answer these questions:
- What do you need for a file to be a valid PHP script?
- How do you print content to the browser?
- How do I store a value and then reuse it later?
- How do I do some basic math in PHP?

What do you need for a file to be a valid PHP script?

Let’s learn by writing some PHP. Type this into a new document:

1
2
3
<?php
echo 'Hi there';
?>

Save it as basics2.php in the root of your web site. Open the file in your browser. The address will vary depending on your setup but is likely to be either http://localhost/basics2.php or http://localhost:8888/basics2.php if you are running MAMP or Universal Server. You should see the message:

Hi there

This is a very simple piece of PHP, but it demonstrates three important things about the structure of PHP files:

  1. PHP files should be saved with the extension .php.
  2. PHP files should start with an opening tag of ‘<?php’ and end with the closing tag ‘?>’
    - there are exceptions but for now we’ll keep it simple.
  3. Each PHP statement must finish with a semicolon. Usually each statement is one line, but not always. We will focus on single-line statements for this tutorial.

To keep our sample code readable I will omit the opening and closing php tags.

How do you print content to the browser?

In our code above line 2 uses the PHP command echo. Echo simply writes data (in this case, the words ‘Hi there’) to the browser.

Echo can do much more than this, and there are many other ways to write to the browser, but one thing at a time!

How do I store a value and then reuse it later?

Let’s change our file. Edit basics2.php and replace the script with this:

1
2
$aPerson = 'Jay';
echo 'Hi ' . $aPerson;

Here line 1 is an assignment: we create a variable called ‘aPerson’ and give it the value ‘Jay.’ The variable ‘aPerson’ is now available to use elsewhere in our script.

On line 2 we echo ‘Hi ‘ to the browser, then we echo the contents of ‘aPerson.’ Using the name of the variable with the $ in front tells PHP to lookup the contents of the variable.

To illustrate the way variables work a little further, change basics2.php to read:

1
2
3
4
$aPerson = 'Jay';
$prefix  = "Screamin'";
$lastName = 'Hawkins';
echo 'Hi ' . $prefix . ' ' .  $aPerson . ' ' . $lastName;

Refresh your browser. Some things to note here:
On line 2, Screamin’ is surrounded by double quotes. The reason for the double quotes is that they allow us to ignore the single quote. There are many ways of dealing with unusual characters and we won’t go into them here and now, but this is a simple example of an approach to a common situation.

How do I do some basic math in PHP?

Create a new PHP document:

1
2
3
4
5
echo '<pre>';
$myNumber = 7;
echo $myNumber . "\n";
echo $myNumber + 1 . "\n";
echo $myNumber . "\n";

Line 1 is there to help make the output readable, so let’s ignore that and skip to line 2. We assign the value 7 to the variable $myNumber. On line 3 we echo the value. Them on line 4 we echo the value but with 1 added to it. Then we echo the value again and note that it hasn’t changed: line 4 hasn’t changed $myNumber!

So what is going on here? Well line 4 is a calculation, no doubt about it. But nowhere has it changed $myNumber. To do that we’d do something like this:

1
2
3
4
5
echo '<pre>';
$myNumber = 7;
echo $myNumber . "\n";
$myNumber = $myNumber +1;
echo $myNumber;

On line 4 we perform a calculation where we add 1 to the value of $myNumber, and then we store the result as $myNumber: in other words we overwrite its value. It is this $myNumber = that is important here.

This post is part of PHP Beginners Tutorial. Go on to the next session: Loops and Conditionals.

24 hr tutorial Hey!
Based on the feedback from readers of this tutorial series I’ve expanded, revised, and improved this brief intro as a whole book. It is much longer, so you get a more in-depth guide to getting started. As a result you’ll write better code. When you buy the book you also get a download password for all the source code from the tutorials. Build a functioning blog from scratch, and other useful mini-projects! Check it out »

No Comments

Be the first to start the conversation.

Leave a Reply

*

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>