This is a small introduction to the wonderful world of PHP. In this introduction I will discuss how to write PHP code, not how to install it. Installation is pretty simple, I’m sure you can figure out how to do that once you’ve downloaded it from php.net. Also note that this covers just the basics, there is so much more to PHP than this! If you’re ever in doubt, go to the documentation at php.net.
Tags & comments
To use php on your page, you first need to change the extension to .php, and when you write your code you must enclose it in a so called “php tag”. There are three diffrent, one proper and two short ones, but each of them have their advantages. These are the diffrent tags:
<?php // This is the proper tag ?>
<? // This is the short tag ?>
<?=\“This is the very short tag.\”;?>The two first tags are pretty simple. They can span several lines, and everything inside them is parsed as php code. The third one is a bit special. It starts with
<?= and ends with ;?>. Everything in between there is parsed and echoed. This one can span only one line, and there can’t be any semi-colons in it, unless they are inside a string. (Semi-colons end the line and must be included on every line that isn’t a control structure.) This can be useful if you just want to output a string somewhere, like the title of your page, or some parsed string data.
Comments are also very useful, as they help you keep track of what your code does, and where your control structures start & end. Be careful not to overcomment your code though, as it takes longer time to parse and gets harder to read. Also remember that a comment never comments out the end of the php tag. Comments look like this:
// This is a regular comment, spanning only one line.
- This is another version of the single-line comment.
/* This is the last comment style.
It can span several lines. */Pretty easy, huh? Well that’s just the beginning. In the next section, we’re going to learn about variables!
Variables & echoing
An essential thing to make any half-assed PHP application work is variables – they store information for you. A variable can be boolean (false/true), string (text), intreger (numbers), arrays or objects. Objects are a bit overkill if you’re only just starting to learn PHP, so I won’t be explaining that. Here’s an example of the first three (string, intreger and boolean):
<?
$string = “Hello World”; //Strings are, well, strings of text.
$intreger = -50; // Intregers can be both positive and negative
$boolean = false; // This is a boolean value. This is almost the same as an intreger with the value of 0.
?>
Now, the array is a bit more complicated. You can create an array in two ways; the easiest one being the array function. Here’s an example:
<?
$array = array(“One”,“Two”,“Three”); // This creates the array and inserts some values.
echo $array0; // This prints the first value in the array.
echo $array2; // This prints the third value.
$array[“One”] = 1; // This sets the “One”-value to 1.
echo $array[“One”]; // This will print 1.
?>
Now there are three important things to notice here; the first one is that when you call an array you’ll have to subtract one from the “row” you want, because PHP starts counting from 0, not 1. The second thing is that you can have not only numbers but also strings as the “row identifier”. And the third thing: The echo function. This is used to print a variable to the page. You can also use the print function, which really is just an alias for the echo function. Now join us in the next section, where we’ll enter the exciting world of expressions and control structures!
Control structures
Expressions and control structures are two other very important things if you want to make something useful. The first of them that we’ll learn about is the expressions. Expressions are used in all control structures, and they are what checks if a value satisfies a criteria. There are five diffrent expression types:
and!=- checks if two values are equal to or not eqaul to another.<and>- checks if a value is less than or greater than another value.<=and>=- checks if a value is less than or equal to or greater than or equal to another value.=– checks if two values are equal to another, and if they’re both of the same type.!==– checks if two values are of diffrent types or not equal to each other.
When you use the expressions you mostly use them in if statements. The if statement is very simple:
<?php
if(expression){
// Do stuff
}else if(expression){
// Do other stuff
}else{
// Do something else
}
?>Of course, you can skip the elseif and else parts if you don’t need them. Now, there’s something called a switch statement which can be used instead of using multiple if statements, but that is a bit complicated, so i won’t discuss it in this guide. If you want to read more about it you can go to php.net’s documentation. There are two more control structures we’re going to learn more about: the for and while loops. We’ll start with the while loop, as it is easier to understand. Here’s how it looks:
<?php
while(expression){
// Do stuff
}
?>Pretty easy. While
expression is true it runs the code inside the loop. The for loop is a bit more complicated:<?php
for(start;end;increment){
// Do stuff
}
?>As you can see, this is more complicated. “
start” is the initialisation of a variable, most of the time you’d use “i=0”, which basically makes the loop start at 0. “end” is the expression, and while it is true the loop keeps running. “increment” is a peice of code that tells the loop how much the variable should increase or decrease. Most of the time you use “i++”, wich increments the variable by one, but if you need something else that works too. Try this a few times as it can be a bit tricky to understand. In the next section we’ll discuss global variables.Global variables
So, what is a “global variable”? Well, a global variable is not really variable at all. They are constant arrays, however it may change when you reload a page or they may depend on what browser you use. There are six global variables:
$_SERVER– Stores information about the server and the user (who views the page)$_COOKIE– Stores information about the user’s cookies (only the ones your site has set)$_GET– Stores the variables sent by forms using the “GET” method (visible in address bar)$_POST– Stores the variables sent by forms using the “POST” method (“invisible”)$_FILES– Stores information about files uploaded using the “POST” method$_SESSION– Stores information about the current session (used in login systems)
The most important ones are the $_GET and $_POST variables. Here’s how they work: Let’s assume that you have a webpage with a form, that has the method set to “GET”. The form is set to call a file called “echo.php” wich should echo the text in the input field named “text” when the user presses the submit button. Here’s what the “echo.php” page would look like:
<?php
echo $_GET[‘text’];
?>As you can see, they are simply variables. For each input field you have in the form, there is one value in $_GET, whose identifier is the same as the name of the input tag. The same goes for the $_POST tag, except that the form method has to be set to “POST”, and when you do a POST-request the values of the input tags are not visible in the address bar. This is useful in for example login forms. If you use GET, however, you could easily mess upp stuff by simply adding ?var=value to the end of the address. If you want to be on the safe side, use POST.
Well, that’s the end of this little introduction. And remember, php.net is your friend, as they have information about every single PHP function in their documentation.