What? Okay, so here's the general idea of what I'm going to do in this tutorial:
Teach you the basics of PHP.
Simple as that…nothing more, nothing less…well, maybe a little more…
------------------------------------------------------------------------------------------
Okay, let's get started!If you have a server, then great! If not…well, get one! Ask a friend, look online, anything! If it has PHP installed on the server, then you're set!
Once you have a server and an FTP account to transfer the files, we can start by making our first PHP program! To make a program with PHP, there are a few things you need to understand first:
- Every PHP program MUST start and end with , respectively. You will see examples of this later.
- All expressions must end with a semicolon ( ; )
- A PHP file (.php) with HTML scripts in it WILL be read as HTML, unless inside tags. (examples are explained further down)
So, now that you're confused with all of that, let me clear something up with an example script. First, open notepad, or anything that can save a .php, in need of a good one?( //<-- the "//" indicates a one-line comment, and only everything after and on the same line as the "//", will be counted as a comment. // Please note that the "echo "Hello World!"; //"echo" is a basic PHP function that displays data in the form of a string. If you notice, I added the ‘;' at the end, and I put my string (Hello World!), in quotes. This means that it will echo it as a string.
/* This is an example of a Multi Line comment. It will only stop with the break symbol -->*/
?>)
Okay, confused? Good. Go upload that file to the server (save it as test.php). Now, go you your server, and view the file. Notice how it displays "Hello World!"? Good, catching on? Nice!
Now, a little bit harder...
= "This is a variable set as a string.";
$my_number = 0;
// Here, I set two variables. In PHP, a variable is defined with a $, then a string. IE: My two variables are called my_string, and my_number, because they each start with a $.
/* Now that the two variables are set, let's do something with them.
First, I'll make it easy and just display what they equal */
echo "$my_string = ".$my_string."
$my_number = ".$my_number;
/* Notice how I used a ‘.' To append separate strings and variables together, this makes the echo function display multiple strings
and variables, with one execution…IE: Instead of doing this:
echo "$my_string = ";
echo $my_string;
echo "
";
echo "$my_number = ";
echo $my_number;
I just put it all in one echo. */
?>)
That's the basic overview of variables…they're simple once you get the hang of them. Now, to perform a function, such as add(+), multiply(*), divide(/), and subtract(-), you simply do this:
//Set Initial Number
$num = 10;
//Make a new variable, and perform +
$add = $num+5;
//Make a new variable, and perform -
$sub = $num-5;
//Make a new variable, and perform *
$multiply = $num*5;
//Make a new variable, and perform /
$divide = $num/5;
//Echo all my variables
echo "$num =".$num;
echo "
$sub =".$sub;
echo "
$multiply =".$multiply;
echo "
$divide =".$divide;
?>)
If you're noticing, it's a LOT like Flash (if you know flash). It's just a bit more work (because it's all through text, no drawing and such). You set your variables (with the $, to define a variable), add a ; at the end of a function, and display data. Simple.
Now, how about some if() statements? Let's make it easy:
(//set number variable
$num = 5;
//run if() statement
if ($num == 5){
//if the if() statement is true, echo string
echo "$num is = to 5.";
} else {
//else, echo another string
echo "$num does not = 5.";
}
?>)
Now, that example is pretty pointless, because I set $num to 5 to start, so it will echo "$num is = to 5.", however, if you change $num to anything else, it will echo "$num does not = 5.". Now, how about some user input? If you haven't noticed from above, I can put some HTML scripts in the echo function, and it will work (the
, made a line break) IE:
(echo "This is an example of an HTML line break:
Top
Bottom";
?>
So, we know we can run HTML scripts, let's make HTML interact with PHP…yes, that's it! Here we go:= $_POST['name'];
if (!$name){
echo "Please enter a name: ";
?>)
(
} else {
echo "Hello ".$name.", welcome!";
}
?>)