Our first script


Part I: MySQL tutorial
Part II: PHP tutorial

Start:



Links:
MySQL Manual
PHP Manual
Student Server Home

Simplest script ever:

hello.php:

<html><head><title>Hello, world!</title></head>
<body><?php
$var 
"Hello, world!";
print 
$var;
?></body></html>

You already recognize a lot of that, because it's HTML! Remember, PHP is embedded, so it exists alongside HTML, although the file is called "hello.php", to let the server know it should interpret the file. Let's look more closely at what's happening with the PHP, though.

First, we see the PHP tags, <?php ... ?>. That tells the server to switch out of HTML mode and into PHP mode, so that it can interpret that part of the document. Inside the tags, we have two statements. First, a declaration:

$var = "Hello, world!";

This tells PHP that we are taking the value "Hello, world!" and putting into the variable called "$var". The dollar sign is used by PHP to indicate variable; something is a variable iff it begins with a dollar sign. The equals sign is called the assignment operator: it assigns a value ("Hello, world!", in this case) to a variable. Note that this line, like every line of PHP code, ends with a semi-colon.

These aren't your math teacher's variables! In most programming, you should think of a variable as a box that can hold a value. At any time, you can put in a different value, or change the value that's in the box. In this case, we've created a box called "$var" and put in it the value "Hello, world!".

Now, let's look at the next line:

print $var;

This line just prints the value of $var. Simple enough.

Let's see what it looks like in action.