A. Header, footer, variables, and the echo function
A1. Header and footer
Alright, well, to begin a successfull script, one must always start with the correct header. For a server to recognize that a PHP file is actually a PHP file, it must be able to find the correct header to begin parsing (aka, reading and executing). The server must also know when to STOP parsing, which is why we have a footer as well! What's this correct header and footer, you ask? Well, that's what I'm here to tell you!
PHP:
<?php //Correct header
//Script code...
//And the footer below!
?>
Very simple, yes? You shouldn't have any problems with this part of a script (atleast, I'd hope not). Be sure to always include both of these, and keep the scripting between the header and footer tags. Anything outside of these tags will not be parsed by the server.. And will be visible to the user. Also, you can put PHP scripting anywhere in a file. You can have your basic HTML layout, and you can put some PHP within the file too. But if the filename of this file does not end with the right file extension (".php", ".php3", ".php4"), it won't get parsed, so be sure to always have the file extension correct if there is PHP in the page. Here's an example:
PHP:
<html>
<head>
<title>blah</title>
</head>
<body>
This is regular HTML stuff. <a href="blah.html">This is a regular HTML link, too!</a>
<?php //Notice this isnt in the beginning of the file? It doesn't need to be.
//Script code..
?>
This is more normal HTML stuff. <a href="link.html">Linky, Linky!</a>.
<?php // Another header and footer? I smell more code!
//Script code..
?>
</body>
</html>
Simple, yes? Also, in case you havent figured it out yet, the two forward slashes ("//") denote a comment. Anything after two of these slashes in a PHP script will not be parsed. It will only show up when viewing the code in an editor of some sort. Also, all PHP is parsed BEFORE the user even views the page. The server will parse the code, and then produce the result to the client when finished.
A common question asked by many people is whether or not the PHP code will show up in the users editor when viewing the source of a web page. The answer is NO. Your code is completely safe unless you give the file that you've created (physically via a file transfer or FTP transfer) to another person. I usually don't recommend doing this unless your giving the file to someone who is helping you. Not just someone who is claiming to help you, either.
A2. Variables
Alright, now here we are onto the variables section! Exciting, isnt it? Well, no, not really, but anyway, onto the point. The tutorial! Variables are basically the first block of PHP code. They're there to store the data. We wouldn't get very far without storing data in something, even if only temporary, right? Right. First of all, we'll start by teaching you how to define a variable.
PHP:
<?php //header!
$variable1 = "string"; //Defining a variable (correctly)
$variable2 = string; //Defining a variable (incorrect)
$variable3 = 1920; //Defining a variable that is numerical (correct)
$variable4 = "1920"; //Defining a variable that is numerical (ALSO correct!)
$variable5 = $variable1; //[1] (later explanation)
$variable6 = &$variable1; //[2] (later explanation)
$variable7 = $variable3 + $variable4; //[3] (later explanation)
$variable8 = function(); //[4] (later explanation)
?>
As you can see, a variable is defined by the dollar sign ("$") followed by the name of the variable. You can name a variable anything, but usually you should try to stay away from variable names that contain only numbers. A string (aka, any type of data other than numbers) should always be surrounded in quotes, which is why you see that I said (in the comment) that $variable2 is incorrect.
With $variable3, you see I am trying to define a variable with a numerical value. With numerical values, you do not need to surround it in quotes, even though you can if you wish (as you see in $variable4).
With $variable5, you can see instead of a numerical or a string value, I'm referring to $variable1 ([1]). When doing this, you are (basically) taking the information from the reference variable ($variable1) and copying it to the new variable. It's really very simple. Now, with $variable6, it's a bit different. As you can see, there is an ampersand before the reference variable this time [2]. The ampersand tells the script to actually referr to that variable, so instead of copying the data directly, the variable actually becomes more of an alias (aka, another name for the same thing) to the old one. Any changes done to $variable6 will also change the original, $variable1. Simple, eh?
Now, with $variable7 you can see im trying to solve a mathematical equation. With PHP, you can do mathematical equations and set the resutl to a variable. With $variable7, I am adding $variable3 and $variable4, which are both basically the same number, and $variable7 would end up being set to 3840, which is the result of the added equation. A variable can also be set to the result or return value of a function, as seen in $variable8 [4]. Functions that have a return value will set the variable to that value. Simple.
Note: Always remember to end ANY line with a semi-colon (";"). The only exception to this is if the line ends in a bracket ("{" or "}").
A3. The echo function
This is a really simple one, so I'll keep it short. The
echo function is what prints something out for a user to see. Here is an example.
PHP:
<?php //header :)
echo "This is what the echo function does.";
?>
This would make a user see "This is what the echo function does," whenever they visit that PHP file. The function is followed by either a string (in quotes!), an integer (numerical value, quotes or not), a mathematical equation (result), or the result of a function (that gives a return value). Also, you can have it echo out the value of a variable like so:
PHP:
<?php //header
$variable = "This is the contents."
echo $variable;
?>
Very simple. If you need more of an explanation, send me a pm!
One last note!
For strings, you may surround them in either these:
', or these:
". If you plan on using the same type of quote within the string, you need to cancel it out. Like this:
PHP:
<?php
echo "This is a \"sentence that contains quotes.\" Beware.";
echo 'This is a \'sentence that contains quotes.\' Beware.";
// No need for cancellation if the quotes are different. Like so:
echo "This is a 'sentence that contains quotes.' Beware.";
?>
This is called "Escaping," and is rather simple. Simply put a single backslash ("\") in front of the character that needs cancellation.
Expect my next tutorial very soon! Comments are welcomed!