PerlWiz - First PHP Steps

Click here for the perlwiz.biz documentation home page
Contents Prior Page Next Page

This page covers the basics that you need to know to get going with PerlWiz and the PHP language

About PHP

PHP is a scripting language, rather like Perl to look at. It is generally used for generating Web Pages, and has a large following with both home hobbyists and large corporations alike.

It is often used in conjunction with Linux,  Apache Server and MySQL (acronym L[inux] A[pache] M[ysql] P[hp]) not only as an ISP hosting solution, but also within businesses.

Many people find it easier to get to grips with PHP for web solutions than Perl, due to its intuitive use of HTML markup.

PHP pages look like standard HTML pages. Where ever you wish to place a block of HTML that needs to be computed by the server, you put a PHP begin and end tag, and put code written in PHP between those tags to generate the HTML to be placed at that point.

It's more flexible and efficient than JavaScript, because it is generated server-side, and you can place the PHP blocks anywhere in the HTML - not just on page load etc. - and the page just looks like a standard HTML page by the time it gets to the user's browser - so they can never see the code behind the page - unlike JavaScript.

So how does this differ from Perl? Well, the basic difference is that Perl works by having a script that can generate HTML as print statements within the script, whereas PHP works by being a web page that can do processing and generate customized bits of the HTML within the page.

There are pros and cons to either method, and there are differences in the language that sway people one way or the other.

It's also a matter of what you know. For example, if you wish to write scripts to do server-side processing that do not need to generate web pages, then you may wish to use Perl. On the other hand, PHP can be used as a normal scripting language that doesn't generate HTML as well - just make the PHP tag cover the whole document!

It can be very confusing for a beginner. In general, I would advise that if you wish to do primarily Web Pages with some customized sections, then PHP is a good starting point.  If you wish to do complex scripts that may generate web pages as part of their processing, then Perl is probably a better option. I will be screamed at from both camps for saying this though!

Your First PHP Program

Before you start, make sure you have installed PHP in an appropriate place on your computer, and ensure that it is linked into PerlWiz (normally happens automatically).

To create a new temporary PHP script file to do some testing with, start PerlWiz - e.g. with the Default project - and click on the down-arrow to the right of the button on the toolbar.

This brings a list of the types of temporary file you can use. Click on the New PHP Script option, and a new temporary file will be created with the outline of a HTML document and the PHP script start and end tags, placing the cursor at the beginning of the script as follows:-

<html>
<head>
  <title>PHP Title</title>
</head>
<body>
  <?php
    ;
  ?>
</body>
</html>

The script starts within the HTML when you see the tag start <?php

The script ends within the HTML when you see the tag end ?>

You can have as many PHP scripts as you like within the web page.

The command we will issue sends text to the web server - i.e. inserts text where the script is within the HTML page. The is the echo command. We'll start with good old Hello World.

Type the following in where the cursor is:-

echo "<p>Hello World</p>"

This will make the entire program:-

<html>
<head>
  <title>PHP Title</title>
</head>
<body>
  <?php
    echo "<p>Hello World</p>";
  ?>
</body>
</html>

This is no different than just putting <p>Hello World</p> into your HTML page without the PHP tags, except it is actually being generated by PHP rather than being part of your document. The fun bit is where you start doing work to produce some dynamically generated text (but that's another tutorial ! - or look at the PHP help file for more inspiration).

If you click on the Text In/Out tab now, you will see the HTML that is produced:-

Content-type: text/html
X-Powered-By: PHP/5.0.0b3

<html>
<head>
  <title>PHP Title</title>
</head>
<body>
  <p>Hello World</p></body>
</html>

The bit in yellow was generated by the PHP echo command.

The version that shows will depend on the version of PHP that you currently have linked to PerlWiz.

Syntax Checking

Do a syntax check on the program by doing one of the following:-

This invokes the PHP Lint syntax checker, which will look throughout for scripting tags and ensure the code within complies with the PHP language structure. If they do, then a message saying No syntax errors detect in... is displayed in the green messages box at the bottom of the screen.

Let's introduce a deliberate error - the letter h on the end of the echo command:-

    echoh "<p>Hello World</p>" ;

If you now syntax check, you will get a rather unhelpful error message: Error parsing..

In fact, you get a more helpful error message if you try to execute the program by clicking on the Params tab. Try this. You get an error message saying PHP Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING...

If you want to take a copy of the error message to send to someone for support purposes, just click on the error line you wish to copy, and right-click. The status bar will show that this has been copied, ready for you to paste into another application - e.g. an email.

Double-click on this error message to move the cursor to line 7 (if it is not already there).

Take out the h again, and this time when you execute the program, the green portion at the bottom of the screen should be empty.

View Text Output

To reiterate - To try the program and see the results as text output (with any errors going to the green area at the bottom of the screen), do one of the following:-

If you wish to see the program again, click on the Source tab to the left of the text output window.

View Browser Output

To try the program and see the results as browser output (with any errors going to the green area at the bottom of the screen), do one of the following:-

A browser instance is displayed showing the output of the program - which is, in this case the words Hello World.

If you wish to see the program again, click on the Source tab to the left of the text output window.

Saving the program

The program is automatically saved when you try to syntax check or execute the program to text or browser output.

If the program file is called <tempn> where n is any number, then the file is a temporary file, which means that it held in a temporary area, and can be overwritten if it is closed, or if another project uses the file. Temporary files can be included in projects, and are typically  used for trying things out without committing to including the file as a program to be part of an application.  Temporary files are created by Clicking on File and New. They are turned into proper files by using the File / Save As... option or alternatively by holding down the Ctrl key and pressing S to save the file to a proper filename (note that once the file has been saved properly, Ctrl+S will save any changes to the same file name).

In our example, we will save the file in the Projects directory:-

The name on the folder tab for the file has changed from <temp1> to hello_world.php

(c) Copyright 2003-4 Arctan Computer Ventures Ltd.   All Rights Reserved.
If you have any issues regarding this on-line help, please contact the author by clicking here.
Alternatively, you can leave a voice message on +44(0)7050-618-297 or fax on +44(0)7050-618-298

This Page was last updated: 08 April 2004 13:01