My PHP journey part 8 (through to 9).

I’ve merged the 8th and 9th instalment into 1 single article…

MYSQL is often ushered in the same breath as PHP as the two are meant to work very well together.  MYSQL is a relational database system.  What do databases do? Store data, and then you can do as you wish with this data.  On my course we’ve been using the advanced terminal to get a grip with things – this has it’s pros and cons, the pros probably out weighing the cons in the long term.  The pros being that we are being taught interchangeable skills – skills which can go a long way in the work place. The cons being, if you haven’t dealt with this before, you can find you brain panicking as you really do need to build up your confidence when dealing with this sort of application.

Now throughout this part of my course (and I type this with as much respect for my tutor as possible, as she is trying to cram as much in as possible – however, quart and pint pot comes to mind!) I reverted to looking at the manual, located here and following it step by step.  This doesn’t have any reflection on my tutor and what she knows or how she teaches, but this was something I wanted and needed to take in more slowly, and with all of the other stuff I’ve learned so far, have a free reign to be creative and do as I wish with this application and hope that this application comes in handy at some point (which no doubt it will).

Instead of regurgitating what it says on the above links (and you should really take a look at what they have to say before you start reading anything about PHP/MYSQL from my view point) here is some text from MYSQL, about what it is!

[box color=”green” align=”left”]

MySQL is the world’s most popular open source database software, with over 100 million copies of its software downloaded or distributed throughout it’s history. With its superior speed, reliability, and ease of use, MySQL has become the preferred choice for Web, Web 2.0, SaaS, ISV, Telecom companies and forward-thinking corporate IT Managers because it eliminates the major problems associated with downtime, maintenance and administration for modern, online applications.

Many of the world’s largest and fastest-growing organizations use MySQL to save time and money powering their high-volume Web sites, critical business systems, and packaged software — including industry leaders such as Yahoo!, Alcatel-Lucent, Google, Nokia, YouTube, Wikipedia, and Booking.com.

The flagship MySQL offering is MySQL Enterprise, a comprehensive set of production-tested software, proactive monitoring tools, and premium support services available in an affordable annual subscription.

MySQL is a key part of LAMP (Linux, Apache, MySQL, PHP / Perl / Python), the fast-growing open source enterprise software stack. More and more companies are using LAMP as an alternative to expensive proprietary software stacks because of its lower cost and freedom from platform lock-in.

MySQL was originally founded and developed in Sweden by two Swedes and a Finn: David Axmark, Allan Larsson and Michael “Monty” Widenius, who had worked together since the 1980’s. More historical information on MySQL is available on Wikipedia

Our Continued MySQL Values

We want the MySQL database to be:

  • The best and the most-used database in the world for online applications
  • Available and affordable for all
  • Easy to use
  • Continuously improved while remaining fast, secure and reliable
  • Fun to use and improve
  • Free from bugs

 

We want the people working on MySQL to:

  • Subscribe to the Open Source philosophy
  • Aim to be good citizens
  • Prefer partners that share our values and mindset
  • Answer email and give assistance to users, customers, partners and co-workers
  • Be a virtual organization, networking with others

[/box]

Source: MYSQL

My PHP journey part 7.

Laying out a page template is kind of interesting – and kind of more in-line (from what I understand) with building a WordPress theme. This is going to be a short blog post because I am already embarking on something a bit more exciting… where this could be explained in a bit more detail.

Take a look my article about HTML to get a basic gist of this…

<!doctype html>

<html lang=”en”>

<head> <!– Google likes <title>’s contents to be in desc. order of specificity to the page –>

<title><?php echo $pageTitle; ?> | my blog</title> <meta charset=”utf-8″ /> </head> <body>

<h1><?php echo $pageTitle; ?></h1>

<?php echo $titles; ?>

</body>

<footer>

<?php get_footer; ?>

</footer> 

</html>

Again, this is a very rough generalisation, so any hardcore coders out there – resist the urge to punch the screen, or me! I’ve got some very specific plans for what I want to do with what I am learning and my next project.

I’ll back soon with some awesomeness!

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.

My PHP journey part 2.

So we just started to tackle operators. We looked increment and decrement… now we’re going to look at comparison operators.

Comparison Operators

The way in which I am remembering how these operators work is by simply remembering – comparisons. Same as, equal to, identical to, not the same as and so on.  It’s funny, equal to is not the same as identical to.  Something worth remembering.

So the symbols and meanings are as follows.

== …. “is equal to” (ie, values are equivalent; data types may be different though)

!= …. “is not equal to”

<= …. “smaller than or equal to”

=== …. “identical to” (both value AND data type are the same)

We can use these to make decisions because they equate to true or false data types.  How and when something is allowed access or even allowed to work. We’ll get to this later with a logical operator.

So for example…

$a = 4

$b = 8 

1+ 3 == $a ….//true

2 * 4 === $b//true

…And so on.

Truthy and Falsy 

Boolean data types equate to True and False – and these will determine whether the rest of script will run.  They have some simple atrributes that are worth remembering.

False:

  • Boolean false
  • the integer 0 (zero) and float 0.0
  • null
  • the string “0”
  • an empty string
  • an empty array

Truth:

  • Boolean true
  • Any string apart from the “falsy ones”, eg “string”
  • Integers apart from the above – e.g. 1, -123.45

 Conditional Statements 

PHP uses conditional statements to decide what to next.  It evaluates, operates and thinks, if this is X then… do something, if it’s not do something else

The main conditional statements are:

– if

– if…else

– if…else if

– switch

A piece of conditional statement code looks like this.

if ( expression ) {
piece of code here which equates to true or false will run
}

Think about this… You’re literally writing your own logic.  Is something not equal to… you could write it that if something is not equal too, but is true? For example:

 

$name = ‘Bill’;

if( $name != ‘Bert’ ){

echo ‘You’re not Bert!’;  //This will echo because != means ‘not equal to’ 

}

You can invert or shortcut the expression by making it false… you do this by putting ! in front of it. 

So it’ll look like this:

$weather = ‘bad’;

if( ! ($weather == ‘good’) ){  

echo ‘take your umbrella!’;

}  //To make this clear, weather equals ‘bad’ we know this – so this reads ‘if the weather is not the same as good, go get my umbrella.  It’s a double negative// 

Okay so you’ve written the script, and now you’ve written details on whether it works or not.  So what if you script comes to an end too suddenly? You can put in an else statement. So if something doesn’t happen at the if stage, something else will happen.

For example

$name = ‘admin’;

if( $name = = ‘Julie’){

echo ‘Hi Julie, how are you?’;
}
else {
echo ‘Welcome, user’;
}

Are you following me so far? 

Logical AND && and Logical OR ||

A logical AND is represented as a double ampersand.  If you have two or more expressions within you IF statement  it’ll mean that both of those expressions will have to evaluate to true for it to run.

For example:

$x = 3; 
$y = 5; 

if ( $x < $y && $y < 10 ){ 

echo $y

}

Both of those parts equate to true so on a blank screen you’ll see the number 5. If one of them didn’t equate to true you wouldn’t see anything.

A logical OR is represented by a double pipe || These symbols are alot easier to please as you only need one of the expressions to equate to true.  

Okay that’s enough brain melting for one day!