PHP
By Web Monkey
Page 4 Your First Script
--------------------------------------------------------------------------------
You'll be glad to know that the really tricky stuff is behind
you. Installation of software is always a black hole because
so much changes from system to system. But with any luck your
database is up and running, and PHP is compiled and installed
with our Web server and able to recognize documents with .php3
extensions.
Let's dive right in and write our first script. Create a
text file containing the following:
<html>
<body>
<?php
$myvar
= "Hello World";
echo
$myvar;
?>
</body>
</html>
Now call up the URL, for instance, http://myserver/test.php3.
You should see a page containing the text "Hello World."
If you get an error message, check the PHP documentation to
see if you set things up properly.
That's it! That's your first PHP script. If you view the
HTML source for the page, you'll see that there is only the
text. Hello World
That's because the PHP engine has examined the page, processed
any code blocks that it found, and returned only HTML.
The first thing you'll notice about the script above are
the delimiters. These are the lines that start <?php.
This indicates the start of a block of PHP code, and ?>
indicates the end of the block. The power of PHP is that these
can be placed anywhere - and I mean anywhere - in your code
in any number of ways. Later we'll see some interesting uses
for these, but for now let's keep it simple. If you wish,
you can also configure PHP to use short tags, <?, and ?>,
but these are not XML compliant, so be careful. If you're
making the switch from ASP, you can even configure PHP to
use the <% and %> delimiters.
Another thing you'll notice is the semicolon on the end of
each line. These are known as separators and serve to distinguish
one set of instructions from another. It is feasible to write
an entire PHP script on one line and separate the portions
with semicolons. But that would be a mess, so we'll add a
new line after each semicolon. Just remember that each line
must end in a semicolon.
Finally, you see that the word myvar
begins with a dollar sign. This symbol tells PHP
that this is a variable. We assigned the words "Hello
World" to the variable $myvar. A variable can also contain
numbers or an array. Either way, all variables start with
the dollar sign symbol.
The real power of PHP comes from its functions. These are
basically processing instructions. If you add up all of the
optional add-ins to PHP, there are more than 700 functions
available. So there's quite a bit you can do.
Now let's add some MySQL to the picture.
next page»
|