Archive for the ‘Tutorials’ category

PHP Lesson 3

February 7th, 2007

Sunny Man’s TutorialsIn lesson 2 we touched on variables in PHP and how to create one and then change it via a number of processes. Today we’re going to use some of the functions that PHP provide which allow us to manipulate and query strings and that will most likely become very handy in the future.

If you wish to take a look at the functions it offers then head on over to the PHP Manual, specifically the string functions.

Lesson 3: String Functions

Last week we touched on how to find the length of a string using the strlen() function. Today we’re going to look at a few more functions and put them into a practical example. To find the length of a string all we have to do is employ this function as is shown here:

<?php
$five = strlen("mouse"); //$five's value is 5
$word = "mousemat";
$eight = strlen($word); //$eight's value is 8
?>

Now we’re going to look at replacing characters or phrases within a string with something else. To do this in the simplest way we are going to use the str_replace() function. There’s several parameters to this function. This means that we have to provide more information within the functions. In this case the format is str_replace(string to search for, string to replace it with, string to search within).

$string = "Sunny Man’s Blog";
$string = str_replace("Man","Boy",$string); //replace 'Man' with 'Boy'
echo $string; //displays Sunny Boy's Blog

Later on when we get to talking about SQL and databases you’ll need to be able to encrypt strings. There are several different types of encryption available in PHP. All of them are simple enough to use but, as we will will discuss later on, knowing when to use the correct one is vital. First let’s take a look at the different types.

$password = md5("password");
echo $password; //5F4DCC3B5AA765D61D8327DEB882CF99

$password = sha1("password");
echo $password; //5BAA61E4C9B93F3F0682250B6CF8331B7EE68FD8

Both md5 and sha1 have their advantages and disadvantages but they’re both quick to generate and once stored in a variable, easy to remember. Say we wanted to take a raw password that was given to us by a form (we’ll cover this soon), replace the letter ‘i’ with ‘z’ and encrypt it for storage in a database, this is how we would do it:

<?php
$password = "birmingham";
$password = str_replace("i","z",$password);
$password = md5($password);
?>

This is the end of lesson 3. In the next lesson we’ll talk about taking variables from a form, collecting them and manipulating them in a real life situation. After than we’re going to put all we’ve learnt into a logging in script in lesson 5 which will introduce you to salts, database queries and some other stuff we will cover in lessons 6 to 10.

PHP Lesson 2

February 6th, 2007

Sunny Man’s TutorialsIn a previous post I started you off on the basics of PHP and you managed to get Hello World! displayed on the screen. This lesson we are going to learn about how to create, use and manage variables. Hope you enjoy it…

Lesson 2: Variables

Unlike programming languages such as C++, in PHP you don’t have to state what type of variable you’re creating when you create one. A variable such as $myvar could just as well be a number as it could be a word. It’s as easy as pie to create different variables:

<?php
$age = 15;
$name = "John";
?>

Here we created 2 variables: $age and $name which values’ are 15 and ‘John’ respectively. It’s that easy to create variables. Now that we have created variables we can manipulate them. Say for example we wanted to increase the $age variable and assign the new value to $age again, this is what we would add before the end of the script:

$age = $age+1;

If we now output the $age variable we would find that it displays 16 and not 15.

echo $age; //this would display '15'

We can also attach strings together. Take the $name variable we created earlier. We can take that and also create a last name in a different variable and then string them together to make the $name variable include the first and last name. Here we go:

$lastname = "Smith";
$fullname = $name.$lastname; //this would create "JohnSmith"
$fullname = $name." ".$lastname; //this would create "John Smith"

So, let’s try and put this all together and make a useful program out of it. Here we manipulate the age by turning it into dog years. We also here string the first and last names together and then as a step towards the next lesson we’ll count the number of characters in the names.

<?php
$age = 15;
$firstname = "John";
$lastname = "Smith";
$i = 0; //used later on as a counter

