Posts in category "PHP Learn"

Learn PHP: Includes & Requires

Continuing on from Learn PHP: Ifs & Elses, we can start learning about including other files, which is very helpful for things like sidebars on your website etc.

Include and Require include a file within another PHP script. There are four ways of doing this:


Now using them is simple. Lets say you have a file called “sidebar.php” with your links? All you have do do is the following:


One thing to remember is how to traverse directories in PHP. For instance, if my file “sidebar.php” is in a folder called “includes” you could go:


Or another approach, if my file “sidebar.php” is in the parent directory, you could go:


Now why are there four different ways to include?

Include and Require are practically the same, apart from the way they give out errors. Include will continue parsing your script and just log an error. Require however will stop the whole script to give you an error. Really it’s all down to what you want.

include_once and require_once only allow you to include the file once. This is not only more secure but stops from errors where the file was just included and variables were overwritten halfway through the script. Again they give out the same errors as include and require.

So lets go over one of each!


Posted on September 16, 2009

Learn PHP: Ifs & Else's

Continuing on from Learn PHP: Output And Operators, we can start learning about control statements which will make up the logic of your code.

Here is one in action:

$condition = true;
if ($condition) {
echo 'condition is true';

Simple enough to understand? First we are setting a variable $condition to = true. Then we test if that variable equals true, if so it will output anything within the curly brackets, in this instance it will output the statement condition is true.

Now if statements don’t seem on their own! What you really want is if / else statements. Setting up a similar example to the above, but with a number stored in the variable:

$condition = 10;
if ($condition == 5) {
echo "condition is 5";

Here the variable is set to 10. PHP Checks if it is 5. As we all know 10 is not 5, so PHP does nothing.

However in this statement, we add some more checks using the else if and else control words:

$condition = 10;
if ($condition == 5) {
echo "condition is 5";
} else if ($condition < 5) {
echo "condition is less than 5";
} else {
echo "condition is $condition";

In the above if the condition is 5, PHP will output that it is 5. It then checks to see if the condition is less than 5, and outputs if that is the case. Lastly, if it doesn’t match anything else it outputs what the condition is. So in this instance we would get the output condition is 10

One last example using strings just to highlight that they can also make use of this functionality:

$condition = "yes";
if ($condition == "no"){
echo "condition is false";
} else if ($condition == "yes"){
echo "condition is true";
} else {
echo "Something happened to the variable";

Here you can see that the condition is actually a word. PHP checks it just like it would a number and outputs all the same. Here PHP would output condition is true.

You can now move onto the next part Learn PHP: Includes & Requires

Posted on August 28, 2009

Learn PHP: Output & Operators

This series was severely outdated, as it was originally written in 2009, I have updated it with more modern options as of 12th July 2020

Continuing on from Learn PHP: Setup, we can start with a few of the basic principles / building blocks of the language.


The first thing you want to be able to do is output data to your console, we can do this by using the echo statement.

echo "hello world";


Comments are multipurpose they allow you to temporarily cause code not to run as well as make notes describing code functionality.

PHP supports two different ways of commenting, C/C++ style and Unix shell style (Perl Style). We will focus on C/C++ style here.

Example’s of echo via C++ Style are as follows:

// Comments starting with this are single line.
echo "hello";
/* Any comments within this combination of symbols
can span multiple lines
echo "world";

You can also use comments to temporarily cause a block of code to no longer run. In the example below the console won’t output anything.

echo "hello world";


You will use variables for a variety of things when programming. Essentially they let you store some data to either use or modify later on.

Here are two examples of variables, one containing a string, the other an integer.

$first_variable = "Variable One";
$second_variable= 1;


There are a number of operators you can use within PHP.

Arithmetic Operators

There are plenty of arithmetic operators for operating on numbers.

Here are an example of a few of the basic ones you’ll use frequently.

$add = 2 + 1;
echo $add; // Will output '3'
$subtract = $add - 1;
echo $subtract; // Will output '2'
$multiply = $add * 4;
echo $multiply; // Will output '8'
$divide = $multiply / 2;
echo $divide; // Will output '4'

String Operators

There are two string operators available in PHP. You’ll use these for joining strings together like below:

$variable = "Hello";
echo $variable . " world"; // Will output 'Hello world'
$variable .= " world";
echo $variable; // Will now output 'Hello world'

You can now move onto the next part Learn PHP: Ifs And Else’s

Posted on August 20, 2009

Learn PHP: Setup

This series was severely outdated, as it was originally written in 2009, I have updated it with more modern options as of 12th July 2020

Cloud based options

There are plenty of free cloud based editors that will allow you to get up and running without having to install anything on your computer. If you are just looking to try out PHP and aren’t sure you will continue using it, this is the best option.

Windows options


Wamp is a GUI installer that will guide you through installing a web server Apache a database mySQL, the PHP programming language and PhpMyAdmin which allows you to easily manage your databases.

If you are looking for an option for more long term use, this is the option I would suggest.


Xampp is another GUI installer, made by Apache. It will install a web server Apache, a database MariaDB and two programming languages PHP and Perl.

PHP Built-in web server

As of PHP 5.4 there is now a built-in web server available in PHP.

This means that you can now just go here and download the latest “Non Thread Safe” archive. You should then extract the archive to somewhere (eg C:\php) and then add that directory to your path as explained here.

This Stack Overflow answer contains answers that explain in detail the differences between “Thread Safe” vs “Non Thread Safe”.

OSX options

For Mac OSX the only easy option I’m aware of is installing via Homebrew, if you don’t already have Homebrew, it is a package manager that if you are getting into development you will want to have installed.

Once Homebrew is installed you can just run the following to install php.

brew install php

Linux options

If you are using Linux, you are going to need to utilise a search engine in order to get setup. Digital Ocean have good walkthroughs like this one for Ubuntu 18.04.

You can now move onto the next part Learn PHP: Output & Operators

Posted on July 07, 2009