PHP – A Simple Guide

PHP (Hypertext Preprocessor) is a server scripting language designed basically to help create more dynamic web-pages. Unlike HTML, Javascript, and other web-languages, PHP is processed on the server side, before any other language is processed on the client side. If you click “View Source” on a web-page, you will not be able to see the PHP code, because it’s executed on the server-side, but you will however be able to see anything you generate from it (such as html, text, or anything you send to the client-side using PHP). offers a wide documentation of every function and syntax that it has to offer.

Let’s go over the basics:

To start, simply create a file, for example index.php

<?php //used to start PHP code

//inside can be any PHP related syntax.

?> // used to end PHP code

//outside the tags can be regular client-side code (HTML, CSS, Javascript, ect)

<?php //you can also jump in and out of tags multiple times on a page.


Below is a php file with both client-side script and PHP (Server sided script). Copied from

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
            echo "Hi, I'm a PHP script!";

In this example, we have a regular html page, but we jump into PHP by using the tags explained above. In PHP, to print text to the screen (similar to java’s System.out.println(“text”)), we use echo “text”; We don’t have to just print plain old text though! We can also print HTML to the screen. The code below will send and print whatever specified (even if it’s HTML code), after sending it to the client, it will become actual functional HTML code. The same can be done with Javascript or and client-sided language.


echo "<div id='whatever'>Im inside an html element.</div>";


Everyone uses conditional statements, right? Everyone knows how they work by now. Here’s a way in which we use conditional statements to display html code depending on the condition. This is very useful if you want to display something on a page depending on a variable or a stage in a process.
As said before, because PHP is executed on the server side (unlike javascript or html, ect on the client side), we can handle which html code is displayed beforehand.

<?php //start tag

$test; //some boolean

if (!$test) {

?> //end tag

<p>Since the test boolean was false, this html paragraph is being displayed on the page. You can put whatever html in here since we're not within php tags.</p

<?php //start tag

} else {

?> //end tag

<p>Here, the test boolean was true, so instead of the previous html, we're displaying this one instead.</p.


} // end of conditional else

?> //end tag

In PHP, variables are used a little differently than languages you’re probably most familiar with.

$variable = 1;
$variable2 = "Hey";

As you can see, there’s no data type at all! Variables in PHP are used as containers for storing any and all kinds of data/information.

  • Start a variable with a $ sign.
  • Variables must start with a letter or _ underscore character. They cannot start with a number.
  • Variables are case sensitive from one another.

Because variables are containers for any and all data types, it’s a very “loose” language and makes it very dynamic.

$variable = 1;

echo $variable; //will print 1

$variable = "Hey"; // notice how we set the above variable to a string even though it was previously a numeric value.

echo $variable; //will print Hey

The remainder of this tutorial serves as a repository of information and explanations for basic PHP functions that new rsps owners would most likely find helpful when building or adding onto their website. It will be updated weekly as time allows. Last week, I made a tutorial about Basic SQL Communication, which can also be referred to as a source of information.

Concatination Operators – Joins strings together.

Many people who’ve been coding in Java know by now that you use the ‘+’ operator to join strings together. In PHP, it’s done exactly the same, but with a ‘.’ operator. Concatenation is used very often, which is why I’ve included it in this repository (although very simple). Example:

$variable = "What's " . "up?";
The above variable is now "What's up?".
We can also take the variable we just created and add something else to it using assignment concatination.

$variable .= " How are you?";

With the above changes, $variable now reads "What's up? How are you?".

Other examples:

$new = $variable . "Fine, thanks."; //$new is now "What's up? How are you? Fine, thanks".

array() – Used for initializing arrays.

There are three different types/categories of array’s, but I’m only going to cover Indexed arrays, since the others are more advanced for beginners. Below, each value inside the array has an index value, starting at zero and ending at how ever many total values there are minus one (since we started with zero). If we had 10 values, the indexing would be from 0-9 (total of 10 numbers).

//to make an indexed array, start by using the array() method.

$array = array("string one", "string two", "string three");

Let’s say you wanted to retrieve or modify an element inside the array.

echo $array[2]; // prints out the 3rd element of the array (remember that indexing starts at zero, so 2 would be the third element).

$array[2] = "testing"; // changes the 3rd element inside the array to the specified string.

substr(string, startVal, length) – Returns a substring of the given string.

Again, those who are fluent in other programming languages will know that this is a common function.

$string = "Hey dude";

echo substr($string, 0, 3); // will print "Hey"

The above code takes the string "Hey dude" and returns a substring starting at index 0 and adding a length of 3. 

You also[B] don't[/B] have to specify the length argument.

echo substr($string, 1); // will print "ey dude"

The above takes the string starting at index 1 and returns everything from that point till the end.

Additionally, you can always specify a direct string, instead of a variable:

echo substr("Test", 1); // will print "est".

$_POST – An array that stores posted values.

<form action="anotherpage.php" method="post">
        <input type="text" name="name" value="Joe"><br>
        <input type="text" name="message" value="hey"><br>
    <input type="submit">

The above code creates an html form on your page. Upon clicking the submit button, it posts the form information (name and message) to the post array and directs users to the anotherpage.php.

This is php code listed in your anotherpage.php file. It grabs the posted variables and assigns them to variables.
$name = $_POST['name']; //will be Joe
$message = $_POST['message']; // will be hey

If you would like to display to your user what he has entered on the previous page, simple echo out the variables:

echo $name;
echo $message;

fsockopen() – Opens a socket connection using an IP and port #.

This PHP method can be used to display an online/offline status of your  server from your website, or to just check a port on a server.

@fsockopen($host, $port);

Example using code:

$connection = @fsockopen("", 43594);
if (is_resource($connection)) {
    //port is open, do whatever.
    echo "yay it's online.";
} else {
    echo "Aww, the port is closed or my server is offline :(";

mt_rand() or mt_rand(minVal, maxVal) – Generates a random number value.

This can be used for almost anything; creating session ids for a donation or vote page, number only passwords, or maybe you have something else in mind that requires a random number.

echo mt_rand(); //prints a positive number from 0 to integer max value (2.147 billion).
echo mt_rand(40, 60); //will print a number between 40 and 60 inclusive.

Yes, you can also use the function rand() or rand(min, max) in the same way, but rand() only gives a random number up to 32,000.

md5(string) – Generates an md5 hash using the inputted string.

Many people use md5 hashes all the time, so I’ve included a little bit on this function (although not entirely important). Please don’t use this as a password hash, since it’s not 100% secure.

$valuable = "valuable data or something you want masked";
$valuable = md5($valuable); // will now be an md5 hashed version.