$dog = $age*7;
echo "You're ".$age." human years old and ".$dog." dog years!";
echo "<br />"; //create a new line; just regular html

$name = $firstname." ".$lastname;
$i = $i + strlen($firstname); //0 + 4
$i = $i + strlen($lastname); //4 + 5
echo $name.", you have ".$i." letters in your name!";
?>

Try it out and play around with it! Next lesson we’re going to look at some of the functions we can apply to strings such as getting the length of the string and searching for a specific item within a variable.

PHP Lesson 1

February 5th, 2007

Sunny Man’s TutorialsI’m going to start producing a few classes for those people who wish to start learning the wonderful programming language of PHP. These will be pretty simple classes and will reach until the end of my knowledge of the language. First, a bit of my experience – albeit a relatively short one.

My History

I first started to learn PHP 3 years ago and have since then done some things I’m quite proud of in the language. I’ve built a system for a hospital in Reading, my own blogging software (although quite simple, it does the job) and also a file directory viewer with AJAX technology. But, my PHP knowledge is quite limited and I have not used the language now for some times as I have been concentrating on the design aspect of the web.

Lesson 1: Hello World!

Every programmer starts off with the ‘Hello World!’ program. Here we are going to get PHP to output the text ‘Hello World!’ to the screen. From this point out I’ll assume you’ve installed PHP and have it running on Apache or the like.

So, as with any PHP script we have to tell the server it’s going to be dealing with PHP. So we start it off with the opening and closing tags. All code goes between these tags:

<?php
?>

Now, as I said all PHP code goes in between these tags. So, now we have to make it do what we want it to do. So, in between those tags we know insert this line of code which tells it to output the text to the screen:

echo "Hello World!";

Once we’ve put this all together we should be able to achieve the result we wanted. Try it yourself on your own server! Just save the following code as a .php file and run it!

<?php
echo "Hello World!";
?>

It’s going to get a bit more complicated than this, I can assure you. I’ll post a lesson every weekday – every Friday we’ll do a roundup of everything we’ve learnt!

Back From Tropical Devon

April 8th, 2006

Well, I’m back and ready to entertain you with my words of wizdom and my thoughts. I got home today, sorted everything out and then opened my e-mail. It took me 25 minutes to sort through my e-mail (mostly spam but I’ve won a Cadbury’s Creme Egg Easter Egg :D ) and then another 35 minutes to catch up on my RSS feeds. So, I’m gunna start this post.

Just before I left for the south-west county of Devon I recorded Ready Steady Cook with my Archos AV700 Portable DVR because two teachers from my school, Miss Penny and Ms Mora of the English and Modern Foreign Languages (MFL) departments respectively, were appearing on the show. I recorded it but couldn’t upload to Google Video because it was Friday night so me and a friend only just managed to torrent the thing. A more detailed post and the video can be found on Opher’s [New] Blog in this post. Enjoy.

Also, on the subject of Opher’s Blog, I’d suggest adding it to your RSS feeds list if you haven’t already. Although most of it is copied from the BBC News website ;) , it’s still worth a read. Head on over there now. NOW!

I’m back now and I’ve been looking over my blog logs and found that my Google Earth and SketchUp tutorial has been posted around a bit. Websites such as GEarth Blog, JuicyGeography and Akkam’s Razor have posted links to my tutorial; quite flattering really. Check them out – for me :D .

Well, I dunno what else to post. Oh, I’ll post my homework list so you can all remind me of my impending doom:

  • English – Re-read anthology short stories and go over poem notes.
  • Maths – Do exercise 3.3C, questions 1-4.
  • Spanish – Decide on presentation topic and do revision reviewing of topics.
  • Art – Do development work and collect more evidence.

Well, I also reccomend you check out Cow’s Blog, The Bakery, BBC News, Digg, Ctrl+Alt+Del and Engadget while I regain my blogging powers.

Sunny