Search This Blog

Friday, September 19, 2008

Writing user-defined functions in PHP

Writing user-defined functions in PHP

Anatomy of a function
Writing your own functions is as simple as using the function command like this:

function somefunction() { [php code] }


The command function is followed by the unique name of your function (in this case somefunction). Next comes the parentheses, which may contain variable names, and the actual function code itself, enclosed in curly braces.

Here's a basic example:

function helloworld() { print("Hello world!
\n"); }



This function simply prints the text "Hello world!" to the Web page. It can be called just like a regular PHP function:



include("functions.inc.php");
helloworld();
?>

No comments:

Post a Comment

content-wide advt