The PHP Language reference has details on what a function and class is:
http://www.php.net/manual/en/langref.php
It also explains most the other features of PHP. If want to learn PHP that is the best place to start.
Functions
The function is a grouping of statements (lines of code).
For example the following statements:
$name = 'mary';
$gender = 'girl';
if ($gender == 'girl') {
$line = $name . ' had a little pony.';
} else if ($gender == 'boy') {
$line = $name . ' had a little horse.';
}
echo $line;
Can be grouped together into a function so it can be reused:
getSentence('mary', 'girl');
getSentence('peter', 'boy');
function getSentence($name, $gender) {
if ($gender == 'girl') {
$line = $name . ' had a little pony.';
} else if ($gender == 'boy') {
$line = $name . ' had a little horse.';
}
echo $line;
}
Notice the two function calls:
getSentence('mary', 'girl');
getSentence('peter', 'boy');
These two statements run the whole block of code inside the getSentence function and pass it the variables $name and $gender.
With the first function $name = 'mary' and $gender = 'girl' and in the second $name = 'peter' and $gender = 'boy'.
So the main benefit of functions is that you have grouped code for reuse, allowing the passing of different values for the variables needed by the function. These variables are called the function parameters.
Another benefit of having the code grouped is easier readability. You are essentially naming the block of code, and giving them a specific purpose. Making it easy to read and remember it's use.
Another benefit is that redundancy is removed. You do not have to write the block of code more then once. You just define it once, and call it multiple times. This also makes editing of the function code affect all calls to that function - which reduces errors in having to edit multiple locations when changing just one aspect.
eg:
We can make sure the $name string has an uppercase first character.
function getSentence($name, $gender) {
$name = ucfirst($name);
if ($gender == 'girl') {
$line = $name . ' had a little pony.';
} else if ($gender == 'boy') {
$line = $name . ' had a little horse.';
}
echo $line;
}
We made just one change, and it affected every function call to getSentence(). In this case both: getSentence('mary', 'girl'); and getSentence('peter', 'boy'); Classes
Classes are a grouping of functions.
class Play {
function getSentence($name, $gender) {
$name = ucfirst($name);
if ($gender == 'girl') {
$line = $name . ' had a little pony.';
} else if ($gender == 'boy') {
$line = $name . ' had a little horse.';
}
echo $line;
}
function getSong($name) {
// code here
}
}
All we did was put
class Play { /** functions here **/ }
around a group of functions.
This offers the same benefits that functions do for statements except classes does it for functions.
Classes go further to build a programming methodology called Object Oriented programming (OOP), which you can read more about in link to PHP Language reference.
This defines classes as the template or definition of Objects. Objects being similar to real world objects, with the functions being called "methods" that can be called for the object.
So the class Play can be thought of as the object called "Play" with the methods "getSentence" and "getSong". These methods can then manipulate the properties of the object "Play" or return useful information about "Play". In this way, all the code inside Play becomes independent of code elsewhere in the program.
When the code inside Play requires some code elsewhere to function, it can be brought in using "inheritance", which is a major part of OOP. I will not go into detail about this as it is a very broad topic.
I would recommend getting a book on OOP and reading it to really understand why you should use classes and methods and when to use them.