Functions

Whenever you need to use a certain piece of code over and over again, it is very useful to put it in a function. By putting your code into functions, you make the code easy to reuse. Whenever you locate an error in your code, you only have to fix it once: in the function. Using functions also makes your code easier to read and understand, especially when you choose sensible names for functions. Let’s start with a simple function:

function foo($arg) {
  if ($arg < 10) {
    echo "$arg is smaller than 10";
  } else {
    echo "$arg is greater than or equal to 10";
  }
}

This piece of code declares a function called foo. It takes one argument (that’s the bit between parentheses called $arg above), and it echoes different things, based on the value of that argument. You call the foo function lower down the page, in your other code like this:

foo(15); /* Echoes "15 is greater than or equal to 10" */

Isn’t that simple? You can also make functions that don’t have any arguments, just add an empty pair of parentheses after the name of the function, both when you declare the function and when you use it.

You often need your functions to return a value you can use in your programs, and here is how to do that: simply use the return statement.

function bar() {
  return (time() - mktime(0, 0, 0, 1, 21, 1982))/86400;
}

This function has no arguments and it returns the number of days since the 21st of January, 1982 — my birthday by the way. It uses a number of different built-in functions to get the time now, and then subtract the time I was born, and finally divide the time with 86400, the number of seconds in a day.

Under UNIX all times are measured as the number of seconds since the beginning of the Epoch, which was January 1st, 1970. So when you deal with times as I just did, you are just dealing with rather large integers (it’s been more than 1,000,000,000 seconds since January 1st 1970). Representing all timestamps as integers makes it very easy to find the time between two events.

Note that before we return the number, we could round it with the round() function, which will round the number to an integer: more on round() below!

In the body of the page you call into action that bar function like this:

$days_ages = bar();

If instead you wanted to round the floating-point number to a certain number of decimal places, then it’s easy to make your own function:

function smart_round($float, $dec) {
  if ($dec == 0) {
    return round($float);
  } else {
    return floor($float * pow(10, $dec) + 0.5) / pow(10, $dec);
  }
}

$a = smart_round(2.317, 2); /* Now $a is equal to 2.32 */

This smart_round() function returns $float rounded to $dec decimals. The built-in round() function has been changed in recent versions of PHP to function exactly like oursmart_round()`.

You’ve now seen most of the basic features of PHP. There’s only some links left for you.

10 Comments

  1. Jauhari:

    Thanks for nice info tutorial about function this will help me for coding on PHP

    Thanks again

  2. Bart:

    Thanks for this information. I find PHP a difficult language to code in, but your info on PHP’s basics helped me understand it a lot better.
    Thanks again.

  3. qweree:

    Very nice tutorial. It helped me a lot with understanding the code :)
    Thank You!

  4. Tchuta Leonard:

    Thanks for teaching me PHP basics in less than 24 hours ..

  5. Red Frog Travel China:

    It is cool having this guide. I am using it to design a site for our travel agency in china. Php is cool!

  6. Ionut Vancea:

    I am not beginner in programming, but I found this tutorial and I wanted to read it to see how you explain the basics of PHP. It is a useful tutorial for newbies, and it will give them the necessary knowledge to start.

  7. S4NT4:

    Hmmm..nice dude!

  8. virender sharma:

    it is very easy to use

  9. sitesh agrawal:

    very nece and esy way presentation

  10. Will Knight:

    Its good to know that functions also makes your code easier to read and understand, especially when you choose sensible names for functions.

Leave a comment