If you want to make a MOD and you just don’t know where to start, I think the best thing to do is to look at existing MODs to see how other MOD authors go about doing certain tasks. If looking at an existing .php file makes absolutely no sense, type a keyword of what you don’t understand into the search box on http://www.php.net – in many cases it will be a PHP function that will be well explained and documented.
For this blog post, I will address the four basics of PHP. Understanding these concepts will give you a kick start into the world of PHP and MODding.
“if” statement
The if statement is perhaps the most commonly used aspect of PHP. It checks whether a statement is true or false, and depending on the answer will or will not perform a certain task.
Suppose you want to output “Hello Ben!” but only if a persons name is Ben. In everyday language, it would look something like this:
- Code: Select all
IF Name is 'Ben'
Output 'Hello Ben!'
For strings (ie. words, or letters, characters and numbers) it is marginally quicker to use single quotes than double quotes, hence it is ‘Ben’ and not “Ben”. The reason for this is that anything within double quotes, such as a PHP variable, can be evaluated, which means PHP is conciously looking through the string for anything it needs to process. This is unlike anything between single quotes which is taken on face value.
The “Else” and “Else If” statements are sisters of the if statement, and they can be used to output different results in various cirumstances.
Using the earlier example, say you had two people, Ben and Cephas, and you wanted to output the name of whichever person was chosen. In everyday language:
- Code: Select all
IF Name is 'Ben'
Output 'Hello Ben!'
ELSE IF Name is 'Cephas'
Output 'Hello Cephas!'
Finally, if you had a dozen different people, but only wanted to say hello by name to only Ben or Cephas if they were chosen, you would use all three statements in this way:
- Code: Select all
IF Name is 'Ben'
Output 'Hello Ben!'
ELSE IF Name is 'Cephas'
Output 'Hello Cephas!'
ELSE
Output 'Hello other person!'
The if and else if statements are only executed if the condition is true, while the else statement is only executed if the conditions above it were all false.
In PHP, the three code snippets above would look like this:
- Code: Select all
$name = 'Ben';
if ($name == 'Ben')
{
echo 'Hello Ben!';
}
- Code: Select all
$name = 'Ben';
if ($name == 'Ben')
{
echo 'Hello Ben!';
}
else if ($name = 'Cephas')
{
echo 'Hello Cephas';
}
- Code: Select all
$name = 'Ben';
if ($name == 'Ben')
{
echo 'Hello Ben!';
}
else if ($name = 'Cephas')
{
echo 'Hello Cephas';
}
else
{
echo 'Hello other person!';
}
Note that any line that doesn’t have a statement or a curly bracket on it ends with a semi-colon. This is a convention of PHP to represent the end of a line, and missing a semi-colon will result in an error message. Using the word “echo” is telling PHP to output whatever follows, while “$name = ‘Ben’;” is the initialisation of a variable. Whenever you see a variable (a dollar sign before a word or set of characters, like $name, $var_1, $var_2, $var_test, etc) followed by an equals sign this shows that a variable is being set.
“while” loop
A while loop performs a task until a condition is false. For example, if you wanted to multiply the number 10 a total of 5 times (10×1, 10×2, … 10×5), it may look like this:
- Code: Select all
number = 10
i = 1WHILE i <= 5
Output i * number
i = i + 1
Or in PHP:
- Code: Select all
$number = 10;
$i = 1;
while ($i <= 5)
{
echo $number * $i . ‘,’;
$i++;
}
The asterix is the symbol used to multiply, while $i++ is another way of writing $i = $i + 1. By including . ‘,’ at the end of the echo line, it splits the results with commas, so the final output will be: 10,20,30,40,50,
In phpBB, a while loop will often be used in order to obtain multiple rows of data from a database.
“for” loop
A for loop is quite similar to the while loop, with the key difference being you can choose how many times the loop takes place and all variables can be defined and altered within the condition line.
The example above of displaying the multiples of ten can also be done using a for loop as follows:
- Code: Select all
$number = 10;
for ($i = 1; $i <= 5; $i++)
{
echo $number * $i . ‘,’;
}
As can be seen, the $i variable can be initalised and the $i variable incremented within the one line.
Arrays
Once you know how to use if’s, while’s and for’s, the other major feature of PHP worthwhile knowing is the array.
An array stores many values within one main variable, so is useful for any kind of list, such as a list of songs.
You could create a list of variables, like this:
- Code: Select all
$song_1 = ‘Vertigo’;
$song_2 = ‘City Of Blinding Lights’;
$song_3 = ‘Pride In The Name Of Love’;
$song_4 = ‘Miracle Drug’;
$song_5 = ‘Beautiful Day’;
…
But it is rather messy. It is much better to use arrays.
The most common ways of forming an array are as follows:
- Code: Select all
$song = array(
‘Vertigo’,
‘City Of Blinding Lights’,
‘Pride In The Name Of Love’,
‘Miracle Drug’,
‘Beautiful Day’,
);
Or each array indice individually…
- Code: Select all
$song = array();
$song[0] = ‘Vertigo’;
$song[1] = ‘City Of Blinding Lights’;
$song[2] = ‘Pride In The Name Of Love’;
$song[3] = ‘Miracle Drug’;
$song[4] = ‘Beautiful Day’;
To incorporate this into a for loop as above as a way of listing the five songs, you would do as follows:
- Code: Select all
for ($i = 0, $total = sizeof($song); $i < $total; $i++)
{
echo $song[$i] . ‘,’;
}
sizeof() is a PHP function which counts the number of values within an array – in this case 5. By setting $i to 0 and looping $i for as long as $i is less than 5 (ie. running the loop when $i is 0, 1, 2, 3 or 4) each song can be listed by echoing $song[$i].
An array works by mapping a key to a value. By default (in the shortened form directly above) the key is a numerical value beginning at 0.
So for each array, the key 3 would map to the value Miracle Drug.
The key does not have to be numerical, it can be a string such as in these examples:
- Code: Select all
$song = array(
’song_one’ => ‘Vertigo’,
’song_two’ => ‘City Of Blinding Lights’,
’song_three’ => ‘Pride In The Name Of Love’,
’song_four’ => ‘Miracle Drug’,
’song_five’ => ‘Beautiful Day’,
);
Or
- Code: Select all
$song = array();
$song['song_one'] = ‘Vertigo’;
$song['song_two'] = ‘City Of Blinding Lights’;
$song['song_three'] = ‘Pride In The Name Of Love’;
$song['song_four'] = ‘Miracle Drug’;
$song['song_five'] = ‘Beautiful Day’;
Final word
If you want to test any of these examples yourself, paste a given PHP example into a text editor such as Notepad, and at the very beginning of the file add on a new line [i][/i] on a new line. The file can be named anything reasonable providing it ends with [i].php[/i] (so test.php, test1.php, my_test_file.php are all reasonable) and can then be uploaded to and run on any server with PHP installed.
For more in-depth analysis of each of the above aspects of PHP, please consult the php.net website:
IF: http://www.php.net/manual/en/control-structures.if.php
ELSE: http://www.php.net/manual/en/control-structures.else.php
ELSEIF: http://www.php.net/manual/en/control-structures.elseif.php
WHILE: http://www.php.net/while
FOR: http://www.php.net/for
ARRAYS: http://www.php.net/manual/en/language.types.array.php
“If you want to make a MOD and you just don’t know where to start, I think the best thing to do is to look at existing MODs to see how other MOD authors go about doing certain tasks.”
I disagree. The first thing these people ought to do is actually READ the phpBB Coding guidelines and keep them in mind. That’s the reason why we have so many awful MODs for phpBB 2. It is because these people copied the MODs of the people who DIDN’T read, so the whole practice of bad coding just propagates that way.