PHP BEGINNERS GUIDE
All the basics you need to get started!
Contents
Introduction
Getting Started
Basic Syntax
Variables
Operators
Arrays
Control Structures
Functions
Pre-defined Variables
Expanding
Introduction
What's PHP?
PHP: Hypertext Preprocessor (PHP) is a free, open source, scripting language that is made primarily for the creation of dynamic websites. It is compatible with almost all modern servers and PHP scripts are always executed on the server. I would suggest a simple understanding of HTML which you can get a short taste of in this thread. If you want a more in depth look into CSS and other aspects of site creation (which I recommend you do before diving head first into PHP) take a visit to the w3schools HTML tutorial. This can be accessed here( You do not have permission to view the full content of this post. Log in or register now. ). I will not be covering any HTML elements within this tutorial.
What can it do?
PHP has a huge collection of uses such as:
Database management
Generating dynamic content on a page
Handling data from forms
Con:troll:ing and limiting site access to users
Data encryption
So much more!
The list of uses for PHP goes on and on. Because of this, it is the backbone behind some very big websites including Facebook and it powers the world's most popular CMS (content management system) WordPress.
Getting Started
Installing
To get started with learning PHP you first need to install or buy a web server to host the files on. For this tutorial we will be using XAMPP, this is a free tool which allows for an easy set up of a local web server. Download and install XAMPP from here. Once you have XAMPP open up the control panel and click on start for Apache and MySQL, these are the only 2 modules we will be needing.
Other than this you will also need a text editor for creating the PHP files. The two most common editors are Sublime Text and Notepad++. I personally use Sublime Text however both are good choice and you can get Sublime Text from here and Notepad++ from here ( You do not have permission to view the full content of this post. Log in or register now. )
Now that's all done we should probably check if your XAMPP is working. To do this we will make a very simple script just to check.
PHP Code:
<?php
echo "Welcome to PHP";
?>
Create a new .php file using the editor you installed and drop it into the XAMPP htdocs directory and call it "hello.php". If you are using windows and used the default XAMPP install directory, your htdocs folder will probably be at C:\xampp\htdocs\ if you can't find it. Once you've done all this go to your browser and type in You do not have permission to view the full content of this post. Log in or register now. and if you have followed all these steps you should be greeted with a page saying "Welcome to PHP".
Congratulations! You're now one step closer to becoming a PHP pro!
Before continuing (as requested by Kedox) I'd suggest reading this. It is the PSR-2: Coding Style Guide and will stop you from getting into bad habits while creating scripts. Making functional code is for the users benefit, you should make beautiful code for not just other developers who see your work, but also for yourself.
Basic Syntax
PHP Tags
All scripts start and end with these tags:
PHP Code:
<?php
//Code goes here.
?>
The PHP tags can be placed anywhere in your document.
Here is an example where I have used a function called echo to print out "Hey" on to a web page.
PHP Code:
<?php
echo "Hey";
?>
As you can clearly see, the PHP tags are indicating to my computer where my PHP code begins and vise-versa when I use closing PHP tags. They can also be opened and closed multiple times throughout the file.
Echo
PHP has a built in function called "echo" which has the job of outputting text. It would be a mistake to call it a function as it is actually a language construct, this means it does not require parentheses. As with all PHP statements, the echo construct must always end with a semi-colon ";" so it is clear where each line ends. We've already had two examples in this tutorial where we have used echo so go back and have a look at how they were written. All the outputs of echo are in plain HTML meaning you can use HTML tags within it.
PHP Code:
<?php
echo "<strong>This will output strong text!</strong>";
?>
Comments
A comment is a line or section of code that is not executed as part of the program. Comments serve the role of communicating to others and your self how certain bits of your code work or what they are used for. The first kind of comment is a single line comment. To begin one of these you simply do // and anything on that line after the slashes will not be executed. The other option is a multi-line comment, these are started with /* and end with a */. Anything between this two will not be treated as code. Let's have some examples of comments so you can understand exactly how they look in practice.
PHP Code:
<?php
//This is a single line comment. It only comments this line
echo "Hello world";
/*
This is a multi-line comment. It comments anything between the constructs.
*/
?>
That's all the most basic syntax out of the way. These were beginner constructs that you will need to know but don't fall into any other categories of the tutorial. It is also important to note that these constructs can't be changed. However, now you should be able to write a basic PHP script.
Variables
Regular Variables
Variables are containers which we use for storing information. PHP variables work a bit differently to other languages so I suggest you pay close attention here. A variable starts with a dollar sign ($) which is then followed by the name of the variable. That is then followed by a = sign and the value is entered after that. After the value you need a semi-colon as usual. Let's have an example.
PHP Code:
<?php
$var_name = 10;
?>
There are some important things you must remember when setting a variable name:
The name of a variable must start with a letter or an underscore.
Variable names are case sensitive (so $swag would not be the same as $SWAG).
A variable name cannot start with a number.
A variable name can contain only letters, numbers and underscores.
When defining a variable you do not have to tell PHP the data type. This is where it is a bit different to other languages and shares more in common with something like python as PHP looks at what you set the variable too and gives it a datatype based on that. Because of this, you also never need to declare a variable, it is created the moment you assign a value to it.
You can also use variables in other functions or constructs instead of directly typing in data. This comes in handy when you will be using a value more than once and don't want to have to retype it and/or when the value of it will be changing often. For example:
PHP Code:
<?php
$name = "Ciaran";
//Since I put in the quotation marks PHP knows that is a string.
echo $name;
//Now the echo construct goes and looks for variable $name and outputs it.
?>
Constants
Now I did make a rant thread (disguised as a tutorial) on constants a while ago and I do think they aren't used enough by beginners these days. But you don't care about that. A constant is similar to a variable except it is defined once then cannot be undefined or have its value changed. The same rules for making a constant variable name apply here.
To create a constant you use the define() function.
PHP Code:
define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant.
value: Specifies the value of the constant.
case-insensitive: Should the constant name be case sensitive? This is defaulted to false.
Example:
PHP Code:
<?php
define("HELLO", "Hello there friend.");
echo HELLO;
//Outputs "Hello there friend."
?>
Example using case sensitive:
PHP Code:
<?php
define("HELLO", "Hello there friend.", true);
echo hello;
//Outputs "Hello there friend."
?>
You can also define constants by using the const variable. This works in exactly the same way but is always case-sensitive and is defined a little differently.
PHP Code:
<?php
const CONSTANT = value;
?>
Instead of using define, you simple remove the $ from in front of a variable and use the keyword const in front of the variable name.
Data Types
A data type is the type of information stored within a variable. PHP has 8 main data types listed here:
String
Integer
Float
Boolean
Array
Object
NULL
Resource
Let's start with an integer. These are whole numbers without decimals that; don't contain commas or blanks, must not have a decimal point and can be positive or negative.
PHP Code:
<?php
$inta = 69; //Positive integer
$intb = -69; //Negative integer
?>
Next is a string where is a "string" of characters. It can be any text within a set of quotation marks (double or single). You can also join two strings together using a dot concatenation operator.
PHP Code:
<?php
$stringa = "Hello there buddy."; //You could also replace the " with '.
$stringb = "Nice weather today.";
echo $stringa . $stringb; //This will add b onto the end of a and output them.
?>
A float (floating point mumber) is a numeric variable like an integer except it includes a decimal.
PHP Code:
<?php
$a = 107.5;
?>
A boolean is a variable that can be represented in two possible states: TRUE or FALSE.
PHP Code:
<?php
$x = TRUE;
$y = FALSE;
?>
The other variables are a bit advanced for now and will be covered later in the tutorial. It's good to remember that most variables can be combined with each other and we will go into more detail about this later.
Variable Scope
The scope of a variable is the part of the script your variable can be referenced or used. There are three types of scope: local, global and static. A variable declared outside of a function has a global scope but can only be used outside of a function. Variables declared inside of a function with have a local scope and can only be used inside of this function. We will go into parsing local variables into global later using the return function.
PHP Code:
<?php
$var1 = 50; //This sets a variable with global scope.
function printNum() //Defining a function (will explain this more later)
{
$var1 = 49; //Sets this variable with a local scope.
echo $var1; //Will output 49 as it checks for variables with a local scope.
}
printNum(); //Calling the function.
?>
All the basics you need to get started!
Contents
Introduction
Getting Started
Basic Syntax
Variables
Operators
Arrays
Control Structures
Functions
Pre-defined Variables
Expanding
Introduction
What's PHP?
PHP: Hypertext Preprocessor (PHP) is a free, open source, scripting language that is made primarily for the creation of dynamic websites. It is compatible with almost all modern servers and PHP scripts are always executed on the server. I would suggest a simple understanding of HTML which you can get a short taste of in this thread. If you want a more in depth look into CSS and other aspects of site creation (which I recommend you do before diving head first into PHP) take a visit to the w3schools HTML tutorial. This can be accessed here( You do not have permission to view the full content of this post. Log in or register now. ). I will not be covering any HTML elements within this tutorial.
What can it do?
PHP has a huge collection of uses such as:
Database management
Generating dynamic content on a page
Handling data from forms
Con:troll:ing and limiting site access to users
Data encryption
So much more!
The list of uses for PHP goes on and on. Because of this, it is the backbone behind some very big websites including Facebook and it powers the world's most popular CMS (content management system) WordPress.
Getting Started
Installing
To get started with learning PHP you first need to install or buy a web server to host the files on. For this tutorial we will be using XAMPP, this is a free tool which allows for an easy set up of a local web server. Download and install XAMPP from here. Once you have XAMPP open up the control panel and click on start for Apache and MySQL, these are the only 2 modules we will be needing.
Other than this you will also need a text editor for creating the PHP files. The two most common editors are Sublime Text and Notepad++. I personally use Sublime Text however both are good choice and you can get Sublime Text from here and Notepad++ from here ( You do not have permission to view the full content of this post. Log in or register now. )
Now that's all done we should probably check if your XAMPP is working. To do this we will make a very simple script just to check.
PHP Code:
<?php
echo "Welcome to PHP";
?>
Create a new .php file using the editor you installed and drop it into the XAMPP htdocs directory and call it "hello.php". If you are using windows and used the default XAMPP install directory, your htdocs folder will probably be at C:\xampp\htdocs\ if you can't find it. Once you've done all this go to your browser and type in You do not have permission to view the full content of this post. Log in or register now. and if you have followed all these steps you should be greeted with a page saying "Welcome to PHP".
Congratulations! You're now one step closer to becoming a PHP pro!
Before continuing (as requested by Kedox) I'd suggest reading this. It is the PSR-2: Coding Style Guide and will stop you from getting into bad habits while creating scripts. Making functional code is for the users benefit, you should make beautiful code for not just other developers who see your work, but also for yourself.
Basic Syntax
PHP Tags
All scripts start and end with these tags:
PHP Code:
<?php
//Code goes here.
?>
The PHP tags can be placed anywhere in your document.
Here is an example where I have used a function called echo to print out "Hey" on to a web page.
PHP Code:
<?php
echo "Hey";
?>
As you can clearly see, the PHP tags are indicating to my computer where my PHP code begins and vise-versa when I use closing PHP tags. They can also be opened and closed multiple times throughout the file.
Echo
PHP has a built in function called "echo" which has the job of outputting text. It would be a mistake to call it a function as it is actually a language construct, this means it does not require parentheses. As with all PHP statements, the echo construct must always end with a semi-colon ";" so it is clear where each line ends. We've already had two examples in this tutorial where we have used echo so go back and have a look at how they were written. All the outputs of echo are in plain HTML meaning you can use HTML tags within it.
PHP Code:
<?php
echo "<strong>This will output strong text!</strong>";
?>
Comments
A comment is a line or section of code that is not executed as part of the program. Comments serve the role of communicating to others and your self how certain bits of your code work or what they are used for. The first kind of comment is a single line comment. To begin one of these you simply do // and anything on that line after the slashes will not be executed. The other option is a multi-line comment, these are started with /* and end with a */. Anything between this two will not be treated as code. Let's have some examples of comments so you can understand exactly how they look in practice.
PHP Code:
<?php
//This is a single line comment. It only comments this line
echo "Hello world";
/*
This is a multi-line comment. It comments anything between the constructs.
*/
?>
That's all the most basic syntax out of the way. These were beginner constructs that you will need to know but don't fall into any other categories of the tutorial. It is also important to note that these constructs can't be changed. However, now you should be able to write a basic PHP script.
Variables
Regular Variables
Variables are containers which we use for storing information. PHP variables work a bit differently to other languages so I suggest you pay close attention here. A variable starts with a dollar sign ($) which is then followed by the name of the variable. That is then followed by a = sign and the value is entered after that. After the value you need a semi-colon as usual. Let's have an example.
PHP Code:
<?php
$var_name = 10;
?>
There are some important things you must remember when setting a variable name:
The name of a variable must start with a letter or an underscore.
Variable names are case sensitive (so $swag would not be the same as $SWAG).
A variable name cannot start with a number.
A variable name can contain only letters, numbers and underscores.
When defining a variable you do not have to tell PHP the data type. This is where it is a bit different to other languages and shares more in common with something like python as PHP looks at what you set the variable too and gives it a datatype based on that. Because of this, you also never need to declare a variable, it is created the moment you assign a value to it.
You can also use variables in other functions or constructs instead of directly typing in data. This comes in handy when you will be using a value more than once and don't want to have to retype it and/or when the value of it will be changing often. For example:
PHP Code:
<?php
$name = "Ciaran";
//Since I put in the quotation marks PHP knows that is a string.
echo $name;
//Now the echo construct goes and looks for variable $name and outputs it.
?>
Constants
Now I did make a rant thread (disguised as a tutorial) on constants a while ago and I do think they aren't used enough by beginners these days. But you don't care about that. A constant is similar to a variable except it is defined once then cannot be undefined or have its value changed. The same rules for making a constant variable name apply here.
To create a constant you use the define() function.
PHP Code:
define(name, value, case-insensitive)
Parameters:
name: Specifies the name of the constant.
value: Specifies the value of the constant.
case-insensitive: Should the constant name be case sensitive? This is defaulted to false.
Example:
PHP Code:
<?php
define("HELLO", "Hello there friend.");
echo HELLO;
//Outputs "Hello there friend."
?>
Example using case sensitive:
PHP Code:
<?php
define("HELLO", "Hello there friend.", true);
echo hello;
//Outputs "Hello there friend."
?>
You can also define constants by using the const variable. This works in exactly the same way but is always case-sensitive and is defined a little differently.
PHP Code:
<?php
const CONSTANT = value;
?>
Instead of using define, you simple remove the $ from in front of a variable and use the keyword const in front of the variable name.
Data Types
A data type is the type of information stored within a variable. PHP has 8 main data types listed here:
String
Integer
Float
Boolean
Array
Object
NULL
Resource
Let's start with an integer. These are whole numbers without decimals that; don't contain commas or blanks, must not have a decimal point and can be positive or negative.
PHP Code:
<?php
$inta = 69; //Positive integer
$intb = -69; //Negative integer
?>
Next is a string where is a "string" of characters. It can be any text within a set of quotation marks (double or single). You can also join two strings together using a dot concatenation operator.
PHP Code:
<?php
$stringa = "Hello there buddy."; //You could also replace the " with '.
$stringb = "Nice weather today.";
echo $stringa . $stringb; //This will add b onto the end of a and output them.
?>
A float (floating point mumber) is a numeric variable like an integer except it includes a decimal.
PHP Code:
<?php
$a = 107.5;
?>
A boolean is a variable that can be represented in two possible states: TRUE or FALSE.
PHP Code:
<?php
$x = TRUE;
$y = FALSE;
?>
The other variables are a bit advanced for now and will be covered later in the tutorial. It's good to remember that most variables can be combined with each other and we will go into more detail about this later.
Variable Scope
The scope of a variable is the part of the script your variable can be referenced or used. There are three types of scope: local, global and static. A variable declared outside of a function has a global scope but can only be used outside of a function. Variables declared inside of a function with have a local scope and can only be used inside of this function. We will go into parsing local variables into global later using the return function.
PHP Code:
<?php
$var1 = 50; //This sets a variable with global scope.
function printNum() //Defining a function (will explain this more later)
{
$var1 = 49; //Sets this variable with a local scope.
echo $var1; //Will output 49 as it checks for variables with a local scope.
}
printNum(); //Calling the function.
?>