Skip to Content

Intro to PHP tutorial

Author: Bill Trikojus

 
1

What is it?

 
2

I’m scared –what does PHP look like?

 
3

Some examples

 
 

*NOTE: This tute is aimed at people who already have a good understanding of Actionscript.

 
1

What the hell is it?

 
 


PHP is a server side scripting language designed for building dynamic webpages. It can process forms, collect data from and insert data into databases and much much more. Server side means that in order to use php on your website, the server that your website is hosted on must have php installed (btw the Swinburne server has PHP installed). PHP is free, open source and supported by every major operating system so its not hard to find a host that has it. PHP does not require the user that is visiting your site to have any special plugins (unlike Flash) –the php script is all processed on the server and what is output is straight html –something any browser can understand.

 
2

I’m scared –what does PHP look like?

 
 


Don’t be scared. PHP and Actionscript were both born out of the C++ programming language and therefore have many similarities.
For example, here is a variable in Flash

i=56;

And here is what it would look like in PHP

$i=56;

So as you can see it's almost identical except for the fact that variables in php must be prefixed with a $. Another thing to note is that PHP is much stricter than actionscript and will fail if you forget to put the ; at the end of each line.

Let's look at a for loop

for($i=0;$i<10;i++){
//do stuff
}

Again, very similar to actionscript (//comments are the same as well)
Some more examples -
Make an array

$names=array("Bob,"Mary","Jane");

and then access an element from that array

$mum=$names[2];

To get the length of the array you use count();
eg

$theLength=count($names);

which in our example would make $theLength variable = 3

At the end of most php scripts you want to output something to the browser, and the most common way of doing this is by using the 'echo' function
For example

echo "Who is your Daddy?";

would output
Who is your Daddy?
to the browser window.

Going back to our array example

echo "My Daddy is " . $names[0];

Would output
My Daddy is Bob
Note the use of '.' - the full stop is used in php to join strings (like the + sign is in Flash)
You can insert php into your website like so

<?
//some php
?>

The php in your website will often appear within straight html content
For example, your page may start with straight html, then a little bit of php to display some dynamic content and then some more straight html to close the page

<html>
<head><title>My php page</title>
</head>
<body>
<?
$a=5;
$b=6;
$theImage="images/test.jpg";
$displayImage=true;
$theSum=$a + $b;
echo "5 + 6 = " . $theSum . "<br>";
?>

Above is a line of text that was output from php.
<br>
<?
if($displayImage){
?>
Here is an image<br>
<img src="<? echo $theImage; ?>"><br>
<?
}else{
?>
I don't want to show you my image
<?
}
?>
</body>
</html>

As you can see, you can jump in between php and html as many times as you like throughout a page.

Anyway I could go on forever but I think we should look at a couple of practical examples.

 
3

Some examples

 
 


Open up testform.php in Dreamweaver and upload the file to your Swinburne webspace (make a sub folder called phptests and put it in there). Test the form online to see how it works and then we'll go through the actual code that's involved.

This is quite a smart form. It detects any errors in the information that the user supplied, displays an error and enters that information back into the form elements so that the user doesn't have to retype everything again. If there were no errors, the form is hidden and a message is displayed.

Now a number of people have asked how to make a site easier to update. The best way that I have found is by using the PHP 'include' function.
For example

<body>
some text and
<?
include 'moretext.php';
?>
and some more text
</body>

This will grab the contents of moretext.php and insert it into the middle of this html page. What this means is that you can break up a site into little parts (header, menu, content, footer etc) and then compile them as the page is loaded. If all pages on your site are getting their menu from menu.php then only one file needs to be updated for all the page change. Like CSS, this saves time and ensures consistent design.

Have a look at the provided 'include_example'. Open main.php and see how the various elements on the site are compiled into 1 html page. Upload the files to see it in action.

NOTE: for this to work your links need to provide a variable that tells php which file to include in the 'content area'.
eg

main.php?page=page1

everything after the ? gets sent to PHP in a $_REQUEST array and it can then extract the value like so

$thePage=$_REQUEST['page'];

You can string together as many variables as you like (within reason)

main.php?page=page34&mood=grumpy&pet=phlappy

OK that's a quick intro to PHP. I hope you can see how powerful it is. I've provided a hit counter example as well so have a look at that to see how PHP can open files, read them and even write to them (note: to do this, the file must have write permissions turned on). You can download these examples here.Your task now is to make a page that includes php. It can be a form, a file uploader, a search engine - anything you want. The more complex it is - the more you will learn.

bulletBack


Share

Like this? Click a link below to share it...


Subscribe and Download

Subscribe to the Swinburne Faculty of Design Podcasts

Post a comment

Garbage posts and SPAM will be deleted.

* Name:
* Email: (will not be made public)
* Comment:
* Reply Notification: