PHP: Main - It's like triple chocolate icecream
------------------------
This is an extension to Momo's tutorial which is also listed in PHP Main. We're going to be focusing on user defined functions, arguments etc. You should have a basic understanding of PHP at least - this tutorial is not for newbies.
So we're going to start off with a simple array of fruits:
<?php
$fruit = array("Banana", "Kiwi", "Melon", "Orange", "Pear", "Strawberry");
print_r($fruit);
?>
So I just chose some random fruits, obviously then printing the output. Now, to DO anything with this kind of output I can use a foreach() loop OR a for() loop. Let's do both.
<?php
// add in the code from the previous block, drop the print_r if you like
foreach ($fruit as $f)
{
echo "I like $f very much. <br />";
}
//foreach loop will go through all the array elements, so you should have an output for all your array keys
?>
So there you can see that PHP will run through every single array key performing what's in the curly brackets. for() loops are more complicated but more flexible. Let's say we just wanted to do the first 3 keys of the array:
<?php
for ($i=0;$i<3;$i++)
{
echo "Only the first three keys. This one just happens to be $fruit[$i]<br />";
}
?>
By substituting the number inside the square brackets, we use the increment $i on the array. Of course we can emulate the foreach loop by changing the first line to:
for ($i=0;$i<count($fruit);$i++)
Functions
Functions are pretty straightforward. You define a function like this:
function myFunction()
{
//sample code goes here
}
Optional arguments can be defined within the brackets. For example, say I wanted to add in a string to another string:
<?php
function ins_String($str1, $str2)
{
//use two arguments - one for the first string, other for the second
echo $str1 . " " . $str2;
}
ins_String("Hello", "World");
?>
Simple.
Now, how about functions and arrays? Well, let me show an example I did a while ago for generating lists. My final goal was to create a function that would let me do a HTML list with the type of list as an optional argument.
Our result wants to be like this:
<ul>
<li>This</li>
<li>Is</li>
<li>Example!</li>
</ul>
I didn't want to do sublists at the time, but it could probably be done just as easily. Now, our function needs to be expanded/shrunk on the fly, so we're going to use an array as an input.
$myList = array("Example", "List", "Test", "Works", "Great!");
So now the function:
function htmList($array, $type="u") {
//Here I am setting a default value for type - so if it is not specified then the list will be an unordered list
$items = count($array);
//Count the number of items in the list
if ((!$type == "u") OR (!$type=="o")) { $type = "u"; }
// Checks for what value type is. If it's not unordered or ordered then default to unordered.
echo "<{$type}l>";
// Prints the html for the list type
for($i=0;$i<$items;$i++)
{
echo "<li>" . $array[$i] . "</li>";
// Loop through all the elements and generate a list item for each
}
echo "</{$type}l>";
//Finally validate our list! Note the curly brackets so as to show PHP that $typel is not a variable
}
And then generate the list with:
htmList($myList, "o");
Or:
htmList($myList);
Hope you learned something. :-)