|
![]() |
Overclock.net - Overclocking.net > Overclock.net Forum > FAQs | |
Info: PHP Tutorial Part 1
|
||
|
|
LinkBack | Thread Tools |
|
|
#1 (permalink) | |||||||||||
|
Programmer
|
PHP Tutorial
__________________Welcome to the basic PHP tutorial. You will be learning the following items:
You will need:
Ok, here we go... Tags surround EVERY PHP script. The tags are <?php for opening, and ?> for closing. An example: PHP Code:
Comments It is possible to add comments to scripts, such as your name and a copyright notice, as so: PHP Code:
Let's start with Variables, since they are rather basic and easy to learn. A few examples of variables: PHP Code:
Variables can be names anything, they just have to start with a $. So $asdkskla is a variable, $hello is a variable and so on. Echoing Let's move on to echo. Echo is a very simple print, spitting out whatever you tell it to. For example: PHP Code:
If/IfElse/Else You can setup if/else command rather easy. These are handy if you want to check what is input against hat you have in your database, along with some other uses. A basic if/else command looks like this: PHP Code:
So let's say we have a variable, $input, and we want to check it for age (it's a number). You can make it as a cheap password to get into a section of your site, but this is not recommended. PHP Code:
Elseif's are rarely used, since normally you can sum it all up in one if statement. $_POST and $_GET POST and GET retrieve information either from forms or from the link itself. POST gets info from forms, such as username and password. Suppose you had a simple HTML form like so: Code:
<html> <head><title>Form</title></head> <body> <form name=form method=POST action=script.php> Username: <input type=text name=username><br> Password: <input type=password name=pass><br> <input type=submit value="Login!"> </form> </body> </html> You MUST name the input fields to get info from them. So, to retrieve the data in that form, you would use $_POST as shown: PHP Code:
$_GET is slightly different. It comes in handy when you are using the switch command, which I will go over next. For example, lets take an example URL: Code:
http://www.overclock.net/showtopic.php?t=1337 Say you have a PHP program that looks like this: PHP Code:
open this script up and you should get "No variable present in 'act'" on the screen. Now go to your script like so: Code:
name.php?act=test Like I said before this comes in handy with switch, which you will learn in a second! **** OK, coffee break.. or whatever, just let you brain rest for a second, to absorb all the wonderful code! Continue whenever you are ready! **** Switch(s) A switch is generally a fancy if/else. They come in handy for GET variables. An example of a good usage for switch is: PHP Code:
Don't forget the @ sign, it helps to ward off errors due to strange programming glitches. Fun with Functions Functions come in handy if you do a certain thing a lot, like append something to the end of a variable or filename. You can make a function and call to it at any point after it has been created. An Example: PHP Code:
Note that both FUNCTIONNAMEs are all capitals, since PHP is very case sensitive. $t is not the same as $T, as functionname is not the same as FUNCTIONNAME. A Better example, to append "lmao" to the net of a string: [PHP<? function lmao($string) { echo $string . "lmao"; }; lmao("Look as this, "); //Echoes Look at this, lmao ?> [/PHP] You can also use a function to append random characters to a filename, using arrays. Arrays Arrays store information. Whether it is a little information or a lot, you can use arrays. Some examples of arrays are: PHP Code:
The Curly Brackets ({ and }) are there to tell PHP that you are accessing a variable, so it does not have a conniption when it sees some random brackets ([ and ]). Basic MySQL connectivity To connect to a MySQL database you need an address, a username, a password, and a database to connect to. Let's say that for this example, the host(or address) is localhost, the username is root, and the password is toor, though this may not reflect you current settings (or mine, so don't try it). So, lets set these values as variables, and connect to MySQL! PHP Code:
PHP Code:
Includes Including file is good to hide MySQL connection info and other connection info. An example for MySQL is this: say you have the connection info in <basedir>/db/db.inc, and you want to get to it.. PHP Code:
PHP Code:
And that's it for this FAQ.. basic PHP for beginners I hope you like it, stay tuned for Part 2! -Monkey
Last edited by xxmonkey321xx : 09-21-06 at 07:42 PM Reason: Added "You will need" |
|||||||||||
|
|
|
|
#2 (permalink) | ||||||||||
|
Linux Lobbyist
![]() |
Great FAQ! I've always wondered where to start with PHP. This answered those questions. Also, it's not too far off from java in certain ways, but it seems to me that most programming is the same with just different syntax, functions, and managed/unmanaged memory allocation. Hopefully it's true so I can learn more languages than java (which I know very little).
rep+
__________________
Signatures are overrated.
|
||||||||||
|
|
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|