![]() |
|
PHP Tutorial #4 So far we have been dealing with functions that are already built into PHP such as date() but you can write your own. Take a look at this:- <?php
function boldit($arg)
{
echo("<b>$arg</b>");
}
echo("This is not in bold<BR>");
boldit("This is now in bold");
?>
This is a very minor illustration but functions are very powerful tools for the programmer and saves writing the same code over and over again. A function is called and is given an arguement which in this case is a string of characters. The function simply takes the arguement and includes it in a bit of HTML. This means that we can send any string to this function and it will make it into bold using HTML. From the top, the function is declared and has a block of code just like an if statement. It has an arguement which is just a variable which in this case is called $arg and can contain anything. To show this function is action, an ordinary string is displayed. This is followed by a version in bold type that has been converted by calling the boldit function. Simple but very useful. Sometimes it would be useful to use the result from a function elsewhere in the code. Here is how it's done using the keyword return:- <?php
function addthem($a=1, $b=1)
{
$total=$a+$b;
return $total;
}
$a=3;
$b=6;
$str=addthem($a,$b);
echo("The sum of $a and $b is: $str<BR>");
?>
This time we are using numbers instead of strings (for the first time in these tutorials so far). This time our function has more than one arguement which are both initialized with 1 just to be sure they have been initialized. These two arguements are then added together and returned to the main code. We have a further 2 variables which are given values of 3 and 6. The variable $str will contain the returned value from the function that is called with arguements containing 3 and 6. Then we echo a message shwoing the 2 values and the result. Try playing around with this perhaps substituting the addition to multiply maybe creating your own function called multiplythem. Have fun (no really, this IS fun...honest...). Now we come on to the subject of files. PHP allows the programmer to create, read and delete files but only on the server, not on the users PC. Here we get a list of files in a particular folder:- <?php
$dirname = "/var/www/html";
$dir = opendir($dirname);
while(($file = readdir($dir)) != false)
{
if(($file != ".") and ($file != ".."))
{
$file_list .= "$file<BR>";
}
}
closedir($dir);
?>
<H1>Files in <?php echo($dirname); ?></H1> <ul> <?php echo($file_list); ?> </ul> </body> The meat of this sandwich is the command opendir, readdir, closedir combination which as expected opens a directory as if it were a file, read it extracting information from the folder about what files are available and then closing again (very important to close). The . and .. folders are ignored and the .= operator will append a string to the variable. Finally, there is an extra bit of HTML to go before the </BODY> tag to display the result. This is HTML so the <BR> does a newline as before. Windows users will have to change the $dirname to something like c:\apache or similar to get a result. Next time we'll start saving and loading files. Can't wait... Please eMail me at: nickjc@nickjc.co.uk |
|