PHP Crash Course - VSBE Group

PHP Crash Course

Getting Started

Creating your file

To create a file, you need to make sure that the extension of the file ends in .php to make sure that the PHP executes correctly.

What to put in your file
You can put your php code anywhere inside your file as a PHP file is the same as a HTML file.

For Wordpress, the PHP tags use up the whole documenet and you must make sure there is no blank space at the end of the file.


Forms are used to transfer data between two pages. There are three main elements when using a form; the header, the elements and the submit. The code for these sections will be displayed below.

Data Transfer Methods
There are two different ways of transferring data from page to page.

Get can be used in one of two way; sending data from page to page using forms, or transferring data by manipulating the URL. The main feature of using this is that all of the information is stored in the URL. One advantage of this is that if a user wanted to go to a specific page with certain values, they can do so.


The main feature of POST is that all of the data is transferred without using the URL. This means that personal information or sensitive data can be sent from page to page without anybody seeing the data.


There are two things you need to change, the action and header. The action is the location where you want to send the data to. The method is the data transfer method you wish to use. Information about this is shown above.

Form Elements
Most of the elements that are mentioned below can have different properties added to them. The main one that you may use often is value="" which, when used, displays the a value inside of the element when the page is loaded.

When the text element is used, a text box will be displayed to the user


When the number element is used, a text box will be displayed to the user with the ability to use an up-down button to change the value. There also different options including mins, max and step.


When the select element is used, a dropdown menu will be displayed with the values that are inside of the select tags.

Text Area

When the text area element is used, a large text box will be displayed. The size of the text area can be adjusted using the rows and columns selection inside of the text area tags. The size can also be adjusted by the user.


When the hidden element is used, nothing will be displayed to the user.


To submit the form, you will need a button to submit the data and tell PHP that you want to move onto the next page.

At The Other End...




A header is used if you want to have the same menu styles put across all of your different pages on your site.
include 'filename.php';

Moving Between Pages

There are three main methods of moving between pages; hyperlinks, forms and automatiaclly using PHP.

This is generally used when you want to move between two pages without transferring data between them. To use this method, you will need to use the "a href" tags. You can put anything inside of the page including text and buttons.


Forms is a way to move between pages while transering data between them. This method had already been convered on this page. Click here to jump to forms

PHP Headers

This is used when you want PHP to automatiaclly move to antoher page without a users interaction. This could be useful if you have got pages that are exculsivly for reteving data or backend pages in general.


There are lots of different variables types that are inside of PHP. For all of the variable types mentioned below, you do not need to declare the variable date type.

Local Variables

These variable are only available inside the document that the variable was created on.

Session Variables

These variable are available inside all PHP pages that are loaded up in the same session. This means that once a variable has been declared on one page, it can be used throughout the site.

When using sessions, you will need to add the code session_start() at the top of the document. This has to be used in every file that uses sessions.

Row Variables

These variables are used when getting data from a SQL database. This might not be used when selecting data using Wordpress.

Destroying Variables
Once you have finished with a variable, you may want to destroy it so that it cant be used again.

Displaying Text

When displaying text you need to use the echo statement.
echo "Hello World!";
This will display anything inside of the echo statement as HTML and will be displayed when the page is rendered.

Displaying a variable
When you display a variable, you need to use speech marks to exit out of the echo statement and full stops to tell the code that the following needs to be concatenated.
echo "Hi There".$variablename.", How are you?";

Escape Character
If you need to add character such as speech mars when echoing you will need to use an escape character.
echo "Did you say \"Hi Sam\"?";


Arithmetic Operators
Operator Name Example Result
1 Addition $x + $y Sum of $x and $y
- Subtraction $x - $y Difference of $x and $y
* Multiplication $x * $y Product of $x and $y
/ Division $x / $y Quotient of $x and $y
% Modulus $x % $y Remainder of $x divided by $y
** Exponentiation $x ** $y Result of raising $x to the $y'th power

Information extracted from W3schools

Comparison Operators
Operator Name Example Result
== Equal $x == $y Returns true if $x is equal to $y
- Subtraction $x - $y Difference of $x and $y
=== Identical $x === $y Returns true if $x is equal to $y, and they are of the same type
!= Not equal $x != $y Returns true if $x is not equal to $y
<> Not equal $x <> $y Returns true if $x is not equal to $y
!== Not identical $x !== $y Returns true if $x is not equal to $y, or they are not of the same type
> Greater than $x > $y Returns true if $x is greater than $y
< Less than $x < $y Returns true if $x is less than $y
>= Greater than or equal to $x >= $y Returns true if $x is greater than or equal to $y
<= Less than or equal to $x <= $y Returns true if $x is less than or equal to $y
<=> Spaceship $x <=> $y Returns an integer less than, equal to, or greater than zero, depending on if $x is less than, equal to, or greater than $y. Introduced in PHP 7.

Information extracted from W3schools

Logical Operators
Operator Name Example Result
and And $x and $y True if both $x and $y are true
or Or $x or $y True if either $x or $y is true
xor Xor $x xor $y True if either $x or $y is true, but not both
&& And $x && $y True if both $x and $y are true
|| Or $x || $y True if either $x or $y is true
! Not !$x True if $x is not true

Information extracted from W3schools


Single-line Comments
Multi-line Comments

if, else & elseif Statements

if Statement
if...else Statement
if...elseif...else Statement


While Loop
For Loop

Database Connections

The following code is used only for connecting to a database in plain PHP. This is not to be used when coding for Wordpress. Please see below for information on database connections for Wordpress.

Select Data
Insert/Update/Delete Data


Insert Data Into Database

Select Data From Database