PDA

View Full Version : First PHP Tutorial



stuffradio
October 17th, 2006, 23:51
Hey, this is the first tutorial I've made... so what do you think?


--------------------------------------------------------------------------------

Part 1:Intro to the tutorials

Hello reader,

I'm assuming you're reading this because you have seen a vast growth on the World Wide Web in the development of Dynamic Websites. These websites could be used to house Forums, CMS's(Content management Systems). By the end of these tutorials you should be able to make your own mini CMS, or be able to modify the vast majority of scripts out there. Since I can't guarantee that everyone uses the same method of making their website as I do, you'll have to read up on the docs they provide... or just study the code yourself.

One requirement for learning PHP is knowing HTML. If you don't know HTML you can go to http://www.htmlgoodies.com/. You'll need HTML to do things like, write your own Forms, Tables for layouts or Div whichever one you prefer to use. Javascript is also a plus for some websites.

I started my website creating career with HTML. The way I learned HTML was by looking at HTML code, erasing some of it, also retyping it in a separate document, saving the file at arbitrary times and seeing what the results looked like. Then I memorized all the commands, few years later I was thinking to myself..., “man, I should learn PHP so I can write my own websites and make CMS's, etc.” I got on the net, kept at it reading tutorial after tutorial. Here I am today teaching others how to write in PHP.
Enough talking, lets begin the first lesson!

Lesson 1: Variables

In PHP, there is a method of storing information where you can access things you stored in any page you write on the website. There are different types of variables. Some are the following:

Static Variables: $variable = “This is a static variable”;
Dynamic Variable: $variable = $statement[table_row];
Global variable: global $variable; $variable = “something”;

Note: In Dynamic Variable you see, $statement[table_row];. That is a method of getting information from a mysql database. You'll learn how to connect and access the database/modify it later on.

OK, you've read all this information, and now want to get something down and see some results! You're going to need some hosting because in PHP you can't just double click a php file and see the results on the fly. You'll either have to setup Apache/PHP on your windows computer at home, or you will have some remote hosting with FTP. The program I prefer to use for coding all my websites in is:
Notepad/Wordpad. I prefer Wordpad because I like the way the text looks, but there really is no difference between the two. In Wordpad when you save the .html or .php files you need to put a quote in front and after your filename otherwise it'll add a .txt to the end of your file. In notepad you shouldn't don't have this problem.

Here is the first script of the tutorials!

Index.html:
<HTML><Title>My First Script!</Title>
<body>
<form action=”calculate.php” method=”post”>
<table cellpadding=”0” cellspacing=”0” border=”0”>
<tr>
<td>Math variable a:</td><td><input type=”text” name=”a”></td>
</tr>
<tr>
<td>Math variable b</td><td><input type=”text” name=”b”></td>
</tr>
<tr>
<td colspan="2"><input type=submit value=Calculate></td>
</tr>
</form>
</table>
</body>
</HTML>
Ok... so in this one script you'll notice a bunch of things. You can see that we've created a form inside of a table. We made a column with Math variable a, and math variable b. The input boxes both have names, which is how we get the data on the second page. The name of the input boxes are just like making variables in php, except it's in HTML, and it's kinda dynamic because it can be anything! So what you need to do now, is make a second script called:

calculate.php

<?php
$a = $_POST['a'];
$b = $_POST['b'];

$math = $a + $b;

echo $math;
?>

That's it! Now you have a script that lets you enter any two numbers in to the input boxes on the html page... have it submit a form to the php page, and you retrieve the information, add them together and echo the output.

You can find what it should look like at:

http://www.wuensche.ca/tutorials/Mathcalc/index.html
http://www.wuensche.ca/tutorials/Mathcalc/calculate.php

Exercise:
Write a form that asks for your details, then have it echo the information on the other page you have the form submit to.

All questions should be emailed to carl@wuensche.ca
Copyright ©2006 StuffTutorials Original owner/writer: Carl Wuensche

Darko
October 18th, 2006, 00:07
well, first you should that any "first tutorial into a programming language" should start with a Hello world code, cause don't you think that someone who has no experience at all, will feel little confused with variables.You should have the newbie, outputing stuff, like words, cause that will make him more intersted. As for the tutorials it's pretty good, and detailed, so good job

stuffradio
October 18th, 2006, 00:10
True... but I want to be different from everyone else ;)
I think the end user would feel more satisfied with a calculator script then a hello world script.. don't you think?

Fried
October 18th, 2006, 02:37
I agree stuffradio, but you still need to at least tell the user how to display text on the page... If they don't even know how to do that then how will they know how to do anything?

Wojtek
October 18th, 2006, 04:23
$text = "The result is: ";
$math = $a + $b;

echo $text;
echo $math;

[JSH]John
October 18th, 2006, 08:01
Nice tutorial, glad that it's not one of those hello world ones, they started to bore me :P

blueonline
October 18th, 2006, 10:16
very good tutorial
i think lately u can teach me some more lessons :)

stuffradio
October 18th, 2006, 12:12
Wojtech to get it on the same line you could also do,



<?php
$text = "The result is: ";
$math = $a + $b;

echo $text.$math;
?>

That's called concatination or something like that, you seperate the variables with a period and it puts the output on the same line ;)

krakjoe
October 18th, 2006, 12:17
<?php
$text = "The result is: ".($a + $b)."";
echo $text;
?>

would be my preferred way of doing it, you need to wrap the vars in () to tell php to do that calculation before it echos anything......

nice tut ...

also. the output would have been the same no matter which way it was written in code, without the use of <br> or \n + <pre>

themoose
October 18th, 2006, 12:28
A dynamic variable isn't always mysql, in fact, most often it's just an array.


<?php
$array = array("john" => "17", "herbert" => "28", "sam" => "3", "peter" => $somevar);

echo "Sam is $array[sam] years old.";

Keagle
October 18th, 2006, 12:31
Wow, thanks :) I'm 'trying' to learn PHP, and nice tut :P

Wojtek
October 18th, 2006, 15:29
Wojtech to get it on the same line you could also do,

[PHP]

That's called concatination or something like that, you seperate the variables with a period and it puts the output on the same line ;)
It's Wojtek
and you wrote a first-time tutorial. No need to confuse the newbies with the dots combining the vars ;)

Save that for your mortgage calculator script tutorial :)

Darko
October 19th, 2006, 15:15
i think the next tutorial should be about if statements, do something simple

stuffradio
October 19th, 2006, 16:19
Yeah thanks, I was already planning on doing that ;)

toffe92
October 29th, 2006, 03:34
<?php
echo "sam is 13 years old";
?>