My PHP journey part 5.

Arrays

An array, is a variable that has more than one value. This is known as a compound data type.

They’re useful because they can store stuff.

They look like this.

$months [0] = ‘January’;
$months [1] = ‘February’;
$months [2] = ‘March’;
$months [3] = ‘April’;
$months [4] = ‘May’;
$months [5] = ‘June’;
$months [6] = ‘July’;
$months [7] = ‘August’;
$months [8] = ‘September’;
$months [9] = ‘October’;
$months [10] = ‘November’;
$months [11] = ‘December’;

The number in the square brackets is the order of the values, fairly obvious – it does have a fancy name – ‘array operator’.

But they can so look like this, in which case php will assume the number for you – counting from zero…

$shopping = [
‘bread’,
‘milk’,
‘butter’,
‘pasta’,
‘cheese’,
‘bin bags’,
‘oranges’,
‘apples’,
‘washing up liquid’,
‘rice’
];

So if you were to

echo $shopping[0];

The ‘bread’ will appear.

An array operator can also be a string key. For example:

$dog[‘name’] = ‘rover’;
$dog[‘age’] = 3.5;
$dog[‘eats’] = ‘shoes’;

Indexed arrays

An indexed array will look like this:

Array
(
[0] => tidy house
[1] => walk dog
[2] => go shopping
)

Where as an associative array will look like this:

Array
(
[name] => rover
[age] => 3.5
[eats] => shoes
)

The hash rocket => (sounds cooler than what it actually is!) means in php “Has the property” but will mean something different in other scripting languages.

Associative Arrays

Much like the above indexed arrays – except you can let php do the some of the work.

They look a bit like this.

$nikki = array(
‘first_son’ => ‘Ryan’,
‘second_son’ => ‘Liam’,
‘third_son’ => ‘Callum’,
‘first_daughter’ => ‘Jessica’
);

//In English, this reads “Nikki has some children, her first is Ryan, her second son is Liam…” And so on. 

php is doing the indexing for you… Hooray!

 

Outputting arrays

It’s all well and good having arrays which store loads of data, but how do you get stuff out of them?

Okay so you have the following array:

$joe = [‘eats’ => ‘pizza’, 

‘siblings’ => ‘two brothers’, 

‘hobby’ => ‘hang gliding’];

You could then write a script which says:

<p>Joe’s favourite food is <?php echo $joe[‘eats’] ?>, which he likes to eat with his <?php echo
$joe[‘siblings’] ?> while <?php echo $joe[‘hobby’] ?></p> 

It’s the same with indexed arrays, except you’ll need to put the number instead of the word. (Some hardcore php-ers will say this looks clumsy – but at this stage it doesn’t matter).

Now there’s a whole bunch of stuff you can talk about on arrays, including extracting the values and the labels using a foreach construct, plus multi-dimensional arrays and nested arrays.  But I think I’ll save this for another blog post.  I’m still trying to get my head around the above!

My PHP journey part 3.

Loops

Today we’re going to learn about loops. I’ll be honest, It’s taken me about a week and a half to understand what loops are and what they do.

When my tutor introduced the subject by saying something on the lines of “Hooray! Today we’re doing loops!”

I was I felt an hooray – instead I felt like my head was going to explode. And what was worst is that I think the rest of the class managed to grasp it.

Anyway.. onwards and upwards…

The While Loop

Apparently, this is the simplest type of loop.

It looks like this…

while ( condition ) {
code to run
}

If the condition evaluates to true then the code (the bit in curly brackets) will run.

A simple example we used in class was counting to ten.

$i = 1; //The value of $i… simple enough so far

while ($i <= 10) { //While $i is less than or equal 10…which it is… we’ve already defined it..
echo “$i”; //show $i from now on…
$i++; //Now, we’re adding an increment to $i

} //Close curly brackets.

Remember, you won’t see any number higher than 10, because you’ve told it not to count that high using the <= symbol. Less than or equal to.

The Do While Loop

