Vitenka: 1527
I made this / Cat Games - Less lag, more 'thats bad'
[vitenkas.morat.net]
/ MORAT / freespace / Catnews / Editorial
There's nothing to see here except for shadows of the past - and these ones won't be returning.

I'd point you to my next project here - but I'm not that organised. My style is to act and then sort out the consequences, rather than the other way around. Oh, and lying. I do that a lot too. (i.e. if you look closely, you may have seen some links appearing roughly once a week)

Vitenka.com is registered to me for the forseeable future, so you might find something there.

Edited by Vitenka at 2003-04-09 08:22:54

 
Vitenka : Tue 5 11:07:08 2001  
... the first part of a series to explain how to make your clan site pretty ...

Php Helps Programmers

Some of you seem unable to comprehend the MORAT news system. So we're going to explain it all, from the very beginning.

First of all, some stuff you need to know about URL's.

Index dot what?

If you type a url, say http://freespace.morat.net/Vitenka/ that ends in a directory, then the web server will try to give you the index page.

In no particular order (well, there is an order, but it's different on different servers) it will try index.html, index.htm, default.htm, index.cgi, index.phtml and so on.

You want to use index.phtml and to make sure that there are no other .htm's lying around. If you want to use php then call your files .phtml

Client / Server

This is going to sound pointless and technical, but it's important. Skip over it, then don't blame me when you don't uderstand something later.

Web browser forms request. That's a URL, some form data, maybe some cookies. It sends it to server. Server eats it. Server runs some code - server then sends the file to the client. The client (your web browser) then eats it, maybe runs some code in it, interprets the html, and displays it.

PHP runs on the server. Javascript runs on the client. This is important.

My first php program

Here we go:

<HTML>
<?php print ( "Hello World\n" ); ?>
</HTML>

Type that into an empty text file, and upload it to some webspace that runs php (MORAT is fine) with a sensible name (call it tmp.phtml)

Browse across to it, and you should get a little page that says 'hello world'

Wasn't that thrilling? If you like, you can skip ahead to something more exciting. Otherwise...

Explaining it

Well, the <HTML> you should already know about - that's just to tell the browser that it's an HTML file. Then, as you might expect, the <?php ... ?> tag says 'this is a bit of php' - and inside that is the bit that does the work. And what thrilling work it is. It 'print's 'Hello World'.

An important thing to understand is that it literally prints this into the file that gets sent to the browser, replacing the php tag entirely.

Unlike javascript, people can't read your code and figure out how it works.

One big advantage of this is that you can print out html tags in there - and they will work just as if they were typed into the document there.

The '\n' is code to say 'print a carrige return here. Note that this just gets printed to the source code - to put a newline in the output the user sees you have to use <BR> just like in normal html.

Something More Interesting

Let's see what we can do about getting a little input.

Take your original file, and edit it to look like this:

<HTML>
<?php 
      print ( "Hello $user_name" ); 
      print ( "<BR>" );
      ?>
<FORM> method=GET action="">
<INPUT NAME=user_name size=40 maxlength=800 VALUE="">
<input type=submit value="Try It">
</FORM>
</HTML>

Then upload it again, and try it out.

Now - isn't that nifty? Type your name into the box, submit it, and it does what you expect it to. And yes, you can make it say rude things.

Don't you dare skip past the explanation this time.

Explaining Input

We'll start this explanation off from the middle. You type your name into the form, and hit the submit button ('try it').

The text field in the form is named NAME=user_name - you will see it on the end of your URL '?user_name=whateverYouTyped'

This gets sent to php as a variable (I'll explain them later, for now think of it as a magic box) name $user_name (the $ is what is technically referred to as 'syntactic suugar' - think of it as magic that php wouldn't work without)

Then the php prints it out. The $ tells php, cleveraly, to not literally print out 'user_name' but to print out the name that you just put in it.

I also decide to print a linebreak here. No real reason, just showing that you can have multiple prints. I could just as well have printed it by putting it outside the ?> - or I could have printed the entire page (right down to the <HTML> from inside the php) The choice of which to do is mostly made on the basis of which looks tidier.

Anyway, then it prints out the form, ready for you to use it again.

Oh. To show how it prints out as source code, not just to your screen, try putting some html in the box. Maybe <UL><LI>

Variables

Variables are called that, bcause they can change. Whereas "Hello World" always gets printed out as "Hello World", $user_name can come out as "Fred" "Barny" "Wilma" "3.1415926" or anything else you can think of.

We've seen two things you can do with variables already. You can grab them from the user, and you can print them out.

Other things you can do - you can do calculator type stuff with them. Add them together, subtract them...

<HTML>
<?php 
      $a = 4;
      $b = 6;
      print ( "A = $a <BR> B = $b <BR> \n" );
      $c = $a + $b;
      print ( "C = A + B = $c <BR> \n" );
      $a = $b - $c;
      print ( "now A = B - C = $a <BR> \n" );
      ?>
</HTML>

Ok, so that's pretty lame and boring.

Wrap up

In fact you can't really get anywhere with this until you try doing different things with your rogram depending upon what's in the variables.

Which will have to wait till another column, because this one is too long already.

Until then, why not try making a little shopping list calculator type thing?

You know - tell it how many apples at what price, how many whatever elses, and it tots up a total.

You should have everything you need - you just need to add more input's to the form (remember to give them different names) and then multiply and add them together.

Edited by Vitenka at 2001-06-05 11:10:33

Older News  
Main News>>>
[M]
[Haiku]

[Rifle]
[MORAT]
[Fa-Teen]

 
[Froody Comics]



[Nifty Links]
[Editorials]
[Guilt Box]
You owe:
00:04
 
['Tenkas Tips]

This HTML design by Vitenka
I'm aware it sucks, but am also too lazy.
Note that now you've changed all the colours, it could be about anything!
BTW - this site looks fine in IE5 and netscape4. If you have a problem, if no one else can help, and if you can find it, maybe you can use - Netscape 2.