Arrays

In our previous post we’ve seen that values can be stored in variables. But what if you want to group a set of variables into 1 variable. For this case we can use an array.

You can see an array as something like you filesystem (Windows Explorer or Mac Finder). It has a top-level folder and can nest various subitems. For instance you can have an array with categories in 1 single variable called $categories. We would set it up like this:

<?php
$categories = array('my first category', 'another one', 'etc');
?>
view raw arrays.php This Gist brought to you by GitHub.

In the example above we add 3 categories to the $categories variable we can get the category values by utilizing the code below:

<?php
echo "My first category is " . $categories[0]
?>
view raw arrays2.php This Gist brought to you by GitHub.

As you can see we added the variable with a zero in square brackets. Items in an array will by default start to count at 0. These numbers can be a bit confusing and therefor we can also use names when we create an array. Let’s see how that works:

<?php
$categories = array(
'first' => 'my first category',
'second' => 'new category',
'third' => 'etc...'
)
 
// Show the second category
echo "The second category is " . $categories['second'];
?>

As you can see we assign a name to the value, these are called key-value pairs. You can now access the value within the array using this key.

In a later course we will look how to loop through the array when we do not know how many keys there are.

Nested arrays

Now we have mastered how we can use arrays we can also have a look at another powerful feature which is nesting. Actually it is nothing more than putting an array in an array. The code below will probably make it all clear.

<?php
$shop = array(
'electronics' => array(
'stereo set',
'speakers'
'tv'
),
'software' => array(
'games',
'office',
'graphics'
)
);
 
echo "I want to buy a " . $shop['electronics'][0];
?>

As we can see we have used associative and non-associative arrays here. We access the top level array by adding ‘electronics’ and get the first element within electronics by giving it a key 0.

Try and play some with arrays because they will be very commonly used in your future projects.

Variables and constants

This is the chapter where things are going to get exciting; PHP has 2 default placeholders in which it can store data these are the variable and the constant. The main difference between these 2 is that the value of a variable can be changed during code execution, while the one of a constant will always remain the same. So the main question you should ask yourself during creation of your code (or even better, before you start coding) is; will this value ever change?

Creating a constant value

To create a constant the statement called ‘define’ is used which takes 2 parameters; the name of the constant and the value of the constant, just take a look at the example below for clarification.

<?php
define('MY_CONSTANT_NAME','The value of my constant');
echo MY_CONSTANT_NAME;
// This will show 'The value of my constant' on your screen
?>
view raw constant.php This Gist brought to you by GitHub.

On line 1 we start by telling we are going to use PHP code.
Line 2 is where the magic happens, we fill the constant called MY_CONSTANT_NAME with the value ‘The value of my constant’
Line 3 finally shows the content of MY_CONSTANT_NAME by using the echo statement.
Line 4 only has some comment which will be ignored by php, it is only used to let other programmers know what you are doing. After the 2 // could be any text you’d like.

It is best practice to use uppercase names for constants, this makes them easily recognizeable within your code.

Many common uses of constants are website data (web shop name, website owner phone number, website owner name, etc), fixed prices like P&P or transaction costs, but it could ofcourse be just anything that you don’t want to change while running your code.

Variables

Variables are pretty similar to constants, with the main difference being that their values can be changed. To name a variable we can use the following piece of code:

<?php
$newVariable = 'Hello World';
echo $newVariable;
?>
view raw variable.php This Gist brought to you by GitHub.

In the example above we assign the text ‘Hello World’ to the variable $newVariable in the first line. In the second line we use the echo command to show on screen what is in our variable.
Now here comes the interesting part, changing a variable:

<?php
$newVariable = 'Hello World';
echo $newVariable;
$newVariable = 'Goodbye World';
echo $newVariable;
?>

As we execute the code above we see that we do the same actions twice. On screen this should result in ‘Hello WorldGoodbye World’ as we echo the content of our variable twice but change it’s content before echo-ing it the second time.

 

This opens up a lot of possibilities which makes PHP so powerfull, in the next chapter we will be talking about operators which you can use to manipulate these variable contents by for example making calculations. But before we go there, there is just one more final piece of understanding needed.

Data types

Variables in PHP can be of different data types, by default PHP tries to guess the best data type based on your input. But for certain tasks understanding of these data types is very impotant. Like making calculations, if you would calculate with the number 15 as a text it would lead to different results as calculating with a numeric number 15.

This article is still in draft mode, the final content will be added in the near future.

What is PHP and what do I need to use it?

In this course we will explain what the PHP language is and for which purposes you can use it. We will also cover the easiest and fastest way to setup your PHP environment to get you started at creating your own scripts. After completing this course you should be capable of setting up a working PHP environment and knowing what you can do with it. In the following courses we will cover how to use the PHP language.

PHP is a server side language which means that all the tasks it handles get handled on the server. For example; you are viewing this website, you are on the client machine, through the internet you connect to a webserver, the server analyses your request (which is the URL you are at) and processes the code, to return to you (the client), a page with the information you have requested.

To get started with PHP all you will need is a webserver running the PHP software, database software is optional but often used. There are different types of servers available. For testing and following this course I would recommend one of these depending on the operating system you are working with:

After you are all setup with the application or any other server that suits you we are ready to move on to the next course ‘Writing your first PHP script’.

Writing your first PHP code – Hello World

Almost every coding course starts with it, the all-famous Hello World program. All it needs to do is display the message “Hello World” on the screen using the language you are starting out to learn.

To get started you will need a text editor (not a word processor) and write the following lines in a new document:

<?php
echo "Hello world!";
?>

Now save this file to the root of your webserver and name it ‘helloworld.php’.
If you open up a webbrowser and browse to your local server appended with this filename (in many instances this will be http://localhost/helloworld.php when working on a testing server) you should only see “Hello World” if the screen remains empty it means that you will need to troubleshoot your server because it isn’t interpreting the PHP code.

And there you are, you have just written your first piece of PHP code! Ofcourse you could write only the text to a file, but showing text is essential in coding and testing. In the following chapters we’ll go further into the dynamics of PHP which make this (and any other language) so powerfull.