Here is the basic tutorial (I may call it: basic guide) about functions! In this article I’m going to explain what are the functions and how to create them too!

PHP is based on functions. Without functions you couldn’t combine and express your variables. So it can be defined as the hinge of PHP.

As you may correctly think, functions “do something”. Starting from the easiest “print” to the more complex socket opening or image generation.

First of all: all the functions require arguments, from zero up to a lot of them. Functions are called in this way:

  1. function(arg1, arg2, arg3);
Just like most of the programming languages (C, Java and so on).

Arguments are variables which can be defined “on the fly” or previously set and then put as them. For the first example, I’m going to print two strings, equals to each other, in these two different ways:

  1. <?php
  2. $welcome = "Hello World!";
  3. print("Hello World!"); // direct
  4. print($welcome);
  5. ?>

By the way, // means a comment on a single line while /* comment here */ means a comment in multi lines.
In this case the print function requires one argument, which can be an integer, float, array, string… it doesn’t matter, PHP is cool ;) . As you can see the potential of PHP is to get a data, edit it, combine it with other data, recollect other informations and output the final code. In this way you can just imagine the dynamic potentials of PHP versus a static HTML web page.

Some functions require some arguments as the others do, but sometime they may be able to accept extra arguments. They’re set by default and thus can be omitted in the function call, but can be changed if necessary. An example of these functions is fsockopen, which is able to open a connection to another server (i.e. for pinging a game-server or to establish a plain text / binary transmission between two services… don’t bother about it (for now)!):

  1. resource fsockopen ( string $hostname [, int $port= -1 [, int &$errno [, string &$errstr [, float $timeout= ini_get("default_socket_timeout") ]]]] )

Well, all the arguments in squares [] are optional arguments. For example, I want to ping my web server (port: 80, default value of the argument), then my FTP server (port 21). Here is the code:

  1. <?php
  2. $web = fsockopen("php.elegosproject.org");
  3. $ftp = fsockopen("php.elegosproject.org",21);
  4. ?>

If I want to know if something goes wrong, I may specify $errno and $errstr, two variables that are set in case of errors, but this function is very complex, it was just an example on how optional arguments work ^^.

So, functions may edit variables, but most commonly they return something. For example the function rand, saw for the first time in the ABC of PHP article, returns an integer (which is a number).

Generally in other program languages, like C, when you create a function you have to declare what type of variable will return. In PHP there is no need to do it, as all the variables are treated in the same way!

To create a function you have simply to… write it! just with this structure:

  1. <?php
  2. function myFunction($myArg1, $myArg2, $myArg3) {
  3. // my operations here
  4. return $myVariable; //can also not return at all, in that case omit this line
  5. }
  6. ?>
  1. Functions require arguments, but if your function requires no arguments, simply leave the parenthesis void -> function myFunction()
  2. All the code of the function is closed in the graphs {}: it’s a separate environment, and thus you won’t be able to use local variables declared in the main body of your script, but only global ones (like $_SERVER, $_POST, $_GET and so on) and local ones declared in the function (or arguments passed to the function, but pay attention: they are only COPIES of the original variables!).
  3. A function may return something. In this case, “return $whatToReturn” will be written, generally at the end of the function: in fact nothing of the function will be processed after the “return line”, so pay attention!

Here is an example of a very stupid function:

  1. <?php
  2. function getNameSurname($name,$surname, $sex="m") {
  3. if(strtolower($sex) == "m") $title = "Mister";
  4. else $title = "Missus";
  5. return $title." ".$name." ".$surname;
  6. }
  7. $name = getNameSurname("Giacomo","Furlan");
  8. print($name); // will print "Mister Giacomo Furlan"
  9. ?>

In this case the function will require $name and $surname. If $sex is not specified, male will be the default one. Strtolower is a function which makes a string all in lower case (easier to handle in these cases where case sensitivity is not required). Then the function concatenates the sex title, the name and the surname. Thus this function will return a string. The script will eventually call this function and echo the result. Easy, wasn’t it? (don’t bother about if else cycle, I’ll speak about it in another tutorial).