PHP For Web Designers: Part 2
Last time on PHP for Web Designers:
We learned the basics of PHP syntax and learned how to assign variables and work simple if – else statements. In this edition I’ll be talking about data types and functions! Let’s get started.
Data Types
There are many different types of data in PHP. These includes strings (words), numbers, and even boolean true or false. The data type that you choose for your information is vitally important for your PHP applications.
Fortunately, assigning data types couldn’t be easier. For instance, a string is always surrounded by either single quotes (‘) or double quotes (“). For a number, however, you need no such declaration.
For our purposes, I’ll just be talking about 3 data types, numbers, booleans, and arrays.
Numbers (integers to be correct) behave exactly as they sound. For instance, you can add two numbers together, find out if one is bigger than another, you name it!
Booleans are a little bit more complicated, but still fairly easy to understand. A boolean is always either true or false. In PHP, true and false are conveyed as 1 and 0 respectively. Booleans are useful if your trying to determine if something is true, then do something else. You can assign a boolean to a variable by just typing $variable = true; or $variable = false. Booleans come in very handy for functions, which I’ll talk about in a minute.
Arrays are basically groups of information. Think of it like a bag filled with information. You assign an array by typing something like $array = array(‘hello’, 5, false); which then stores those three values into the one variable $array. We can access this information by typing something like echo $array[1]. Here you’d think we’d get back ‘hello,’ but if you try it out you see we get 5 printed onto the screen.
Why is that? Arrays use the zero-system, meaning that 0 actually corresponds with the first value. This is the type of information that you learn once and then remember, like riding a bicycle.
Functions
Nearly everything you’ve seen up until this point has actually been a built in function of php. Functions take the form of function($arguments). Arguments are pieces of information you can pass to the function to perform something different depending on what you passed in.
If we just stayed with PHP’s built in functions, the language would get old real fast. The real power of functions comes when we make our own.
function hello($target) {
echo 'Hello ' . $target;
}
Here what we’ve done is declare a function called hello which accepts an argument called $target. If we run this function now by typing
hello(‘world);
and then load the page up in the browser we should see ‘Hello world,’ printed back to us. However, this is really not the right way of doing this. More correct would be to declare our function using
function hello($target) {
return 'Hello ' . $target;
}
Now we’re returning some information from the function. If we run our function now using the same form as above, it’s not going to do anything visibly. If we adjust our hello(‘world’); statement to become
echo hello('world');
Then we see the correct result returned back. Why is that? Well, now what we’re doing is taking the value of ‘Hello world’ and echoing it back to the browser using our function. When we do it this way, we’re not restricted to just echoing, we can do all sorts of other things!
Conclusion
Alright, this was a pretty short lesson today, but I think you learned two valuable skills. Please tell me in the comments what you would like to see in the next lesson, or whether you even want there to be a next lesson. Thanks!



I would like to learn how we can use this functions and conditionals for our benefit, since what we’ve learnt already can’t be used seriously :S What I mean is: i gain nothing with knowing this if I don’t know what kind of stuff I can do with it.
Thank you..
P.S: I would like to learn PHP passwords too. And another thing: MySql forms instead of e-mail forms.
nice post