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 6.

Okay, so it’s been a while since I’ve last posted – the truth is, a. I’ve been trying to get around all of this and secondly, I’ve been trying to figure out what I want to do with this php knowledge.  Now, I know it’ll be totally, amazingly cool to look at a php and just think “yep, I know what that’s doing.” Because to most people, they’ll look at it and think and have no idea what’s going on.

Now at this stage, just over half way through the ten week course, I can say I that I can look at some script and know what’s what.  But then again, I am looking at someone else’s logic, so to try and work it out what it’s doing would be a total and utter nightmare – this is how I see it so far. So what do I want to do next?

Well seeing as the most of my work is WordPress based, I would like to start building my own WordPress themes.  Plus I think I would have fun with the css aspect of it – the best of both worlds. Anyway, that’s that for now.  Today we’re going to tackle…

Superglobals

What are superglobals?

They sound pretty cool right… Wrong, they’re really quite complicated.  But to try and explain what they’re all about, I am going to use the following analogy… And hope that hardcore coders who may be reading this applaud it. Imagine the world is a matrix, just like the film and everything in it is either a variable, an array or some kind of data type, if this is the case, then the superglobals are the agents.

They’re not necessarily keeping things in check, but they have the power to do more than ordinary php data types, functions or arrays.  This means, just like the movie that they’re not to be trusted unless they’re validated. Superglobals come in the form of arrays. The superglobals consist of…

  • $_GET contains variables sent via a HTTP GET request – ie, as a query string in the URL.
  • $_POST – contains variables from an HTTP POST request, eg a form submitted with method=”post”
  • $_COOKIES – variables from HTTP COOKIES eg, a cookie on the users’ machine
  • $_FILES – variables sent via HTTP POST file upload
  • $_SESSION – a special per-connection id, “session cookie”, stored server-side (of course)
  • $_SERVER – variables about the server (some can be spoofed)
  • $_ENV – variables set by the system or shell in which PHP is running
  • $_REQUEST is a superset of $_GET, $_POST and $_COOKIE

This is paste and copied from our course notes as to why we need to validate our superglobals.

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

In starting to use superglobals, we’re moving away from just learning PHP syntax and starting to look at actual web problems – what PHP was made for. Many superglobals accept user-supplied data, which is always potentially dangerous: — random stuff typed on the url by curious users, malicious file uploads, SQL injection code typed in forms, etc. — so we shouldn’t ever use it without validating: we have to start thinking defensively about our code. Is the supplied data the right data type? Is it blank when it shouldn’t be?

Should there ever be numbers in someone’s name? Does a supplied email fit the accepted formats? and so on. Actually, you should do this anyway, not just with superglobals. Does the array you want to pass to foreach() contain anything? is it even an array (try passing a non-array to foreach)? etc. Here are some built in functions that you need to know, which let you interrogate the data in your script.

[/box]

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 4.

Last time, we took on loops, today we’re taking on functions!

Functions are pieces of code which you run more than once, the perform tasks which may have to rely on more than once in what ever it is you’re running.

There are many php functions which you can find here, but you can build your own.

A function looks like this.

function myfunction (){

//something you want to run goes here
echo ‘This is myfunction’;

}

1. Define your function, so you know what it is, “function, myfunction”

2. Put in notes to remind you of what it’s doing

Paramaters and functions

function greet($name) {
echo “hello, $name”;
}

The $name bit, will only work within the function, inside the curly brackets.

Within the function you can write variables into it, but they are only local variables.

You can also apply some of that if, else decision makers into a function.

For example

function greet($name, $greeting=””) {
if ($greeting != . ‘, ‘ . $name;
}
else {
echo ‘hi,’ . $name;
}

greet(‘joe’,’way to go’);

 

Flow control basics

Now, we know why functions are useful, because they are a piece of code that we can run over and over within a php file.

Echoing things to the screen is great – but we want to start doing more sophisticated things like connecting to a database or something.

At somepoint we’ll need to send data out of a function.

Back to the code that called it.

How can we get stuff out of a function?

We use the keyword, return followed by the value we want to get out of the function.

It looks like this:

function returnThis() {
return “Here’s something”;
}

returnThis();

(Now, if you were to run this code, you would get nothing.)

There are a few ways in which you can can grab a value of a function, you can assign it to a variable or you can send it to another function that can process output.

A good example to use is trim($mystring)

so…

$mystring = ” abc “; //Basic variable
trim($mystring);

Nothing will appear here but if we…

$mystring = ” abc “;
$newvariable = trim($mystring);
echo $newvariable;

Boom, the we can now see the function working.

We don’t always have to create a new variable to show a function.

We can just write something else.

for example:

function mulitply($a, $b) {
//$multiplied = $a * $b;
//echo $multiplied
echo $a *$b;
}
multiply(3,5);

You’ll lose alot of performance points if you make to many variables.

Now, there’s a whole load we can do with functions – but to be totally honest, I feel at this point it’s going to take practice and a whole lot of coding to grasp it… At this point, I want to start building things – but where do I begin, do other coders learning the basics feel this urge, but don’t know where to start?

Let’s hope so!