Monday, March 3, 2008

PHP Basics - Beginner Tips

Alright, just thought that instead of leaving you guys in the dust with no new tutorials, I could write a small, simple (but good) tutorial on some of the basics of PHP. If you look through all the tutorials on this site as a beginner to PHP you may not understand a lot of it, and need a back to basics explanation of everything, so with that in mind this tutorial will cover -

1. Conditions (if statements)
2. Loops
3. Basic Functions

Beginner to intermediates can benefit from this tutorial, you may just pick up a tip or 2!

Alright, before we start learning anything from the actual list above, I think it would be beneficial to learn how PHP actually works. You can't learn the basics of PHP without knowing the base of PHP first.

PHP is a server side scripting language, this means that it is executed on the server, and the results are sent back to the browser as HTML.

When testing out your PHP applications, you can't just create the file, save it and open it locally on your own computer, you either need to install Apache, PHP and MySQL (if your using a database, which you should). All that is pretty complicated, so an easier solution is to get webhosting. You can find webhosting for as low as $1 (which isn't exactly the best, but ok for testing). Once you get webhosting, you can just use an FTP program (easy to use) and upload your files onto your website, most webhosts, actually I would bet 97% of webhosts have PHP installed, so it should be easy to find one. Also, just a side note when you create PHP applications if your file has PHP in it, you need to save it with the .php extension so the browser knows to execute it as PHP.

Now, probably the most basic (and most over-done) PHP example is

< ? php

echo 'Hello World!';

?>

Alright, if your completely new to PHP this may need some explaining. The ).

Next we use an echo, this is how we output text to the browser, echo 'Text Here'; is the proper format, notice the ending ; on the line, most lines of PHP require you to input that, there are exceptions that we will cover later though.

PHP Conditionals (if statements)

Probably in every PHP script, you will use conditionals, from if statements to switch statements they are everywhere, let's get right into it and start you off with a simple if statement, probably the most used conditional.

< ? php

if (1 == 1) {
echo '1 really does equal 1!';
}

?>

Alright, this is probably the easiest if statement you will ever see, it doesn't even have an else part to it. If you can't understand this (which I hope you can) then I will explain it, all it's doing is saying if 1 is equal (using 2 equal signs is how you define equal to) to 1 then continue, and all we do is echo out a simple statement. You can do anything though, you don't have to echo. Then we end the statement. Now let's use the same thing but add on an else, so if it doesn't equal one we have somewhere to go.

< ? php

if (1 == 1) {
echo '1 really does equal 1!';
} else {
echo 'Aww, 1 doesn\'t equal 1.';
}

?>

As you can see, all we did was add on } else { then add another echo statement. Pretty straight forward, but let's say we are using variables, we can make another tiny bit more complicated example.

< ? php

$number = 1;
$answer = 2;
if ($number == $answer) {
echo '1 really does equal 1!';
} else {
echo 'Aww, 1 doesn\'t equal 1.';
}

?>

We use variables in this example, we have a number variable ($number) and an answer variable ($answer), we define static data to these variables, meaning they don't change, but that will be different when you start getting into MySQL and such.

Then instead of comparing 2 numbers in our if statement, we compare our 2 variables. Pretty much the exact same thing, except now our variables both contain different numbers so our output will then be Aww, 1 doesn't equal 1. because our 2 variables did not equal each other, so we go to our else statement.

This is just the basics of if statements, but this is also just a basic PHP tutorial. If you want to learn more about if statements check out any of the tutorials on this site, chances are they use if statements in them.

Now let's check out switch conditionals, these are in theory the same as if statements except they allow you to have a lot of different answers to them. Here, I will show you a simple example first -

< ? php

$var = 'Cows';
switch ($var) {
case 'Dogs';
echo 'You chose dogs!';
break;
case 'Cats';
echo 'You chose cats!';
break;
case 'Cows';
echo 'You chose cows!';
break;
case 'Birds';
echo 'You chose birds';
break;
default:
echo 'You didn\'t choose any animals!';
break;
}

?>

At first glance, this may look a bit overwhelming, but break it into parts in your mind. Let's assume that we have a form where a person chooses a favorite animal, and then there chose is saved into a variable called $var. From there we output an answer depending on what they choose. First we define that we are starting a switch, we want to then put in what we want to check, then we have each case, a case is what we are trying to match with our switch, if our switch and one of our cases match we continue on with that case, if not we keep checking through them. If our switch matches none of our cases, then we have a default, from there we just put out any old answer that you want your user to see.

PHP Loops

Loops are great for executing sections of code over and over again to do the same thing, I know when I first started in PHP I though, "what's the point of that?" well when you get more and more advanced it will become a lot more useful. We will discuss, for loops and while loops. First, we will start with for loops, let's make a for loop that outputs the numbers 1 to 10, each number on a new line.

< ? php

for ($n = 1; $n <= 10; $n++) {
echo $n.'
'
;
}

?>

For being such a small piece of code, this can be actually quite confusing, but if you understand it, you will think "Doh, that was easy". Ok, first you use the for loop function, now this is where you do all your variable defining, the first part is where you define the value of your first variable, then from there you make your condition saying, if that number is smaller then or equal to 10, continue, and then if it is smaller then or equal to 10 we add one onto our variable ($n++ means add one to that variable). Then we execute a piece of code, now the next time we go back, but our $n variable equals 2, and it does this until $n is equal to 10.

Now for the while loop, this one is a bit easier to understand, and is used more often (well I use it more often) it is used VERY often when dealing with MySQL, people use it to extract data from there MySQL results, we won't be covering that in this tutorial though, for my example I will just be showing you an example of how to do the same thing we did with our for loop

< ? php

$n = 1;

while ($n <= 10) {
echo $n.'
'
;
$n++;
}
?>

This will do the same thing as our for loop, except with more code and not as compact, usually a while loop will be used more when working with databases. As you can see, we define our $n variable outside of our loop, then we just do a easy to understand loop, and at the end of our code we add 1 onto our $n variable.

Basic PHP Functions

In the last section of our tutorial we will cover some basic functions in PHP that every user should know, and most likely will use.

These functions will deal with strings, first we will start with a function that will count all the characters in a string (this includes spaces).

< ? php

$string = 'The Big City';
$len = strlen($string);
echo $len;

?>

In this example, we define a variable, in this variable we define the string we want to use, next we use our function, we assign the output of this function for better coding practices, we use the strlen() function, all it does is output a number based on how many characters are in the string, then we echo out our variable, that will give us our answer, in this example our answer is 12.

Next, we will use a function that turns our string into an array, this has many usages, in this example we will use the same string that we just used before.

< ? php

$string = 'The Big City';
$array = explode(" ", $string);
echo $array[2];

?>

Ok, again we make a variable, and define our string in our variable, then we use the explode() function and assign it to a different variable, now the explode function takes 2 arguments, a separator and a string. We say that our separator is a space, (thats the blank space) and that our string, is the variable that we defined before. Now after doing that, we need to echo out what part of the string that we want. When you use the explode function, it turns our string into an array. When you make an array, the first item in the array is defined as [0], the second item is [1] and so on, so if we wanted to access the word City, which is the 3rd item we would want to echo out $array[2].

That is all for now, if you have something you want to learn more about in PHP, just leave a comment and I will add it onto this tutorial!

Thanks for visiting TutorialCode.com

No comments: