Top 10 php Basics

php is a coding language that is used to handle the ‘backend’ stuff of websites. With php you can carry data over from one page to another, you can handle user inputs, you can display text, you can communicate with your database and much more. This language is arguably the easiest language to learn and is great for those who are looking to get into web development. The basics are extremely simple and can be learned in just a few minutes.

I’ll be using my text-based game Velrania for reference.

Please bare in mind php is NOT for producing the visual side of the web page (the part that displays on a browser), only the server-sided stuff. Browser-side visuals are made with HTML/CSS.

If you want a webhost, we recommend Bluehost!


10. Telling a server what is php

Starting off nice and simple. You’ll have a blank web page, you’ll want to tell the server that runs this page that certain code is php, it cannot see it by itself. To do this, you just do:


*php goes here*


When code is inside that <?php ?>, it is interpreted as php. So html or any other type of language would not be understood.

Also, you must ALWAYS put a “;” after every line of php code, this tells the server the line is finished and to move on to the next one.




9. Display Text

Displaying text is really easy and can be done with one command. To do this, you just use the “echo” command. Have a look at the picture, here’s the code for it:


echo “I am displaying this text using the echo command”;


If you know some HTML you can also insert it inside an echo command, as servers read HTML inside echo. You could change the font color by doing:


echo “<font color=’red’>I am displaying this text using the echo command</font>”;




8. The Include command

You can use the “include” command to reduce the lines of code per page, making it easier to organise and edit. You do this with this simple command:


If you have a page called say frontpage.php, and you put that include command at the very top of frontpage.php, any code within example.php will display when frontpage.php is loaded, as it’s told to include example.php




7. Variables

Variables are an important part of php but are extremely simple. A variable is a container of information, you tell it what to contain. To make a varible, just type a “$” sign then a word straight after. The give it a “=” and tell it what the variable will contain. An example:

$variable = 1;

$variable2 = 2;

With that, “$variable” would be equal to 1, and “$variable2” would be equal to 2. You could then do:

$variable3 = $variable + $variable2;

To php, that looks like:

$variable3 = 1 + 2;

And “$variable3” would then be equal to 1 add 2, so 3.

You can also echo variables by doing:

echo $variable3;

That command would display the number 3 on the screen!




6. Useful Commands

There’s a useful command for anything really, if you have an issue and you’re not sure how to get around it, do a quick search and you may find what you’re looking for. For example, if a variable is set to the number 50,000 and you echo it, it will appear as “50000”. But if you use a number_format command, it automatically adds the comma where needed, making it “50,000”. Another command is the “ucwords”, that will automatically make the first letter of the variable an uppercase. Useful for displaying usernames and such.




5. Connect to a database

Any website will need an SQL database. But what is that?

SQL Databases are tables that store your data, things like user accounts, purchases, IP addresses, login dates etc are all stored in there. If you’re making a user profile for example, you’ll want to pull this data from the database and use it to display the profile.

Doing this is easy, you just need a few commands. It’s recommended you put these commands in its own file, rather than placing it on each page individually. And then calling the file with the “include” command.

$con = mysqli_connect(“localhost”, “username”, “password”, “databasename”);

if (mysqli_connect_errno()) {

echo “Failed to connect to MySQL: ” . mysqli_connect_error();


$select_db = mysqli_select_db($con, ‘databasename’);

if (!$select_db) {

die(“Database Selection Failed” . mysqli_error($con));


These commands will establish a connection to your database. You’ll now be able to call data from it. The rest of this part is mostly SQL stuff so I’ll leave this out. But this is how you connect to your database in SQL.





Please for the love of god, comment your code as much as you can. This makes it a million times easier for you to edit and maintain your code. It also makes life much easier for the next person who gets the code to work with it, if they do. Usually just a short description is enough, here’s an example of how to add a comment in php:


This section is dedicated to pulling the user’s password from the database and emailing it to their registered email address


Yep, you just use the /* and */ symbols with text inbetween, easy peasy!




3. IF Command

Remember how we had variables before? Well, IF statements can be used with them to determine the outcome of certain things. There’s a number of ways you can use IF commands to display, or change data you want. Example:

IF($variable1 == 2){

echo “I am 2!”;


So if you’ve specificed “$variable1” as the number 2, it will display “I am 2!”. If “$variable1” isn’t the number 2 it will not display it. IF commands are a huge part of php, so I won’t go through everything. But this is a great start.




2. Carrying Data Over Pages

Carrying data from one page to the other is important as it’s required for a number of functionalities. One of which is registration, you’ll want the user to enter their details, press Register, then the next page to insert this data into the database. Where they can then be called on if they want to login.

The most common ways of doing this are the POST and GET methods.


If you have a HTML form using POST, any data entered into that form will carry on to the next page, ready to be called upon. So you have a form that requires a username, HTML holds that data under the name “userinfo”. You will call it via this:

$usernamevariable = $_POST[‘userinfo’];

That “$usernamevariable” would then be equal to the username, so you can do “echo” or work with IF commands to make other things happen.


The GET command works from the URL. If the URL is say,, you can do:

$username = $_GET[‘userinfo’];

The upside of this is requires no HTML, other than a hyperlink that will obviously have the URL as “”. You can also incorporate variables into this. The downside is anyone can enter custom details for GET to use, which is a huge security issue. Not the most recommended way but if you can do it securely, it does work.




1. While Command

The WHILE command is another useful command that allows you to keep running a command while a condition is met. If you want to display a list of users, you can tell it to keep pulling user accounts from the database until a condition (usually a limit of accounts, or a certain one) has been met.

For an example, I have a battling feature on my game. I use a while command to determine how long the fight goes on.

while ($health > 0){

*php battling code*


echo “the battle is over!”:


As you can see, as long as the user has more than 0 health, they will keep fighting. Very useful command!



Leave a Reply

Your email address will not be published. Required fields are marked *