It looks like this..

do {
code to run at least once
}
while ( condition );

The bit in the ‘do’ brackets is run before the condition is evaluated.

For example

$i = 10; //$i is 10… simple enough
do {
echo $i; //Show me 10..
} while ($i < 10); //Now you’ll just see 10…

I’m not sure of the point of this one, hopefully the penny will drop soon.

The For Loop

This looks like the most useful, but the most difficult to comprehend…

It looks like this.

for ( expression1; expression3; expression4 ) {
code to run
}

Expression 1 – This is only looked at once, so it sets the initial values, for example $i = 10;

Expression 2 – This is evaluated before the code and a boolean, it’ll be either true or false

Expression 3 – This is executed at the end of the loop and can change the value of expression 1… Creating the said loop.

For example

for ($i = 1; $i <= 10; $i++) {
echo $i;
}

So… $i equals 1. If $i is less than or equal to 10… Add another figure next to $i… Counting to 10.

If the middle expression evaluates to true then you’ll see something. In the case above, you’ll see the counter from 1 to 10.

The structure of an HTML document

When you’re writing a HTML document, there is a structure to follow.  You can create these documents using a simple text editor such as Notepad or Notepad++

The HTML document looks like this…

<!DOCTYPE HTML>
<HTML>
<HEAD>
<TITLE>TILE</TITLE>
</HEAD>
<BODY>
<B>CONTENT</B>
</BODY>
</HTML>

This looks pretty complicated at first glance. But let’s break it down…

<!DOCTYPE HTML> Very simple, this is just what the document type is, it’s a html document.

<HTML></HTML> This is an open HTML tag, this surrounds the document, it’s all it does. It goes at the top and at the bottom.

<HEAD></HEAD> Here you would put all of your meta data, java or styling into the web page.  Don’t worry, we’ll come to those later.

<TITLE></TITLE> This is the title of the page, this is what would appear at the top of your browser window.

<BODY></BODY> This is the actual content of the document.

Once you save this, be sure to save it with a .html suffix, so for example…. “something.html”

Read up on how to create things inbetween the <BODY> tags here.

An introduction to HTML

HTML stands for…

Hyper Text Mark-up Language.

HTML is made up of…

– Text content (what you see)
– Markup (what it looks like)
– References to other documents (videos, images…etc)
– links to other pages

Let’s get started.

The HTML markup – tags and elements

<name> content </name>
HTML can be made up of opening tag and a closing tag

The whole of the above put together is known as an element.

<b>Contents</b> – b for bold.
<em>Contents</em> – em for emphasis or italics.

HTML Attributes

Attributes have a name and value.

For example:

<tag attr=”value”>Contents</tag>

The most common attribute, is the link.

<a href=”http://www.whatever.com“>derp</a>

<a> stands for anchor.
href‘ is the attribute name, ‘www.whatever.com‘ is the value.

Image tags

<img> – images’

SCR is the source of the image, either your server or someone else’s.
ALT is the text which isn’t displayed when the image doesn’t load. Believe it or not, it’s actually for blind people.

The whole image tag looks like this…

<img src=”url or a link goes here” alt=”text”>

There is no closing end tag. This is known as a void tag, there is no content, but there is, if you know what I mean!

Images appear in-line with text, unless specified.

Whitespace

You can force lines to show underneath each other and create a whitespace or a line.

<br> – again it’s a void tag.

You can also use a <p> tag. P for paragraph. This is not a void tag. It looks like this.

<p>content</p>

Why do we have two different ways of doing the same thing?

The <b> tag is inline, everything is line based. Where as <p> is known as a block tag and that creates a box around the text and pushes things outside of it down a line.

SPAN and DIV’s

<span>text</span> is an inline tag.
<div>text</text> is a block tag.

They are both ways of keeping content in shape, similar to the <p> and <br> tags.

A <div> will create a new line and push everything down a peg. Much like the <p> tag.

Want to see what an HTML document looks like? Click here.