May 28, 2014 06:39 PM
(This post was last modified: Jun 22, 2014 05:33 AM by 1n9i9c7om.)
![[Image: n0h1y5D.png]](https://i.imgur.com/n0h1y5D.png)
![[Image: Pvlu0HP.png]](https://i.imgur.com/Pvlu0HP.png)
- The Basics
- What is PHP? An Introduction
- Setting up your development environment
- An easy start - your first PHP page
- An introduction into variables
- What if..?
- Let's do the math
- Array's
- I GET your POST
- Loops
- Yay, cookies! And Sessions
- What is PHP? An Introduction
- Using a database
- A bad example - how to NOT do it, and WHY!
- A good example - how to do it, and why
- Prevent XSS vulnerabilities on your website
- A bad example - how to NOT do it, and WHY!
- Example
- A simple login system
- Tips
- Continue learning
![[Image: Cjswpsi.png]](https://i.imgur.com/Cjswpsi.png)
"PHP is a widely-used general-purpose scripting language that is especially suited for Web development and can be embedded into HTML." - php.net
Originally PHP stood for Personal Home Page Tools, but this changed when PHP 3 got released. Now PHP stands for PHP: Hypertext Preprocessor.
In this tutorial, you'll learn the basics of PHP. You'll also learn how to work with databases and how to make your script secure.
I'll include some example at the end of this tutorial, and at last I'll give you tips for further learning.
![[Image: SUY789w.png]](https://i.imgur.com/SUY789w.png)
If you've already Apache, PHP and MySQL installed OR if you got hosting, you can skip this step.
For our development environment we'll use EasyPHP. It comes with Apache, PHP and MySQL - that's everything we need.
If you're on Windows XP or Vista, you'll have to download this, on Windows 7, 8 and 8.1 you can use this.
When you downloaded it, you obviously have to start the installer, so let's do it. I won't explain how to install it in-depth, as it is like every other installer.
When the installation is done, untick "Open Help", make sure "Launch EasyPHP" is ticked and click on "Finish".
You should now see the EasyPHP icon in your tray. Double-click it and you'll see this: (Click to View)
Note: If you're getting this error, log out from Skype, start Apache and log in in Skype again. It'll work then.
The root directory if your website is here: C:\Program Files (x86)\EasyPHP-DevServer-13.1VC11\data\localweb
That means that you have to save your .php files here or in any subfolder.
(Note: This depends on where you installed EasyPHP. If you changed the path during the installation, look there).
We'll need phpMyAdmin to make it easier to work with databases. Go to the EasyPHP folder, open the modules folder and copy the phpMyAdmin folder. Paste it in the localweb folder and rename it to phpMyAdmin. It should look like this now.
Congratulations! You installed the testing environment, we are now able to start!
![[Image: YXffEcJ.png]](https://i.imgur.com/YXffEcJ.png)
Now, after you installed your test environment, we can start.
Coding via Notepad is bad - you should get Sublime Text or Notepad++.
This is a basic HTML page, nothing special, just the most important stuff:
PHP Code
<?php
<!DOCTYPE html>
<html>
<head>
<title>
A basic HTML site
</title>
</head>
<body>
<h1>Basic HTML Site</h1>
<p>This is a basic HTML site.</p>
</body>
</html>
If you want, you can check how it looks like by saving it as basic.html, then move it into the root folder of your webserver and open it by entering http://127.0.0.1/basic.html (or, if you're using hosting, http://yourdomain.com/basic.html).
Now we add some PHP into this code:
PHP Code
<?php
<!DOCTYPE html>
<html>
<head>
<title>
Your first PHP script
</title>
</head>
<body>
<h1>First PHP script</h1>
<p><?php echo "This is your first PHP script."; ?></p>
</body>
</html>
Save it as first.php and run it by opening it in your browser. It should look like this.
echo is a language construct that is used to output strings.
Another language construct to output strings is print. However, I'll most likely use echo in this guide.
![[Image: bjevjyW.png]](https://i.imgur.com/bjevjyW.png)
Variables are like 'containers' - they contain values.
There are different types of variables:
- Strings
- Integers
- Floats
- Booleans
- Arrays (we'll talk about them later)
- ...
Here is full a list of variables types in PHP
Strings basically contains text. In our first PHP page we have this line:
PHP Code
<?php
<p><?php echo "This is your first PHP script."; ?></p>
The text in the quotes is a string.
There are for ways to declare strings in PHP. They're very well explained on PHP.net, so I think it'd be the best to just link you to it.
Here is the explaination.
If you're using a 32-bit build of PHP, integers can be from 2147483648 to 2147483647, on a 64-bit build it's from -9223372036854775808 to 9223372036854775807
An integer can only store round numbers, every number containing a decimal mark will be a float. The maximum of a float is platform-dependent.
Booleans can either contain true or false.
To assign a value to a variable, you just do this:
PHP Code
<?php
$string = "Hello World!";
$int = 1;
$float = 1.0214;
$bool = true;
For further information about variables and types, check out this and this.
![[Image: mL8zf7o.png]](https://i.imgur.com/mL8zf7o.png)
If you're working with PHP, you'll need to use conditional statements.
With these conditional statements you can, for example, check whether a user is logged in or not.
Basic if conditions works like this:
PHP Code
<?php
if($loggedIn)
{
echo 'This will only be executed when the $loggedIn boolean is true';
}
You can check multiple conditions by using && (and) and || (or).
PHP Code
<?php
if($loggedIn && $canPost)
{
echo "This will only be executed if both booleans are true";
}
PHP Code
<?php
if($i < 0 || $i > 10) {
echo "This will only be executed if $i is between 0 and 10";
}
You can use == to check whether a variable is like a variable/string/integer/float:
PHP Code
<?php
if($userId == 1)
{
echo "This will only be executed if the userId variable equals 1!";
}
if($username == "1n9i9c7om")
{
echo "This will only be executed if the username is 1n9i9c7om.";
}
However, == will just check the value, not the type. For example, this will be executed:
PHP Code
<?php
if("0" == 0)
{
echo '"0" is like 0';
}
If you also want to check the type, you'll have to use ===. This won't work as 0 is an integer while "0" is a string:
PHP Code
<?php
if("0" === 0)
{
echo "This will never be executed.";
}
If you have to compare one variable with multiple results, you can use a switch statement instead of many loops:
PHP Code
<?php
switch ($i) {
case 0:
echo "i = 0";
break;
case 1:
echo "i = 1";
break;
case 2:
echo "i = 2";
break;
}
This also works with strings and any other types:
PHP Code
<?php
switch ($language) {
case "php":
echo "The used language is php";
break;
case "c":
echo "The used language is c";
break;
case "vb":
echo "The used language is vb";
break;
}
![[Image: RGKRNuP.png]](https://i.imgur.com/RGKRNuP.png)
There are many things you can do with numbers in PHP - these are the simplest:
PHP Code
<?php
$result = 1 + 1; //simple addition, $result will be 2
$result = 1 - 1; //simple subtraction, $result will be 0
$result = 6 / 2; //simple division, $result will be 3
$result = 4 * 2; //simple multiplication, $result will be 8
$result = 8 % 3; //modulus, will return the remainder of 8 divided by 3, so 2.
//This can be used to check whether we have an even or an odd number:
if($i % 2 == 0)
{
echo "Number is even.";
}
else
{
echo "Number is odd.";
}
You can find out more about this here.
Beside this arithmetic operators, there are other useful functions to work with numbers as well.
I'm not going to give you example of all of them, but here's a quick list of the most useful ones:
- round - Used to round a number.
- ceil - Used to round up a number.
- floor - Used to round down a number.
- rand and mt_rand - Used to generate random numbers.
![[Image: mj3fuIe.png]](https://i.imgur.com/mj3fuIe.png)
If you're familiar with any other coding language, you should know what an array is, if not: Arrays associate values with keys. This might sound difficult at first, but here's an example:
PHP Code
<?php
$pluginInfo= array(
//key => value
"name" => "Easy Refer",
"description" => "Creates an invitation form which adds the members referral code. Also shows the users referrals.",
"website" => "http://www.mybbcentral.com",
"author" => "Jesse Omni",
"authorsite" => "http://www.mybbcentral.com",
"version" => "2.0",
"guid" => "",
"compatibility" => "16*"
);
The array values are actually from Omni's "Easy Refer"-plugin.
So, now you have an array, you can easily extract information from it by doing this:
PHP Code
<?php
echo $pluginInfo["name"];
echo $pluginInfo["description"];
It's quiet easy, isn't it? If you don't specify a key, it'll numerate the values.
For example, an array specified like this:
PHP Code
<?php
$ourArray = array("Easy Refer", "Jesse Omni");
PHP Code
<?php
echo $ourArray[0]; //"Easy Refer"
echo $ourArray[1]; //"Jesse Omni"
Keep in mind that it'll start counting with 0, not with 1!
You can set a key any time, but keep in mind that if it's an number, PHP will continue counting from this number, so if you have an array with 3 values and set the key to 5, the value added after it will have the key 6, if not specified different.
The value of an array can be anything, even another array. That'd look similar to this:
PHP Code
<?php
$memberInfo = array("1n9i9c7om" => array("uid" => 1296484), "Tibit" => array("uid" => 651337));
$memberInfo["1n9i9c7om"]["uid"]; //returns 1296484
$memberInfo["Tibit"]["uid"]; //return 651337
Additional info's about arrays can be found on the official PHP.net page here
![[Image: Swo3yYN.png]](https://i.imgur.com/Swo3yYN.png)
GET and POST are two methods to transmit form-data from one page to another. This might sound complex now, but it'll be something you'll be using on a regularly basis if you're going to use PHP for your projects. It's much easier than it sounds!
First, you'll have to create a HTML form. It looks like this:
PHP Code
<?php
<form method="" action=""></form>
A GET request will append the data to the URL (member.php?action=profile&uid=969003) while POST data will be sent in the headers.
The max. length for GET data is limited. Of course the max. length for POST data is limited as well, but it's much larger (depending on the php.ini).
Now, we have to add some 'input's to your form. This is also easy HTML:
PHP Code
<?php
<form method="POST">
<input type="text" name="username"><br/>
<input type="submit" name="submit" value="Submit"><br/>
</form>
We've added a textbox and a submit-button to our form. You can also add checkboxes, radioboxes, dropdown-lists and other things.
It's important that we add a name to these HTML objects, as we will get their values by using their names:
PHP Code
<?php
if(isset($_POST['submit'])) //make sure this code is only being executed when the submit-button has been pressed
{
$username = $_POST['username']; //get the value from $_POST['username'] and save it in a variable called $username
}
If you're using GET, it'd be the same, except that you use $_GET instead of $_POST.



![[Image: nqALTUJ.png]](https://i.imgur.com/nqALTUJ.png)
![[Image: w5oTTyG.png]](https://i.imgur.com/w5oTTyG.png)
![[Image: 101sEuf.png]](https://i.imgur.com/101sEuf.png)
![[Image: KX6Uzjn.png]](https://i.imgur.com/KX6Uzjn.png)
![[Image: LyU8Avw.png]](https://i.imgur.com/LyU8Avw.png)
![[Image: 9xek1t6.png]](https://i.imgur.com/9xek1t6.png)
![[Image: A5tDN99.png]](https://i.imgur.com/A5tDN99.png)
![[Image: 9fwyfTo.png]](https://i.imgur.com/9fwyfTo.png)
![[Image: bPiGcz3.png]](https://i.imgur.com/bPiGcz3.png)

![[Image: 8Y5L4xP.jpg]](https://i.imgur.com/8Y5L4xP.jpg)
![[Image: empire.gif]](https://i.tiny.re/empire.gif)