PHP is the hottest scripting language around - and with the release of PHP4, more and more developers are looking at it as a rapid Web development tool. This new series of tutorials is aimed at getting novice programmers up to speed on the language, and the first article covers variables, operators and the include() function call. Exploding chewing-gum is optional.
Once you've configured your Web server to parse PHP pages, it's time to test it and see if everything's working as advertised. The simplest way to do this is to pop open your favourite text editor and create a file containing these lines of code:
<?php
phpinfo();
?>
Save the file with the extension .php - for example,
"test.php"
Now, start up your Web browser and point it to the file you just saved - for example, http://localhost/test.php - and you'll see a page filled with what at first glance appears to be gibberish, but on closer inspection will reveal itself to be a list of internal PHP variables. The values of most of these variables can be modified by altering the "php.ini" file that ships with every distribution of PHP. For beginners, the default values are more than sufficient to work with.
There's one essential concept that you need to get your mind around before we proceed further. Unlike CGI scripts, which require you to write code to output HTML, PHP lets you create embed PHP code in regular HTML pages, and execute the embedded PHP code when the page is requested.
These embedded PHP commands are enclosed within special start and end tags - here's what they look like:
<?php
... PHP code ...
?>
or the shorter version
<?
... PHP code ...
?>
Here's a simple example which demonstrates how PHP and HTML
can be combined:
<html>
<head>
<title>Bonding With PHP</title>
</head>
<body>
So who do you think you are, anyhow?
<br>
<?php
// this is all PHP code
echo "<b>The name's Bond...James Bond!</b>";
?>
</body>
</html>
And if you browse to this page through your browser and take
a look at the HTML source, this is what you'll see:
<html>
<head>
<title>Bonding With PHP</title>
</head>
<body>
So who do you think you are, anyhow?
<br>
<b>The name's Bond...James Bond!</b>
</body>
</html>
Every PHP statement ends in a semi-colon - this convention is
identical to that used in Perl, and omitting the semi-colon is one of the most common mistakes newbies make. It's also possible to add comments to your PHP code, as we've done in the example above. PHP supports both single-line and multi-line comment blocks - take a look:
<?php
// this is a single-line comment
/* and this is a
multi-line
comment */
?>