February 6th 2007

(1) PHP Lesson 2

Comments RSS Feed Educational, Programming, Tutorials

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.

February 5th 2007

(3) PHP Lesson 1

Comments RSS Feed Educational, Programming, Tutorials

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!