So here you have it, gog's PHP beginners tutorial. Without further ado....
Intro
PHP is a programming language for the Web. Like PERL, its a server-side scripting language, meaning, all compiling work is done by the server and outputted as HTML. So why should you learn PHP? Well, PHP is a VERY easy language to learn, but the concepts behind it are the same for all languages, so an understanding of PHP will lead to a greatr understanding of more chanllenging programming languages. And even a basic knwledge of PHP and HTML can make running your website 200% easier.
In order to execute PHP scripts, you'll need to either:
- Have a webserver and PHP installed on your computer.
- Have a webhost that allows PHP scripts, alot of free hosts (geocities,tripod,etc) don't allow PHP, your ISP webspace might, but it's best to check with a quick call or e-mail to tech support.
phpsu.com
I'll breifly go over the first of the two mentioned above. If you're going to install it locally, ignore the next paragraph.
People that are uploading to a host, jump down to 1.2, throughout the tutorial, i'll assume you're hosting it locally. This really makes no difference, just don't do something silly like when I say go http://localhost/bleh.php, go yoursite.com/bleh.php, and upload after and changes.
1.1 - Installing PHP on Windows XP
Before being able to execute PHP scripts on your box, you'll need some form of webserver installed. Apache is without a doubt the best and most widely used webserver package, but for what we're doing, IIS (Internet Information Services) the Windows Webserver comes with Windows XP and is perfect. You can install it with other windows addons in Add/Remove Programs in the control pannel. (I'm using XP Pro, I'm not sure if it comes with Home. Person Web Server (PWS) that comes with 98 might come with home. If IIS comes with Home, then ignore that :-/
welcome to phpsu.com
---------
If you want to install Apache, go
http://www.astalavista.com/library/os/windows/installing-apache-windows.shtml
then to install PHP
http://www.php.net/manual/en/install.apache.php#install.apache.windows
Apache is a better webserver, but like I said before, if its just for testing a few PHP scripts, IIS is fine.
---------
Now if you've been following from above, IIS should now be installed. Navigate to your C:\ drive, Inetpub folder, then inside there the wwwroot folder. Create a folder in there called 'test' and inside that folder, make a new txt document, open it in notepad, and type something, 'Gog owns me' will suffice, but anything will do. Now rename this file to boo.html and go http://localhost/test/boo.html . You should see what you typed, if you do, IIS is working :) phpsu
Now to installing PHP. Installing PHP is a breeze, well, if it configures itself auto like it should.
First, surf on over to http://www.php.net/downloads.php. The file you want is about 1/4 of the way down the page, under the 'Windows Binaries' subheading. Grab 'PHP 4.3.2 installer [1,035Kb] - 29 May 2003' it should be the second one in that section. Once downloaded, Run, and it all should install and configure itself on IIS.
Now, once its finished, go back to our boo.html we did before. Rename it to .php, and open it in notepad. Erase what you wrote in it before, and in it type
<?
phpinfo();
?>
www.phpsu.com
Save and close.
Now go to http://localhost/test/boo.php
You should see a whole lot of crap about your server, etc. If you do, the install was a success! If you just see the words 'phpinfo();', then it hasn't worked. I can't really troubleshoot this, but i'm sure Google can :)
1.2 - Your first Script
Now, go back to boo.php where we did phpinfo(). Delete phpinfo();, but leave <? and ?>. <? and ?> tell the server that the document is PHP, without them it will just output your code as normal text. You can also use <?php to start, if you want.
Now, in our humble boo.php, type: $text = 'at0mic is leet'; then, on a new line: echo "$text";
So boo.php should now look like:
<?php
$text = "at0mic is leet";www.phpsu.com
echo "$text";
?>
phpsu提供的php教程
Save this, then navigate to http://localhost/test/boo.php. You should see the words at0mic is leet printed on the screen. Woo! You've just done your first script! With no 'Hello World' bullshit. What you've done above, is set the variable $text, to 'at0mic is leet' then you've outputted the variable. Now if you look at the Source of that page (Right Click in Internet Explorer -> View Source) you'll notice you don't see any of your code, just 'at0mic is leet' printed in the top left. Thats the magic of PHP, and any other server-side scripting language, no-one can see your code, they just see the output.
That script was very basic, and really does nothing usefull that you couldn't just do normally without PHP. But alas, there is a point to all of this. Next!
1.3 - IF/ELSE Statements
PHP would be as usefull as a copy of Noggin' without if/else statements. IF/ELSE statements are standard for any programming language, and allow the script to carry out different actions based on what a certain vairable equals/is greater or less than/etc.
do you kown phpsu.com?
If else statements are broken up into three parts. The IF part, THEN, and ELSE. For example:
if ($this == $that) {
echo "They equal! Hurray!";
}
ELSE {
echo "I love you!";
}
phpsu提供的php教程
So in that example, if the variable $this was the same as $that , the script would output 'They equal! Hurray!' if the two variables didn't equal, the script woul output 'I love you!'. Any code contained within { and } will be executed, so you arn't just limited to the one thing, obviously. So, lets re-open boo.php, and add and if / else thing
<?php
$text = "at0mic is leet";
echo "$text";
$gog = "cool";
if ($gog == "cool") {
echo "Gog is Cool!";
}
else {
echo "We all know gog is mega cool";
}
?>
http://www.phpsu.com
Whoa, the page is getting quite long. This should now print 'at0mic is leet' and 'Gog is Cool!'. And thats the reals basics of IF ELSE statements, theres a whole heap more you can do with them be sure to check out
http://au2.php.net/manual/en/control-structures.php#control-structures.if
1.4 Forms n' Shit
After reading this far your probally wondering just how users can actually interact with your scripts. Thats where forms come in, you can take the data a user has entered into a form, and say, do if else statements, etc.
Forms are just plain old HTML, looking something like this.
<form action="something.php" method="post">
<input type="text" name="name"><br>
<input type="submit" value="Submit">do you kown phpsu.com?
</form>
do you kown phpsu.com?
This code, would be saved in a entire new document, called whatever.html, and when the user entered his name and clicked submit, would submit the data he entered to something.php, using POST method. Something.php could look like this:
<?php
echo "Your name is $_POST['name']";
?>
welcome to phpsu.com
This would output 'Your name is' then whatever the user entered in the form. Data carried to a PHP script using POST method is all contained in the $_POST array. So displaying this data is as easy as typing " $_POST['bleh'] " where bleh is you would put whatever you called it in the form, where is says 'name='. You follow? :)
Okay, so lets put this into practice with our boo.php. Make a new file in our 'test' folder (or wherever you put boo.php) and call it form.html. In it, put:
<form action="boo.php" method="post">
Nickname: <input type="text" name="nickname"><br>
Real Name: <input type="text" name="name"><br>
Age: <input type="text" name="age">
<input type="submit" value="Submit">do you kown phpsu.com?
</form>
http://www.phpsu.com
Save form.html and close. Re-open boo.php. And add:
echo "Hello $_POST['name'] , who goes by the alias $_POST['nickname'].";
if ($_POST['age'] < 8) {
echo "You are pretty young, what are you doing here?";
}
ELSE {
echo "So, you are $_POST['age'] , eh?";
}
welcome to phpsu.com
Ofcourse thats just an example, a little gay, but you can change what the people are entering to whatever you want.
You can also use GET instead of post, that sends the data in the URL, ie http://www.website.com/form.php?secretfbipassword=rabbit .
In that url, the data entered in 'secretfbipassword' in a form will be sent, in this case the secretfbipassword was rabbit, so form.php might say
echo "The secret FBI password is $_GET['secretfbipassword'] - dont tell anyone!";
www.phpsu.com
Notice how I used $_GET instead of $_POST ? If you want to send data using $_GET, you'll need to change the form, where it says method=.
The best thing about $_GET is you don't have to send data from a form with it, for example, you could just put the data in the url ?name=data then retreive it on the next page, thus carrying variables from page to page. Theres other ways you can do that, using cookies/sessions, etc, but for the sake of simplicity I won't go into them here.
1.5 Mail With PHP
You'll need to install an SMTP server to use mail(). Best, if you're going to do this, upload it to a host instead of running it locally .
Sending mail is PHP is simple. PHP has its own mail() command, which makes sending mail as easy as buttering toast, (if you have all 10 fingers, that is). You can use mail() as follows:
phpsu提供的php教程
mail($to,$subject,$body,$headers);
phpsu提供的php教程
Theres no explaining what each thing is, you don't need to use descriptive variable names, as the position of the variables in relation of the commas decides what they're used for, (first one being the e-mail to, second subject, third actual e-mail body, and fourth and headers you want to include.)
So thats easy enough, lets open boo.php, and add this:
mail(chris_lowden@hotmail.com,Hello,Just testing Chris);
phpsu提供的php教程
That would send an e-mail to chris_lowden@hotmail.com (me) with subject hello, and 'Just testing Chris' in the body. Change that to whatever you like, your e-mail for testing purposes, or a mates, etc.
I don't think theres much more to explain with the mail() command. It certainly isn't the most usefull, but its pretty cool none-the-less.
2.0 - Conclusion and Further Reading
If you've finished this, congrats! You can now write basic PHP scripts. Armed with your newfound knowledge, you might want to check out these sites:
TITLE:PHP Beginners Tutorial