Thursday 6 September 2012

Brand new HTML and PHP Tutorial

Hi all

You can find a brand new tutorial website specifically for HTML and PHP development. It's in its early stages at the moment but you can find it at TeachMe.webege.com It covers HTML basics, CSS and PHP. You'll learn all about loops, arrays, design and much more.

Come check it out now at TeachMe.webege.com

Monday 2 July 2012

All about PHP loops

So maybe you read my last post about arrays here, or maybe not. If you want a quick catch up or to learn something new check it out.

Quick recap. I'm a web developer at Computer Related. And I love web developing.

This post is about loops in PHP, it's not designed to be a be all you need post, but more of a reminder or a basic lesson to get you going.


I have many more tutorials and self help pages over at teachme.webege.com.


What is a loop?

A loop is exactly what it sounds like, something going around and around. For our purpose a loop is something that gets executed over and over again


Why do I need a loop

If you want to do anything more than once then more than likely you need a loop of some kind (not all the time).


Types of loops

In PHP we have four type of loops. We have a FOR loop, FOREACH loop, WHILE loop and a DO WHILE loop

.

How do I know which one to use

Once you understand the principles of a loop you'll know, which we'll go through next.


The principles

Imagine the scenario below

I want to print 10 numbers on seperate lines to the screen. And I may want to change that to 100 in the future.

You could just write 10 numbers in html, and if needed write another 90. But what if we then wanted 1000, this becomes more tedious. Sounds like a loop is needed.

What about if I created a loop that went round the amount of times i wanted, and each time it printed out a number to the screen. Now something like this could work with any amount of numbers.

Ok, lets do it.


for(i = 1; i <= 10; i++){
 echo i."/n";
}

Output =
1
2
3
4
5
6
7
8
9
10


i=1;
while(i <= 10){
 echo i."/n";
    i++;
}

Output =
1
2
3
4
5
6
7
8
9
10


do{
 i=1;
 echo i."/n";
    i++;
}while(i <= 10);

Output =
1
2
3
4
5
6
7
8
9
10

OK. So some of you may be thinking what the *, and some may be thinking i remember this. Let's get us all on the same page.

For a start, the above code is correct other that instead of '$i' i have used 'i' so it reads easier. 'echo' simply means write out the line, '/n' means start a new line.

Notice in all the loops 'i' is the index, and we use this to count how many times the loop has gone round. We also need to tell the loop to stop at some point, that is what the '10' is doing, it says once 'i' is equal to 10 stop the loop. The important thing to notice here is that we could change the '10' to '100', '1000' or what ever we like. See how the loop makes life simpler. Another thing to notice in all the loops is the fact that 'i' needs to be incremented, 'i++' increments 'i' by 1, or in lay terms adds 1 to 'i'.

So there we have the basics needed for a loop, a start number, a max number and a way to increment start number.

So what's the difference

Not much between a 'while' loop and a 'for' loop other than syntax. The 'for' loop initialises all its variables and conditions within the 'for' statement, whereas the 'while' loop only states the condition, the variables are initialised outside of the 'while' loop and incremented within.

The 'do while' loop and 'while' loop differ only slightly, the 'while' loop checks the loop conditions before entering the loop, whereas the 'do while' loop checks the loop conditions at the end of the loop. So in the examples above if i say that 'i = 11' then in the 'while' loop nothing is printed as it checked if 'i' was equal too or less than '10', whereas in the 'do while' loop it would print '11' because 'i' is checked after the echo statement.

What's the fourth loop type you mentioned?

Glad your still here and concentrating. The 'foreach' loop is specifically used for arrays, not to say the the others can't be used for arrays.

The 'foreach' loop is like the 'for' loop but all we need to declare is the array to loop and the output variable, see below to understand.


$array = array(1, 2, 3);

foreach( $array AS $number){
 echo "Number: $number /n";
}

output =
Number: 1
Number: 2
Number: 3

If i where to do the above using a 'for' loop it would look like below


array = array(1, 2, 3);
num = count(array);
for( i = 1; i <= num; i++ ){
 echo "Number: i /n";
}

output =
Number: 1
Number: 2
Number: 3

Did you notice the 'num = count(array)', count() is a very valuable PHP function, which counts the number of elements within an array, so above we would get 3.


At the moment that's all I can think of pertaining to loops, if I forgot anything or you think I should add something then please comment or email or send out smoke signals lol.

Friday 29 June 2012

All about PHP arrays

Why write this ? Because I'm a web developer who never stores anything in his head, especially since google made searching the web even easier.

Anyway like I said, i'm a web developer at Computer Related (www.computerrelated.co.uk), in the UK. Well in Birmingham, West midlands to be precise.


I have many more tutorials and self help pages over at teachme.webege.com.

The basics

What is an array? I could get all technical up in here, but why! Really all you need to know is that an array stores multiple pieces of data in one variable.

OK maybe I need to be a bit more technical ;-) I find examples work best

Imagine the sum below

3 + 3 = 6
Easy. But long winded to change.

Now imagine this

x + y = z
$x = 3, $y = 3 and $z = both x plus y
Now we are working with variables. We can make x and y anything and z will always add up. Cool.

So what has this got to do with arrays? Everything. An array is made up of multiple variables. See.

For the next example I will create an array and add some data.

$array = array(4, 5, 8)
Now we have
$array[0] = 4, $array[1] = 5, $array[2] = 8

Do you understand what just happened? the variable $array was given 3 pieces of data 4, 5 and 8. The data is automatically stored in indexed order i.e 1, 2, 3, 4, etc.

So to access the data i just need to use $array[index], index being the actual index number.

Hold on, so why does it start from 0

Well spotted, we naturally count from 1 to 10 discounting 0, but computers count from 0 to 9. Hence 0 being the first index.

So what can I use an array for really

Well primarily instead of having for instances of 5 variables you could have one array, see below for example.

$one, $two, $three, $four, $five
or
$numbers[index]

We can also do cool stuff like use them in loops, but since I haven't discussed loops yet i'll leave that for now.

Back to arrays, lets say you want declare an empty array so you can add data later

$foo = array()
Now you can add to it like this
$foo[] = 23
That will add data to next free slot in array.

I don't want to use numbers for my index, i'd prefer to use words. OK, that can be done. It's called an associative array

$array = array("one" => 1, "two" => 2, "three" => 3, "four" => 4)
Now
$array['one'] = 1, and $array['two'] = 2, etc.

Suppose i have an associative array and I want it to be indexed instead? Of course we got a solution for that too.

$new_array = array_values($array)

Oh my days, I know that's what your thinking, yes it's that easy.

OK, how do i shuffle the array? If your not in PHP then long ting, but in PHP it's as quick as a quick thing lol.

$new_array = shuffle($array)

Done. I know, this PHP stuff is easy. Be carefull though, shuffle changes the index completely, wiping over old indexes.

Ok, you got this far so i'm going to give you 2 extra tips, not directly array related but near enough.

explode() is used to seperate a string at specific points (known as delimeters), i.e comma, fullstop, space, etc. Once exploded the data is then stored in an array.

$string = "hello, clarent, from, www.computerrelated.co.uk"
$array = explode(',', $string)

Now we have
$array[0] = hello, $array[1] = clarent, $array[3] = from, etc

Another cool tip is - Every string variable is an array. So

$string = "hello", then $string[0] = "h", $string[1] = "e", etc

If i've missed any usefull array stuff out please let me know so we can all grow.