View Single Post
Old 27-02-2006, 09:47 PM   #5 (permalink) Top
Waffle
Alpha Geek
 
Waffle's Avatar
 
Join Date: Apr 2004
Age: 20 Male
Posts: 2,270
Times Helpful: 51
My Mood: Angelic
Status: Offline

My Computer

Time for an update..not so much beginners, but just random PHP Stuff.

Functions
Functions, are either:

Prebuilt into the code of a given language, and (usually), return values when parameters are passed into them.

They can also be defined by a programmer, to create useful shorthand pieces of code, that can be reused quickly and easily.

Some existing PHP Functions
At last count, some 3000+ functions are built into PHP (the latest version anyway). They are constantly being expanded, as new versions/releases are brought out.

A function is structured like this:
PHP Code:
<?

functionname
(parameters) {

code to be executed with given parameters

}
?>
Here are some of the more common functions.

echo() - is used to display output to the web browser.

It is not strictly a function however, it is a language construct. It cannot return a value, but is considered a faster operation than the alternative, Print.

Print works largely the same as Echo, except Print can return a value ( True/False).

It is also said to be slower in execution, however the difference is 1000's of miniseconds, and so this is barely worth mentioning.

Both will return the data entered between the parentheses to the browser (although echo does not require the ()'s, it will operate with just quotation marks).

PHP Code:
<?

echo "Hello!"// shows "Hello!" on the screen
print ("Hello!"); //shows "Hello!" on the screen

?>
Also to note, the parentheses in this example are not vital, but is considered valid coding.

-------------------
strtolower($string); - self explanatory - String to Lower (case), presumably.

example usage:
PHP Code:
<?php
$str 
"Waffles AND SyRuP go Well TogetHer";

$str strtolower($str);

echo 
$str// Prints waffles and syrup go well togther
?>
note that the string ($str), was passed into the function strtolower($str), and the new value of $str, after being stripped of capital letters, is then echoed.

I believe this is recursive coding...using the same variable again, but in like an updated form. (vague explanation admittedly)

------------

Now, to create your own function..

First you need to tell PHP you are starting a function, then give it a name:

PHP Code:
<?

function MyFunction() { }

?>
is the syntax.

You must then decide what you want your function to do.

I have coded a handy function, that allows the coder to enter the name of a database, and a table within, and have the number of rows returned to the screen (this is useful when counting say, the number of members, or tutorials a database has.)

Rather than type out lengthy SQL queries, several times, the function can be used multiple times, with different table/database names to return the row count.

I will call the function "CountRows", and I need only 2 variables (it's basic).

that would be, the database name, and the table name.

PHP Code:
<?
function CountRows($database,$table) {}
?>
we need to do something with those variables..so:

PHP Code:
<?
function CountRows($database,$table) {

mysql_connect("localhost","user","pass"); //connect to mysql

mysql_select_db($database); //select the database entered into the first parameter..

//run a query on the chosen table..

$sql "SELECT * FROM $table";
$result mysql_query($sql);
//now count the rows..
$num mysql_num_rows($result);

return 
$num;

mysql_close() //close the connection
}  //and end function
?>
we are done.


in order to use this, we would first include the function code on the page we are working, and then:

PHP Code:
<?

include "functions.php"//includes our functions page, 
//so we can use them easily.

//then..

echo "Number of downloads:"//we want to count our downloads.

echo CountRows(data,downloads);

//data is the database name, dowloads is the table name. 
//this will return the number of rows in the table, and output it as an integer.

?>
and there you have, a basic, very useful, working function.

I would probably extend this by throwing in some error handling, like what happens if the user can't connect to MYSQL or chooses an invalid table/db name. But providing you enter the correct info, you will be able to count any row, in any table, in any database.

Hope this helps.

Send a message via MSN to Waffle   Reply With Quote
The Following 3 Users Say Thank You to Waffle For This Useful Post: Show me >>