Writing PHP code is very easy. You don’t need specialized software to do this, all the tools should be found in a new installation of your operating system. Let’s start off with a little script and analyze its contents. PHP files are made up of plain text, just like a HTML document. So open your favorite text editor, and type the following:
print "Hello Web!";
Always remember to save PHP’s files with the extension ".php" – this is very important, because it tells the server to how to treat these files, and run the appropriate interpreter to "understand" their contents. So just go ahead and give it an easy-to-remember name like "hello.php". If you run the web-server on your own computer, copy the file to the root location of your web-server – you can find this in your web-server’s documentation. If you’re not running the server on your computer, then you must upload the file to your server. You can do this using a FTP client. After you’ve put your script where it should belong, you must open it via your web-browser, so point your web-browser to the path of the script according to your server’s address – for example, if you’re running a server on your own computer, then just load into your web-browser the file http://localhost/hello.php
If everything goes ok, then you will see "Hello Web!" (without the quotation marks) in your web-browser’s output window. If PHP is not installed or the server didn’t recognize the file’s extension, you will see the source code of the script (the script itself).
While the above script is pure PHP, you can incorporate it into a HTML document. So go ahead create a file named "hello2.php" under your web-server root directory with the following content:
<title>PHP Test 2</title>
<?php print "<b>Hello Web!</b>"; ?>
You can easily see now that PHP only parses the text between the PHP tags ("<?php (…) ?>"), and replaces this text with the output of the script. The rest of the script is left alone, and is forwarded as-is to the web-browser. This way you will be able to combine both HTML and PHP in the same page. This is how the file will look like after being parsed by PHP:
<title>PHP Test 2</title>
As you can see, incorporating HTML into a PHP document is simply a matter of typing in the code. The output of this last script will be pretty much the same as the first one, except that the new document will also have a title: "PHP Test 2", and the welcome message will be written using bold characters.
You can include as many blocks of PHP code as you need in a single document, combining them with HTML as required. Although you can have multiple blocks of code in a single document, they combine to form a single script. Anything defined in the first block (variables, functions, or classes, for example) usually will be available to subsequent blocks.