advertisement

Alright, so my last PHP basics tutorial seemed to help quite a few people, and that tutorial only tipped on the edge of the iceberg. I thought I would create another PHP basics tutorial seeing as how the last one was more helpful!

This tutorial will cover more topics, and will be a bit more advanced then the last one. If you are completely new to PHP, you might want to read my other PHP basics tutorial, that one will get you to the point you need to be for this tutorial.

The last tutorial covered –

1. Conditions (if statements)
2. Loops
3. Basic Functions

This tutorial will cover –

1. Basic Arrays
2. Including Files
3. More Functions

If you have read my last PHP basics tutorial, you will know how to use if statements, loops and some basic functions. This tutorial is going to expand on that and teach you some basic array stuff, how to include files, how to send a basic e-mail and using some more intermediate functions.

Arrays

Alright, let’s jump right into it, arrays in PHP are a great way of storing lots of organized data. You may use arrays in PHP already and not know it yet! There are things called superglobal arrays ($_POST, $_GET, $_SERVER, $_COOKIE, $_FILES, there are more but these are the most popular). These are superglobal arrays and are used in almost every single PHP script you will ever make. If you don’t use them don’t worry your not doing anything wrong:)!

Now, these aren’t the arrays that we will be talking about. PHP allows you to have 2 types of arrays, indexed arrays, which uses numbers as keys to reference each value in the array, and associative arrays which uses words as keys to reference each value in the array. With the indexed array, the first index in your array will be set at 0 unless you set this to something different on your own. This is the case though in most programming languages when you are using arrays though.

<?php
$array[] = 'Sean';
$array[] = 'McKnight';
$array['website'] = 'TutorialCode';
?>

This would create an array, and assign my first name, last name and website name (tutorialcode) to it. As you can see, I add a word as the key. So that when you want to access what my website is you can use –

echo $array['website'];

Using words as keys can be a good way to remember what a certain array value holds. It is easier to remember what your website is by naming its value website, then having to remember a certain number.

Alternatively instead of defining each and every array element separately you can just use the array() function –

$provinces = array('QC' => 'Quebec', 'BC' => 'British Columbia', 'AL' => 'Alberta');

Now, if you wanted to use this array you could just echo out which one you want, here is an example –

echo $provinces['AL'];

This would print out Alberta, since you used AL as the key for Alberta. When using this function, you do not have to define keys to each value –

$provinces = array('Quebec', 'British Columbia', 'Alberta');

This would be the same thing as the array we created just before, except instead of using words as keys (such as AL for Alberta) it would be numbers. In this example the number 0 would output Quebec and so on…

Now, what if you wanted to access every value in an array, you can’t always remember every single key or value you have in an array. So using a loop (which we learned about in the first PHP basics tutorial) called the foreach loop, this is specifically made for using arrays.

foreach ($array as $value) {
	// Do something with the $value variable
}

An example of this would be –

foreach ($array as $value) {
	echo 'This value in the array is '.$value;
}

You can also access the key for each value in the array using the same loop, just changing the condition a bit –

foreach ($array as $key => $value) {
	echo 'This value in the array is '.$value.' and the key for that value is '.$key;
}

This is just a basic introduction to arrays, in the next portion of this series I might touch on arrays once again, but I also might not!

Including Files

In PHP including files is a huge part of it. It might not be the most complicated part, but it can sure as hell help you develop your scripts! It can make your life much easier, and make your scripts more organized as well as make it faster for coding scripts! First I will show you how to include files, and the different examples as well as a real life example of how it can help you improve your scripts and make it faster and easier for you as well. PHP has 4 functions that you can use to include files into your scripts they are, include(), include_once(), require() and require_once().

Each one has a difference obviously, not a big difference but a difference none the less. Using include() you can include a file into your script as many times as you want, and if they file doesn’t exist your script will just show an error and won’t stop.

With the include_once() function you can include any file, and if it doesn’t exist it just shows the error but continues to execute, but if you try to include the same file twice it will show another error for that.

Then there is the require() function, this function requires that the file you are including can be found. This is good if you are including a crucial file, if the file you are trying to include using require() doesn’t exist it will show an error and stop executing the script so no more of the code after the require will be executed.

Finally, our last include function is require_once() its a mix of require and include_once(), its basically using require() except that if you try to include the same file again it will show an error and stop executing your script.

include('file.php'); // In the same directory

include_once('path/file.php'); // In a directory 1 higher

require ('../file.php'); // In a directory 1 lower

require_once('../../file.php') // 2 directories lower

Using includes in your script can benefit you in many ways. Like most tutorials on this website, the scripts require you to use a MySQL database to store information. Before you use the database you need to connect to it. This takes a few lines of code, and why add it to the top of every single file in your script, when you can just make it in 1 seperate file and use a simple 1 line include at the top of the file you need a MySQL connection in! Check out some of the other tutorials on this site and you will see what I mean.

Another example of how to use this would be for custom user created functions. Let’s say you have a lot of user created functions and you want to use these functions in a lot of different files around your script. Just include the function file into the file you want to use a function for (thats a tongue twister) and then you can use whatever is in the included file. Including a file into a script is just like taking all the code from the file and adding it to your script, except your doing it with 1 file.

More Functions

Just for fun, I thought I would include 1 or 2 more functions.

1 really good function to use is is_numeric() function. This can help your script for security in many ways.

<?php
$num = 3

if (is_numeric($num)) {
	echo 'It passes the test!';
} else {
	echo 'It fails the test.';
}

$num = 'Hello';

if (is_numeric($num)) {
	echo 'It passes the test!';
} else {
	echo 'It fails the test.';
}
?>

This little bit of PHP uses the is_numeric function in a basic way, it checks to see if the $num variable is a number (integer) or not. In the first if statement, it would out put It passes the test! $num is a number, so thats why it passes.

In my second example though, it would show It fails the test. $num is set to a string, so the is_numeric() function would show FALSE.

The last function I will discuss is in_array(), seeing as how we were working with arrays before I think this is appropriate!

<?php
$provinces = array('Quebec', 'British Columbia', 'Ontario');

if (in_array('British Columbia', $provinces)) {
	echo 'Yay for British Columbia!';
}

if (in_array('quebec', $provinces)) {
	echo 'Yay for Quebec!';
}
?>

This creates an array and sets it to the variable of $provinces. Then when you use the in_array(), you need 2 arguments minimum. The text that your looking for in the array, and the array that your looking through.

This would output Yay for British Columbia because it is found in our array. You would think that our second test would pass, but using in_array() is case sensitive, so quebec does not match Quebec.

Well, I hope you have learned a lot from this tutorial read the first part of this series to get a better grasp on PHP if you are really new to the whole PHP thing.

I hope you learned a lot and continue to use TutorialCode, if you have any questions about this tutorial or even just PHP in general feel free to leave a comment or contact me using the form on this site.

Thanks!
Sean
TutorialCode.com

advertisement